我有一个与Laravel一起开发的网站,在我的主要路线上说www.example.com/
。我已经用Nginx和php-fpm正确配置了它。我的配置如下。
然后我在路线/blog
(www.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
Laravel.htaccess 文件更改添加此行
RewriteCond %{REQUEST_URI} !^/blogs/
博客文件夹 .htaccess 文件 添加此代码或修改
重写引擎打开重写基础/博客/# <==== 更改行!!重写规则 ^索引.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ./blogs/index.php [L] # <==== 更改行
!!在 nginx 配置文件中,尝试在端口 80 和端口 443 代码块中修改以下代码
location /blogs { try_files $uri $uri/ /blogs/index.php?$args; set $base /var/www/html; root $base/public; location ~ '.php$ { fastcgi.. }
}