nginx 为所有 php 个文件提供 404,但 index.php

nginx serves 404 for all php files but index.php

我是 nginx 的新手,正在尝试在其上移动一个 wordpress 网站。

问题是我需要 运行 一个名为 "installer.php" 的文件,nginx 显示 404 错误(来自 domain/rocketstack/installer.php)。 如果我添加了一个特定的 "location" 指令,我会返回一个 "No input file specified" 错误(不确定我是否做对了)。 直接访问 domain/rocketstack/index.php returns 相同的 404,但如果我转到 domain/rocketstack/ 则有效(我猜这很好)。

我在 ubuntu 18.04 上使用 php7.2-fpm,"installer.php" 在 /var/www/rocketstack/ 中,权限为 644。已设置 cgi.fix_pathinfo=0在 php.ini。 为了设置环境,我使用了这个指南:https://www.wpintense.com/2018/10/20/installing-the-fastest-wordpress-stack-ubuntu-18-mysql-8/

这是我的 /etc/sites-available/rocketstack.conf 文件

我该如何解决这个问题?我为此浪费了很多时间!然而,它必须如此简单!非常感谢

# This config file uses nginx fastcgi-cache
fastcgi_cache_path /var/www/cache levels=1:2 keys_zone=rocketstack:100m inactive=60m;

server {
    listen 80;
    listen [::]:80;
    server_name _;

    root /var/www/rocketstack;

    index index.php index.htm index.html;

    access_log /var/log/nginx/rocketstack_access.log;
    error_log /var/log/nginx/rocketstack_error.log;

    include snippets/acme-challenge.conf;

    # Exclusions
    include snippets/exclusions.conf;

    # Security
    include snippets/security.conf;

    # Static Content
    include snippets/static-files.conf;

    # Fastcgi cache rules
    include snippets/fastcgi-cache.conf;

    include snippets/limits.conf;

    include snippets/nginx-cloudflare.conf;

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

    location ~ (^|/)\. {
        return 403;
    }
    location ~/installer.php {
    root /var/www/rocketstack/;
        fastcgi_index  installer.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include snippets/fastcgi-params.conf;
    include fastcgi.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include snippets/fastcgi-params.conf;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;

        # Skip cache based on rules in snippets/fastcgi-cache.conf.
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;

        # Define memory zone for caching. Should match key_zone in fastcgi_cache_path above.
        fastcgi_cache rocketstack;

        # Define caching time.
        fastcgi_cache_valid 60m;
        #increase timeouts
        fastcgi_read_timeout 6000;
        fastcgi_connect_timeout 6000;
        fastcgi_send_timeout 6000;
        proxy_read_timeout 6000;
        proxy_connect_timeout 6000;
        proxy_send_timeout 6000;
        send_timeout 6000;

        #these lines should be the ones to allow Cloudflare Flexible SSL to be used so the server does not need to decrypt SSL
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-NginX-Proxy true;

    }

}

server {
    listen              443 ssl http2 default_server;
    listen              [::]:443 ssl http2 default_server ;
    server_name _;

    root /var/www/rocketstack;

    index index.php index.htm index.html;

    access_log /var/log/nginx/rocketstack_ssl_access.log;
    error_log /var/log/nginx/rocketstack_ssl_error.log;

    #once you have SSL certificates using LetsEncrypt you can alter the paths in the two lines below to reflect your domain and uncomment the lines by removing the leading # symbol
    #ssl_certificate           /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    #ssl_certificate_key       /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    # Exclusions
    include snippets/exclusions.conf;

    # Security
    include snippets/security.conf;

    # Static Content
    include snippets/static-files.conf;

    # Fastcgi cache rules
    include snippets/fastcgi-cache.conf;

    include snippets/limits.conf;

    include snippets/nginx-cloudflare.conf;

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

    location ~ \.php$ {
        try_files $uri =404;
        include snippets/fastcgi-params.conf;

        fastcgi_pass unix:/run/php/php7.2-fpm.sock;

        # Skip cache based on rules in snippets/fastcgi-cache.conf.
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;

        # Define memory zone for caching. Should match key_zone in fastcgi_cache_path above.
        fastcgi_cache rocketstack;

        # Define caching time.
        fastcgi_cache_valid 60m;
        #increase timeouts
        fastcgi_read_timeout 6000;
        fastcgi_connect_timeout 6000;
        fastcgi_send_timeout 6000;
        proxy_read_timeout 6000;
        proxy_connect_timeout 6000;
        proxy_send_timeout 6000;
        send_timeout 6000;

        #these lines should be the ones to allow Cloudflare Flexible SSL to be used so the server does not need to decrypt SSL if you wish
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-NginX-Proxy true;

    }

}

通过反复试验修复。当前设置:

    location ~ \installer.php {
    try_files $uri $uri/ /installer.php?$args;
    fastcgi_index installer.php;
    include snippets/fastcgi-params.conf;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

问题是我没有告诉 nginx 在哪里选择 installer.php,我用 try_files 实现了这一点。 location 中的 Root 设置也是多余的。