Wordpress 中的永久链接不适用于 Laravel + Nginx


Permalinks in Wordpress not working with Laravel + Nginx

我有一个与Laravel一起开发的网站,在我的主要路线上说www.example.com/。我已经用Nginx和php-fpm正确配置了它。我的配置如下。

然后我在路线/blogwww.example.com/blog/)中添加了一个博客,并用Nginx alias配置了它。

现在的问题是Wordpress中的永久链接不起作用。Nginx重定向到Laravel的404页面。

例如,当用户输入这样的URL时:example.com/blog/about,Laravel的404页面显示出来,这很奇怪。

我该如何解决这个问题?如何配置 Nginx?怎么了?

server {
    listen       80;
    server_name  example.com;
    root /usr/share/nginx/html/;
    location /blog {
        try_files $uri $uri/ /index.php?$args;
        alias /usr/share/nginx/blog/;
        index  index.php index.html index.htm;
        location ~ '.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME   /usr/share/nginx$fastcgi_script_name;
        }
    }
    location / {
        root   /usr/share/nginx/main_site;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php$is_args$args;
        location ~ '.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

location与别名路径的末尾匹配时,不需要使用 alias。请参阅此文档。

location /blog中的try_files需要默认为WordPress路由器(/blog/index.php),而不是Laravel路由器(/index.php)。

尝试:

location /blog {
    try_files $uri $uri/ /blog/index.php?$args;
    root /usr/share/nginx;
    ...
    location ~ '.php$ {
        ...
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }
}

假设你在服务器上运行laravel,你想使用wordpress作为博客。 并且您在名为文件夹的博客中安装了WordPress。然后你必须在 3 个地方进行更改

1) laravel .htaccess file
2) blogs folder .htaccess file
3) nginx configuration file
  1. Laravel.htaccess 文件更改添加此行

    RewriteCond %{REQUEST_URI} !^/blogs/

  2. 博客文件夹 .htaccess 文件 添加此代码或修改

    重写引擎打开重写基础/博客/# <==== 更改行!!重写规则 ^索引.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ./blogs/index.php [L] # <==== 更改行

    !!
  3. 在 nginx 配置文件中,尝试在端口 80 和端口 443 代码块中修改以下代码

     location /blogs {
     try_files $uri $uri/ /blogs/index.php?$args;
     set                     $base /var/www/html;
     root                    $base/public;
    
     location ~ '.php$ {
         fastcgi..
     }
    

    }