NGINX 代理到 wordpress 网站 [英] NGINX Proxy to wordpress website

查看:49
本文介绍了NGINX 代理到 wordpress 网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态服务站点(使用 nginx).我想在/blog 文件夹下托管一个 wordpress 博客(托管在不同的实例上).使用 nginx 代理时:

I have a statically served site (using nginx). I want to host a wordpress blog (hosted on a different instance) under /blog folder. When using a nginx proxy:

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:

and the following 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/*.

All the files from wp-content (and wp-includes) are not being served correctly, since they are being searched under http://SOMESITE.com/wp-content/* instead of http://SOMESITE.com/blog/wp-content/*.

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

Adding some extra proxy rules didn't work, ex:

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

nor 重新定义 wp-config.php:

nor redefining the wp-config.php:

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

会喜欢任何想法.我也很确定这是一个常见的用例,但在这里找不到任何工作技巧.

Would love any ideas. I am also pretty sure it is a common use case, but couldn't find any working tricks here.

谢谢.

推荐答案

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

I finally got a NGINX reverse proxy working for a Wordpress blog!

我的设置是一个由 NGINX 在端口 8080 上提供的 Wordpress 站点和一个默认站点(在端口 80 上),它在子目录blog"上提供 Wordpress 博客.(例如http://www.example.com/blog).

My setup is a Wordpress site served by NGINX on port 8080 and a default site (on port 80) that serves the Wordpress blog on the subdirectory "blog". (e.g. http://www.example.com/blog).

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

In my "default site" NGINX configuration, I defined the following reverse proxy location:

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 中我做了以下修改:

In wp-config.php I made the following modifications:

/** 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 配置:

For completeness, here is the basic NGINX config for the Wordpress blog:

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

这篇关于NGINX 代理到 wordpress 网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆