【问题标题】:Permalinks Issue - Running Wordpress on NGINX behind a reverse proxy that is also NGINX永久链接问题 - 在也是 NGINX 的反向代理后面的 NGINX 上运行 Wordpress
【发布时间】:2017-07-01 01:51:01
【问题描述】:

我有 2 个运行 NGINX 的服务器实例。

  1. NGINX-A,它是反向代理。它根据配置中的位置块将流量转发到不同的内部服务器
  2. NGINX-B 是一个服务器实例,部署了我们所有的 PHP Web 应用程序。
  3. 其他服务器实例

因此,NGINX-A 接收所有传入流量,将其重新路由到适当的服务器。 NGINX-B 是在 NGINX 而不是 Apache 上运行的传统 Wordpress 主机。


我正在尝试使 postname permalinks 工作,但未能成功。

我当前的目录结构:

/appl/wordpress/myblog == symbolic link ==> /usr/share/nginx/html/subdir/myblog

这是我在安装了 Wordpress 的 NGINX-B 服务器上的当前配置。

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

通过http://NGINX_B-ip_address/subdir/myblog 访问它,postname permalinksplain permalinks 都可以。我确实必须像下面这样更新 wp-config。

define('WP_HOME', 'NGINX_B-ip_address')
define('WP_SITEURL','NGINX_B-ip_address');

现在,我有另一个 nginx 服务器充当反向代理,NGINX-A。

location /{
    proxy_pass http://NGINX_B-internal_ip_address;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    #for wordpress
    proxy_set_header X-Forwarded-Proto $scheme;
}

我还必须使用新主机名更新 wp-config.php

define('WP_HOME', 'http://NGINX-A-hostname/myblog')
define('WP_SITEURL','http://NGINX-A-hostname/myblog');

它使用 plain permalinks 工作,我可以毫无问题地访问 wp-admin。我还可以更改永久链接策略。

如果我不使用 plain permalinks,整个事情就会中断并不断收到 404 错误。

  1. http://NGINX-A-hostname/myblog - 有效
  2. http://NGINX-A-hostname/myblog/wp-admin - 完全有效
  3. http://NGINX-A-hostname/myblog/postname - 失败 | 404
  4. http://NGINX-A-hostname/myblog/?p=postname - 完全有效

就好像 NGINX 反向代理将请求作为目录而不是参数转发。有什么建议吗?谢谢!

【问题讨论】:

  • 请问,您为什么使用单独的 nginx 服务器作为来自另一个 nginx 服务器/实例的代理来提供内容?如果仅用于缓存目的,您最好只使用 nginx 的 FastCGI 缓存作为 PHP-FPM 的代理,但在同一台服务器上。它更高效、更可靠。如果您出于其他原因这样做,那么您能否指出您尝试使用哪种永久链接策略,哪个不起作用,以及您现在正在使用哪个永久链接策略,哪个有效?
  • 我不明白您的问题中 subdir 的用途 - 或者 http://hostname/myblog URI 如何找到通往 /subdir/myblog/ 的方式?
  • @ablopez 你好!我已经更新了问题以使其清楚。普通永久链接在 IP 地址和主机名设置中都有效。 Postname 永久链接仅在通过 IP 直接访问时才有效。
  • 好的,你可以在你的 NGINX-B WP 设置中的当前主题的 functions.php 文件中添加一个exit('URI: '.$_SERVER['REQUEST_URI']);,然后访问一个帖子页面(使用 /%postname%/ 永久链接)从它通过 NGINX-A,让我知道 WP 在 NGINX-B 中接收到什么 URI?
  • @ablopez 你好!我得到 404 not found 错误。似乎 NGINX 将 myblog/ 之后的任何内容都视为目录而不是参数。访问 wp-admin 没有任何问题。

标签: wordpress nginx


【解决方案1】:

好的,我设法通过将它应用到 NGINX-B 服务器来解决我的问题

location /myblog {
        root /usr/share/nginx/html/subdir;
        try_files $uri $uri/  /myblog/index.php?$args;
        location  ~ \.php$  {
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              fastcgi_index   index.php;
              fastcgi_pass unix:/var/run/php5-fpm.sock;
              include /etc/nginx/fastcgi_params;
              fastcgi_param   SCRIPT_FILENAME $request_filename;
        }
}

我之前的配置是这样的,没有用。

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

这也不起作用。不取根入口。 myblog 部分不应存在,myblog 应包含在 try_files 中。

location /myblog {
root /usr/share/nginx/html/subdir/myblog;
    try_files $uri $uri/  /index.php?$args;
    location  ~ \.php$  {
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_index   index.php;
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               include /etc/nginx/fastcgi_params;
               fastcgi_param   SCRIPT_FILENAME $request_filename;
       }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    相关资源
    最近更新 更多