Nginx: 设置默认文件扩展名

6

如何在nginx中设置默认的文件扩展名为.php?

我目前通过 www.mywebsite.com/home.php 访问网页,但我希望只使用 www.mywebsite.com/home

谢谢

1个回答

10
假设您还想提供静态文件服务,您可以使用类似以下的方法:
server {
  server_name example.com;

  # Set the docroot directly in the server
  root /var/www;

  # Allow index.php or index.html as directory index files
  index index.html index.php;

  # See if a file or directory was requested first.  If not, try the request as a php file.
  location / {
    try_files $uri $uri/ $uri.php?$args;
  }

  location ~ \.php$ {
    # If the php file doesn't exist, don't pass the request to php, just return a 404
    try_files $uri =404;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;

    fastcgi_pass your_php_backend_address;
  }
}

1
为了传递参数,必须包括"$args"。我的意思是,"try_files"行应该是:try_files $uri $uri/ $uri.php?$args; - xecute
try_files $uri =404; 在我的 nginx v1.4.1 中导致每个请求都返回 404 错误,但是 try_files $uri $uri/ $uri.php?$args; 在其他情况下非常好用。 - Bendihossan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接