NGINX代理到WordPress网站

9

我将要为一个静态网站(使用nginx)添加一个wordpress博客(托管在另一个实例中),并将其放置于“/blog”文件夹下。当使用nginx代理时:

location /blog/ {
   proxy_set_header X-Is-Reverse-Proxy "true";
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_pass http://55.555.55.555;
}

以下是 wp-config.php:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'http://SOMESITE.com/blog/');

所有来自wp-content(和wp-includes)的文件都无法正确访问,因为它们被搜索在http://SOMESITE.com/wp-content/*而不是http://SOMESITE.com/blog/wp-content/*下面。

添加一些额外的代理规则没有起作用,例如:

location ~* (\/wp-content) {
   proxy_set_header X-Is-Reverse-Proxy "true";
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_pass http://55.555.55.555;
}

不要重新定义wp-config.php:

define('WP_SITEURL', 'http://SOMESITE.com/blog/');

希望能得到任何想法。 我也相信这是一个常见的用例,但在这里找不到任何有效的技巧。

谢谢。

5个回答

25

我最终成功搭建了一个逆向代理NGINX用于Wordpress博客!

我的设置是使用端口8080为NGINX提供的Wordpress网站和默认站点(在端口80上),该默认站点将Wordpress博客服务于子目录"blog"。(例如,http://www.example.com/blog)。

在我的“默认站点”NGINX配置中,我定义了以下反向代理位置:

location ^~ /blog/ {
  proxy_pass http://127.0.0.1:8080/;
  proxy_set_header Host $http_host;
  proxy_set_header X-Forwarded-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-Forwarded-Proto $scheme;
}
在wp-config.php中,我进行了以下修改:
/** set the site URL */
define('WP_HOME','http://www.example.com/blog');
define('WP_SITEURL','http://www.example.com/blog');

/** Fix to get the dashboard working with the reverse proxy.*/
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);

为了完整起见,这里是用于WordPress博客的基本NGINX配置:

server {
    listen 8080;
    listen [::]:8080;
    server_name example.com;
    root /var/www/blog;
<...>
    location / {
         index index.php 
         try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include fastcgi.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_intercept_errors on;
         fastcgi_index index.php;
    }
}

版本:

  • Ubuntu 18.04
  • NGINX 版本 1.4
  • PHP 7.2
  • WordPress 4.8.5

1
这个非常有用!显然,在你的 proxy_pass 后面需要加上斜杠(/)。 proxy_pass http://127.0.0.1:8080/; 可以正常工作, 而 proxy_pass http://127.0.0.1:8080; 则不能。 - Nathaniel Rogers
@NathanielRogers 很高兴知道这一点 - 感谢您指出! - Dagmar
1
是的,在nginx中,尾随斜杠非常重要。我已经停止计算因此浪费的时间了。 - Ginterhauser
如果您正在使用nginx作为Web服务器,那么这篇文章非常棒。然而,Docker容器并不适用于此,并且使用Docker容器似乎会导致PHP处理变慢,因为每个PHP文件的服务时间需要5秒钟。当使用默认的Apache时,我们真的需要一个在反向代理本身中进行PHP缓存的示例。 - Alex Balcanquall
@AlexBalcanquall 如果你想在WordPress中使用页面缓存,最简单的方法是使用插件。如果你想要对象缓存,那么通常会使用像memcached或redis这样的东西。我从未在Apache本身中实现过缓存。5秒真的很慢 - 这是一个原始的WP安装还是可能存在其他与WP相关的速度问题? - Dagmar

4

对我来说,答案比较简单:

location /blog {
   proxy_ssl_server_name on;
   proxy_pass https://blog.example.com;
}

wp-config.php

/** set the site URL */
define('WP_HOME','https://www.example.com/blog');
define('WP_SITEURL','https://www.example.com/blog');

/** Fix to get the dashboard working with the reverse proxy.*/
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);

请注意,这是用于 https 的。
所有的功劳归功于 @dagmar,因为我的答案基于他的。我无法在评论中添加,因为它太长了。

1

我终于成功让nginx与WordPress子目录一起运作了。

https://my-website.com/blog
  1. 搭建本地 WordPress 网站
server {
        listen   7789;

        root /data/web/wordpress/;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                index index.php;
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}
  1. nginx反向代理
server {
    listen         443 ssl;
    ...

    # the trailing '/' is vital
    location ^~ /blog/ {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://localhost:7789/;
    }
}
  1. 将定义添加到wp-config.php文件中
/** set the site URL */
define('WP_HOME', '/blog');
define('WP_SITEURL', '/blog');
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);
  1. 更改wp-includes/load.php以支持SSL反向代理
function is_ssl() {
    // Check reverse proxy header, uppercase at server side
    $proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? "http";
    if ($proto == "https")
        return true;
    ...
}
  1. 确保禁用Chrome磁盘缓存以进行调试(F12 -> 禁用缓存)

1

这是我针对像 https://happy-dev.fr/blog 这样的代理 WordPress 的配置:

location /blog/ {
     proxy_set_header X-Original-Request $request_uri;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto $scheme;
     proxy_pass https://blog.happy-dev.fr/;
     proxy_redirect https://$proxy_host/ /;
}

这适用于位于 blog.happy-dev.fr 的 WordPress,无需将其放在 /blog/ 子目录中,因为它覆盖了默认行为,即:
proxy_redirect https://$proxy_host/ /blog/

我在这里找到了这个链接http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

0

我不得不在nginx配置文件中添加一行额外的代码:

location /wp-content {
    proxy_pass https://blog.site.com/wp-content/;
}

这将负责加载您的所有wp-content资源,包括来自您的WordPress插件的资产。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接