簡體   English   中英

重寫文件擴展名后,Nginx服務器不會重定向

[英]Nginx server not redirect after rewrite file extension

我想刪除php文件擴展名。 所以我搜索並嘗試了這段代碼。

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

它只是說“找不到檔案”。 在我寫'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;
    }


}

我怎樣才能解決這個問題??

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

您應該修改您的location ~ \\.php$ block,以便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語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM