【问题标题】:Redirect to wordpress blog through nginx通过 nginx 重定向到 wordpress 博客
【发布时间】:2016-08-30 14:42:25
【问题描述】:

我对 nginx 和 wordpress 都是新手。 我已将我的项目配置为通过 nginx 服务器提供服务。现在我们需要将 wordpress 博客支持添加到我们的项目中。 我的 nginx 在 80 端口上运行。而 Apache 在安装了我的 wordpress 的 8181 上运行。

现在任何去博客的 url 都会通过我的 nginx 服务器重定向到 8181 上的 apache。

下面是我的博客网址的 nginx 重定向。

location /blog {
            proxy_pass http://127.0.0.1:8181/blog/;
            proxy_redirect https://192.168.1.54 http://127.0.0.1:8181/blog;

            proxy_read_timeout       3500;
            proxy_connect_timeout    3250;

            proxy_set_header   X-Real-IP          $remote_addr;
            proxy_set_header   Host               $host;
            proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto  https;

            proxy_set_header   SSL_PROTOCOL $ssl_protocol;
            proxy_set_header   SSL_CLIENT_CERT $ssl_client_cert;
            proxy_set_header   SSL_CLIENT_VERIFY $ssl_client_verify;
            proxy_set_header   SSL_SERVER_S_DN $ssl_client_s_dn;
        }

为了了解 Google 页面速度,我在下面进行了配置。

location ~* \.(?:ico|css|js|gif|jpe?g|png|woff)$ {
            expires 30d;
            add_header Pragma public;
            add_header Cache-Control "public";
        }

现在,我在这里面临的问题是,当一些 css 或 js 请求来自 wordpress 例如。 http://192.168.1.54/blog/wp-content/themes/twentysixteen/style.css?ver=4.6 被困在为 css 编写的位置配置中,即。 location ~* .(?:ico|css|js|gif|jpe?g|png|woff)$ 因此 wordpress 无法获取 css 或 js。

所以,我需要你的帮助,将 nginx 上 blog 请求的任何 URL 重定向到 8181 端口上的 Apache 服务器。包括js和css。 像https://192.168.1.54/js/somejs.js 这样的其他与资源相关的网址也应该可以工作。

我在 wordpress 文件中做了一些配置更改 wp-config.php

define('WP_HOME', 'https://192.168.1.54/blog');
define('WP_SITEURL', 'https://192.168.1.54/blog');

【问题讨论】:

    标签: php wordpress apache nginx location


    【解决方案1】:

    您应该在location /blog 块上使用^~ 修饰符,以防止与同一级别的正则表达式位置块发生冲突。详情请见this document

    location ^~ /blog {
        proxy_pass http://127.0.0.1:8181;
        ...
    }
    

    此外,您可能应该从 proxy_pass 指令中删除 URL 元素,因为看起来您无论如何都在将 /blog 映射到 /blog

    proxy_redirect 声明又回到了前面。但是你可能不需要它,因为你已经设置了 WP_HOME/WP_SITEURL。

    【讨论】:

    • 感谢理查德的回复。这再次阻止了我的博客网址重定向到博客。但我的主要问题仍然存在。 wordpress css 和 js 仍然显示 404。我仍然需要对块位置 ~* \.(?:ico|css|js|gif|jpe?g|png|woff)$ 做一些事情。如果我删除此块,则为 wordpress 加载 css 和 js。但不是这个块。
    • 您的配置文件中的其他地方一定有问题,因为location ~* 无法覆盖location ^~ - 请参阅我在回答中引用的文档。
    猜你喜欢
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 2015-12-16
    • 2018-02-13
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多