2017-02-03 83 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