nginx如果url包含特定单词,如何重定向到wordpress文件夹

问题描述 投票:2回答:1

首先,我的域根配置为使用反向代理服务Angular网页,该代理重定向到本地IP /端口,这就像魅力一样。如果url包含我要重定向到wordpress文件夹的/blog,我想要覆盖根规则时问题就到了。目前,通过这个配置,我可以达到wordpress但只是特定的网址,如example.com/blog/wp-admin/index.php,但如果我访问example.com/blogis仍然会去角度应用程序。我按如下方式配置了我的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
}

如果我停止我的角度应用程序,它工作得很好,所以我认为我需要首先触发/博客位置,但我尝试了所有可能的形式,没有结果。有人看到了什么问题吗?我认为第一条规则首先被触发但似乎没有。

提前致谢。

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

nginx nginx-location nginx-config
1个回答
1
投票

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;
    ...
}

有关更多信息,请参阅this document。很明显,你的try_files语句包含一个虚假的参数。

© www.soinside.com 2019 - 2024. All rights reserved.