nginx如何重定向到WordPress文件夹,如果URL包含特定的单词



首先,我的域名根配置为使用重定向到本地IP/端口的反向代理为 Angular 网页提供服务,这就像一个魅力。现在,当我想覆盖根规则时,如果 url 包含我要重定向到 wordpress 文件夹/blog,问题就来了。目前,使用此配置,我可以访问wordpress,但只能访问特定的URL,例如example.com/blog/wp-admin/index.php,但是如果我访问example.com/blog仍然会转到角度应用程序。我已经配置了我的nginx如下(我不得不说这是我第一次配置网络服务器):

server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name example.com www.example.com;
client_max_body_size 100M;
root /var/www;
index index.php index.html index.htm index.nginx-debian.html;
autoindex off;
location ~ /blog(.*)+/(.*)$ {
try_files $uri $uri/ /blog/index.php?$args /blog/index.php?q=$uri&$args;
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location / {
proxy_pass http://127.0.0.1:4000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true; proxy_redirect off;
http2_push /var/www/example_frontend/dist/example-frontend/favicon.ico;
http2_push /var/www/example_frontend/dist/example-frontend/manifest.json;
}
location /robots.txt {
alias /var/www/example_frontend/robots.txt;
}
location /sitemap.xml {
alias /var/www/example_frontend/sitemap.xml;
}
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
}

server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}

如果我停止我的角度应用程序,它就会完美运行,所以我认为我需要首先触发/blog 位置,但我尝试了所有可能的形式,但没有结果。有人看出出了什么问题吗?我以为第一条规则首先被触发,但似乎没有。

提前谢谢。

如果需要,我可以附加任何其他配置文件;)

URI/bloglocation的正则表达式不匹配,这需要 URI 中某处的额外/才能匹配。

简单的解决方案是:

location /blog {
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
...
}

以上将匹配/blog/blog/,但也匹配/blogx(这可能是不可取的)。


您可以使用修改后的正则表达式,例如:

location ~ ^/blog(/|$) {
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
...
}

最有效的解决方案是使用前缀位置,但要键入更多内容:

location /blog {
return 301 /blog/;
}
location /blog/ {
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
...
}

有关详细信息,请参阅此文档。偶然地,您的try_files语句包含一个虚假参数。

相关内容

最新更新