【问题标题】:nginx how to redirect to wordpress folder if url contains specific word如果url包含特定单词,nginx如何重定向到wordpress文件夹
【发布时间】:2019-07-22 02:26:15
【问题描述】:

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

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
}

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

提前致谢。

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

【问题讨论】:

    标签: nginx nginx-location nginx-config


    【解决方案1】:

    URI /blog 与您的 location 的正则表达式不匹配,这需要在 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 语句包含虚假参数。

    【讨论】:

    • 感谢您的回复 Richard,我已根据您的建议更新了配置,但仍然有相同的行为。这让我认为问题可能出在角度路由中......但我不得不说我已经删除了它的后备规则。不确定我是否在正确的上下文中查找错误...
    • 另外,请确保您清除浏览器的缓存并在进行任何更改后重新启动 Nginx。
    • 是的,我做到了。现在我用 curl curl https://example.com/blog/ 进行了测试,我得到了正确的 wordpress html 内容,而 Chrome 正在重定向到 Angular 应用程序
    • 不,我得到了一个 301 永久移动(我使用了你的第二个代码 sn-p,而不是第三个,如果知道有用的话)
    • 没关系,所以在我看来,Chrome 出于某种原因仍在依赖其缓存。检查访问日志 - 查看 Chrome 从您的服务器请求什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多