2016-06-08 64 views
1

我想刪除php文件擴展名。所以我搜索並嘗試了這個代碼。重寫文件擴展名後Nginx服務器不重定向

它運作良好,但當我嘗試不存在的頁面。

它只是說「文件未找到」。在我寫'try_files $ uri ...;'之前它返回/404.html。

我試過try_files $ uri = 404;在.php $中,但所有頁面都返回到/404.html。

server { 
    listen  80 default_server; 
    listen  [::]:80 default_server; 
    server_name localhost; 
    root   /var/www/html; 

    include /etc/nginx/default.d/*.conf; 

    location/{ 
      try_files $uri $uri.html $uri/ @extensionless-php; 
      index index.html index.php; 
    } 

    location ~ \.php$ { 
      root   html; 
      fastcgi_pass 127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; 
      include   fastcgi_params; 
    } 

    location @extensionless-php { 
      rewrite ^(.*)$ $1.php last; 
    } 


    error_page 404 /404.html; 
     location = /40x.html { 
      root /var/www/html; 
    } 

    error_page 500 502 503 504 /50x.html; 
     location = /50x.html { 
      root /var/www/html; 
    } 


} 

我該如何解決這個問題?

回答

0

您正在將.php文件傳遞給PHP處理器而不檢查是否存在。

你應該修改你的location ~ \.php$塊,以便nginx處理不存在的文件和404響應,而不是讓PHP抱怨。

嘗試類似:

location ~ \.php$ { 
    try_files $uri =404; 
    include   fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_pass 127.0.0.1:9000; 
} 

我已刪除多餘的root指令,因爲它只是混亂,所以該塊現在繼承了外塊正確的值。我刪除了在這種情況下未使用的fastcgi_index。我已經在include下面訂購fastcgi_param,以免悄悄覆蓋您的fastcgi_param陳述。