簡體   English   中英

nginx + php-fpm = 找不到文件

[英]nginx + php-fpm = File not found

當我嘗試訪問info.php時,我得到一個File not found. 錯誤。

我嘗試了一些教程無濟於事。

配置:默認:

server {
    listen         80;
    listen   [::]:80 default ipv6only=on; 
    server_name  localhost;

    location / {
        root   /var/www;
        index  index.html index.htm index.php;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:7777;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        fastcgi_buffers 256 128k;
        #fastcgi_buffer_size 16k;
        #fastcgi_busy_buffers_size 256k;
        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
        include fastcgi_params;
    }
}

有什么問題?

如果那個 info.php 在 /var/www 中,那么指示 fast_cgi 查找是錯誤的

/usr/share/nginx/html/info.php;

對 html 和 php 使用相同的根。 此外,除了非常特殊的用途外, rootindex參數應該在特定位置之外。

server {
   listen         80;
   listen   [::]:80 default ipv6only=on; 
   server_name  localhost;
   root   /var/www;
   index  index.html index.htm index.php;


   #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

   location ~ \.php$ {
       fastcgi_pass 127.0.0.1:7777;
       fastcgi_index  index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_buffers 256 128k;
       fastcgi_connect_timeout 300s;
       fastcgi_send_timeout 300s;
       fastcgi_read_timeout 300s;
       include fastcgi_params;
    }
}

不用說,您仍然需要確保您的 php-fpm 服務正在偵聽端口 7777。常見情況是讓它偵聽端口 9000。

如果您檢查了所有內容並且配置正確,那么我得到的最后一點是:

  • 如果在文件/etc/php-fpm.d/www.conf提到,請檢查正確的用戶名
server {
listen         80;
listen   [::]:80 default ipv6only=on; 
server_name  localhost;
root   /var/www;
location / {
    index index.php;
}
location ~ \.php(?:$|/) {
   fastcgi_pass 127.0.0.1:7777;
   fastcgi_index  index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_buffers 256 128k;
   fastcgi_connect_timeout 300s;
   fastcgi_send_timeout 300s;
   fastcgi_read_timeout 300s;
   include fastcgi_params;
}

}

Nginx 和符號鏈接

注意,如果root /var/www; 是一個符號鏈接,將其更改為root /var/www/; 否則 nginx 不會通過符號鏈接,你會得到一個File not found錯誤

location nginx 部分,如果您使用root$document_root$fastcgi_script_name的值是正確的。 但是,如果您使用alias該值是錯誤的。 使用alias您需要使用$request_filename而不是$document_root$fastcgi_script_name

location /myapp {
    alias /home/site/www/myapp;
    location ~* "\.php$" {
        fastcgi_pass   php_fpm_server:9000;
        fastcgi_index  index.php;

        # Works in all cases.
        fastcgi_param  SCRIPT_FILENAME $request_filename;

        ## Not work for 'alias' directive, only for 'root' directive.
        # fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

上述變量的文檔:

$request_filename

當前請求的文件路徑,基於 root 或 alias 指令,以及請求 URI

$document_root

當前請求的 root 或 alias 指令的值

$fastcgi_script_name

請求 URI,或者,如果 URI 以斜杠結尾,則請求 URI 並附加由 fastcgi_index 指令配置的索引文件名。 此變量可用於設置 SCRIPT_FILENAME 和 PATH_TRANSLATED 參數,以確定 PHP 中的腳本名稱。 例如,對於帶有以下指令的“/info/”請求

閱讀更多

我看了很久,最后發現 FPM 沒有運行 :-s 在我的情況下,一個簡單的服務 php7.2-fpm restart 成功了!

我遇到了這個問題,這里的答案都沒有奏效。 我的問題最終是由 SELinux 引起的。 禁用了 SELinux ,它解決了這個問題。

請注意,禁用 SELinux 確實會影響安全性。 幾乎可以肯定有更好的方法來解決這個問題,但禁用 SELinux 對我有用。

我在嘗試解決類似問題時遇到了這個問題。 所以我會添加我找到的解決方案。 這是在 Arch 上,但它與 systemd 相關。

此解決方案適用於我的開發機器,出於充分的理由,您不應從 /home 文件夾運行公共站點。

我將 php-fpm 和 nginx 配置為以我的用戶身份運行。 編輯以下文件,並刪除 ProtectHome=true 行

sudo vi /etc/systemd/system/multi-user.target.wants/php-fpm.service

重新加載,並重新啟動一切;

systemctl daemon-reload
systemctl restart nginx.service
systemctl restart php-fpm.service

唯一對我有用的是卸載 php 的整個版本,刪除其所有配置文件,然后再次重新安裝 php 版本。

暫無
暫無

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

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