如何解決Ubuntu系統中nginx部署PHP項目時所有請求返回404錯誤的問題?

如何解決Ubuntu系統中nginx部署PHP項目時所有請求返回404錯誤的問題?

ubuntu系統中使用nginx部署php項目時,經常會遇到所有請求都返回404錯誤的情況。這通常是由于Nginx配置文件配置錯誤導致的。本文將詳細講解如何正確配置Nginx,確保你的PHP項目能夠在8088端口正常運行。

一位用戶在部署PHP項目時,遇到了所有接口返回404的難題。以下是他的Nginx配置文件:

server {     listen       8088;     server_name  localhost;     root   /var/www/html;      location / {         index  index.php index.html index.htm;         try_files $uri $uri/ /index.php$is_args$args;     }      location ~ .php$ {         try_files $uri =404;         fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;         fastcgi_index index.php;         fastcgi_param script_filename $document_root$fastcgi_script_name;         include fastcgi_params;     } }

問題出在location ~ .php$塊中的try_files指令。正確的配置如下:

server {     listen       8088;     server_name  localhost;     root   /var/www/html;      location / {         index  index.php index.html index.htm;         try_files $uri $uri/ /index.php$is_args$args;     }      location ~ .php$ {         # try_files $uri =404;  刪除錯誤的try_files指令         fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;         fastcgi_index index.php;         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;         include fastcgi_params;          # 關鍵修改         fastcgi_split_path_info ^(.+.php)(/.+)$;         fastcgi_param PATH_INFO $fastcgi_path_info;     } }

關鍵在于添加了fastcgi_split_path_info和fastcgi_param PATH_INFO指令。這兩條指令對于處理PHP的URL重寫至關重要,尤其是在使用thinkphp 6 (TP6)等框架時。 如果使用TP6,可以搜索“TP6 Nginx fastcgi”獲取更多配置示例。 同時,請注意將script_filename參數中的script_filename改為SCRIPT_FILENAME (大小寫敏感)。

立即學習PHP免費學習筆記(深入)”;

另外,請確保php-fpm服務正在運行,并且/var/run/php/php7.4-fpm.sock路徑正確。 如果使用其他版本的PHP,請相應調整sock文件的路徑。

通過以上配置和檢查,你的PHP項目應該能夠在8088端口正常訪問。

以上就是如何解決Ubuntu系統中

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享