【问题标题】:NGINX Proxy to wordpress websiteNGINX 代理到 wordpress 网站
【发布时间】:2016-11-07 10:14:57
【问题描述】:

我有一个静态服务的站点(使用 nginx)。 我想在 /blog 文件夹下托管一个 wordpress 博客(托管在不同的实例上)。 使用 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/');

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

谢谢。

【问题讨论】:

    标签: wordpress nginx proxy


    【解决方案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

    【讨论】:

      【解决方案2】:

      我终于得到了一个为 Wordpress 博客工作的 NGINX 反向代理!

      我的设置是一个由 NGINX 在端口 8080 上提供服务的 Wordpress 站点和一个在子目录“blog”上提供 Wordpress 博客的默认站点(在端口 80 上)。 (例如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

      【讨论】:

        【解决方案3】:

        我不得不在 nginx 配置文件中添加一行:

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

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

        【讨论】:

          【解决方案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 所有,因为我的回答是基于他的。我无法放入 cmets,因为它不适合。

          【讨论】:

            猜你喜欢
            • 2021-08-23
            • 2021-05-01
            • 2018-02-15
            • 1970-01-01
            • 2015-03-24
            • 2022-11-29
            • 2017-09-06
            • 1970-01-01
            相关资源
            最近更新 更多