1. 程式人生 > 其它 >Nginx PHP頁面找不到總是返回404

Nginx PHP頁面找不到總是返回404

同目錄下 html 沒有問題,一訪問 *.php 就404

原因:
nginx 配置的 server 段沒有指定 root,導致 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name 中的 $document_root並不起作用

解決方法:
server 段加上 root 網站目錄,或不使用 $document_root變數,轉而使用其它變數或直接指定路徑

server {
    #使用 one 這個快取
        #proxy_cache    one;
        #proxy_cache_valid   any  10m;

        listen         80;
        server_name    abc.com ;

        # 就是增加的下面的 root,指定了當前 server 的root指向哪裡
        root           /a/b/c;

        location / {
            root     /home/wwwroot/tyshgm-www;
            index    index.html  index.php;

            try_files  $uri  $uri/  /index.php?$args;
        }

        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        error_page   401 402 403 404 500 502 503 504  /oops.html;
        location = /oops.html {
            root     /a/b/c;
        }

        location  ~\.php$ {
            root            /a/b/c;
            fastcgi_pass    unix:/run/php-fpm/c.sock;
            #fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include         fastcgi_params;
        }
    }