3

wordpress | 多站点模式下访问某一文件夹文件返回 404

 6 months ago
source link: https://benpaodewoniu.github.io/2024/03/11/wordpress14/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

wordpress | 多站点模式下访问某一文件夹文件返回 404

这是 wordpress 的一个多站点问题。

介绍一下背景,假设域名为 xxx.com,现在新建了一个新的站点 xxx.com/lumi,现在这个站点的主题需要上传一个 SDK,假设这个 SDK 的文件夹名称为 Extend

但是,在多站点的配置下,访问 xxx.com/lumi/Extend/a.js 返回 404

我的 wordpress 是放置在 /var/www/wordpress 里面的。

所以,我将 Extend 上传到 /var/www/wordpress/Extend 或者 /var/www/wordpress/lumi/Extend 还是 404

然后我修改 nginx 配置,添加

location /lumi/Extend {
alias /var/www/wordpress/Extend;
}

也不行,经过我长期的排查,最后发现是 nginx 多站点配置问题。

nginx 多站配置为

# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
# server 127.0.0.1:9000;
}

server {
listen 80;
## Your website name goes here.
server_name _;
## Your only path reference.
root /var/www/wordpress; # wordpress 的放置路径
## This should be in your http block and if it is, it's not needed here.
index index.php;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}

#ignored: “-” thing used or unknown variable in regex/rew
rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ permanent;

if (-f $request_filename){
set $rule_2 1;
}
if (-d $request_filename){
set $rule_2 1;
}
if ($rule_2 = "1"){
#ignored: “-” thing used or unknown variable in regex/rew
}
rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*.php)$ /$2 last;
rewrite /. /index.php last;
}

最后发现,是最后一句话

rewrite /. /index.php last;

影响了。这句话是说,把所有的 url 都定向到 index.php

所以,我们要对这句话进行改造,还要加入

location /lumi/Extend {
alias /var/www/wordpress/Extend;
}
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/run/php/php7.4-fpm.sock;
#server 127.0.0.1:9000;
}


server {
listen 80;
## Your website name goes here.
server_name thlm.com www.thlm.com;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;#新增这两个
ssl_certificate /home/ubuntu/https/fullchain.crt;
ssl_certificate_key /home/ubuntu/https/private.pem;
#rewrite ^(.*) https://$server_name$1 permanent;
#return 301 https://$host$request_uri;

## Your only path reference.
root /var/www/wordpress; # wordpress 的放置路径
## This should be in your http block and if it is, it's not needed here.
index index.php;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location /lumi/Extend {
alias /var/www/wordpress/Extend/;
}

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found on;
}
#ignored: “-” thing used or unknown variable in regex/rew
rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ permanent;

if (-f $request_filename){
set $rule_2 1;
}
if (-d $request_filename){
set $rule_2 1;
}
if ($rule_2 = "1"){
#ignored: “-” thing used or unknown variable in regex/rew
}
rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*.php)$ /$2 last;
rewrite ^/(?!lumi/Extend)(.*)$ /index.php?$1 last;
}

完美解决。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK