如何解決Ubuntu系統(tǒng)中nginx部署PHP項(xiàng)目時(shí)所有請(qǐng)求返回404錯(cuò)誤的問(wèn)題?

如何解決Ubuntu系統(tǒng)中nginx部署PHP項(xiàng)目時(shí)所有請(qǐng)求返回404錯(cuò)誤的問(wèn)題?

ubuntu系統(tǒng)中使用nginx部署php項(xiàng)目時(shí),經(jīng)常會(huì)遇到所有請(qǐng)求都返回404錯(cuò)誤的情況。這通常是由于Nginx配置文件配置錯(cuò)誤導(dǎo)致的。本文將詳細(xì)講解如何正確配置Nginx,確保你的PHP項(xiàng)目能夠在8088端口正常運(yùn)行。

一位用戶(hù)在部署PHP項(xiàng)目時(shí),遇到了所有接口返回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;     } }

問(wèn)題出在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;  刪除錯(cuò)誤的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;          # 關(guān)鍵修改         fastcgi_split_path_info ^(.+.php)(/.+)$;         fastcgi_param PATH_INFO $fastcgi_path_info;     } }

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

立即學(xué)習(xí)PHP免費(fèi)學(xué)習(xí)筆記(深入)”;

另外,請(qǐng)確保php-fpm服務(wù)正在運(yùn)行,并且/var/run/php/php7.4-fpm.sock路徑正確。 如果使用其他版本的PHP,請(qǐng)相應(yīng)調(diào)整sock文件的路徑。

通過(guò)以上配置和檢查,你的PHP項(xiàng)目應(yīng)該能夠在8088端口正常訪(fǎng)問(wèn)。

以上就是如何解決Ubuntu系統(tǒng)中

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊12 分享