2017-02-03 82 views
1

我在使用FastCGI启用的NGINX和PHP7中的URL重写中遇到了问题。禁用.php扩展名,但运行没有.php的文件作为php文件

要求如下。

原始地址:http://example.com/somename1.php
响应:它会抛出404错误

原始地址:http://example.com/somename1
响应:它将执行http://example.com/somename1.php

原始地址:http://example.comhttp://example.com/
响应:它将执行http://example.com/index.php

我已经通过了以下网址,但他们不足以满足上述需求。

  1. How to remove both .php and .html extensions from url using NGINX?
  2. remove .php from url with rewrite rule
  3. remove .php extension from url in nginx

样本 “默认” 文件的内容如下。

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    root /var/www/html; 
    index index.php index.html index.htm index.nginx-debian.html; 
    server_name _; 
    location/{ 
     try_files $uri $uri/ =404; 
    } 
    location ~ \.php$ { 
     include snippets/fastcgi-php.conf; 
    } 
    location ~ /\.ht { 
     deny all; 
    } 
} 

有没有人可以帮我解决这个问题,或者指点我一些其他的页面,我可以获得更多信息?

感谢

回答

0

你有三个要求:

原始地址:http://example.com/somename1.php响应:它会抛出 404错误

使用internal指令。有关详细信息,请参阅this document

例如:

location ~ \.php$ { 
    internal; 
    ... 
} 

原始地址:http://example.com/somename1响应:它将执行 http://example.com/somename1.php

有许多的方式来实现,扩展名的PHP。您问题中的第一个链接使用try_files,指定位置和rewrite

原始地址:http://example.comhttp://example.com/响应: 将执行http://example.com/index.php

通常index指令会在这种情况下使用,按你的样品。如果扩展名的PHP计划弄乱这件事,使用精确匹配的位置块:

location =/{ 
    rewrite^/index.php last; 
} 

更多见this document

+0

此答案不完整,对遇到同样问题的人没有帮助。应该在这里展示所有部分的完整答案,而不是简单地依赖未来可能无法使用的链接。换句话说,服务器模块在工作时是什么样的? – Tomas