WordPress博客在nginx上发布永久链接捐赠404

时间:2021-04-16 作者:user205008

我有以下nginx配置来为blog提供服务/blog/ URL。

server {
        listen 80;
        server_name example.in www.example.in;
        root /var/www/website;

        index index.html index.htm index.php;

        # Serve blog
        location /blog {
                return 301 /blog/;
        }

        location /blog/ {
                autoindex on;
                alias /var/www/blog/;
                index index.php index.html index.htm;
                try_files $uri $uri/ /index.php$args;

                location ~ \\.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_param  SCRIPT_FILENAME    $request_filename;
                        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                }
        }

        # Serve other files
        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \\.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\\.ht {
                deny all;
        }
}
主页工作正常https://example.in/blog 而且管理面板工作正常https://example.in/blog/wp-admin/

当blog posts permalink设置为plain时,blog posts可以使用URL正常打开

https://example.in/blog/?p=123
但是将永久链接更改为另一种格式blog/blog/2021/04/16/sample-post/, 它给出了404

https://example.in/blog/blog/2021/04/16/sample-post/

1 个回复
最合适的回答,由SO网友:Richard Smith 整理而成

您应该使用^~ 上的前缀location 语句,更改alias 对账单收件人root /var/www;, 并更改try_files 陈述

例如:

location ^~ /blog/ {
    autoindex on;
    root /var/www;
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param  SCRIPT_FILENAME    $request_filename;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}