php - 使用 nginx 上游的 Wordpress 常量重定向

标签 php wordpress redirect nginx

出现了一种情况,运行 Nginx 的 server1 会将所有“/”位置转发到 server2,同时将“/api”和其他一些位置保留在 server1 上。这也是试图保持 SSL 工作。试图从 http://test.example.com 移动 WP 网址至 https://example.com已正确加载首页,但正在加载 wp-admin给出了太多的重定向。

服务器1 Nginx:

上游 webapp_url {
服务器IP:80;
}

服务器 {
听 443 ssl;
server_name www.example.com example.com;
access_log/var/log/nginx/example.log;

ssl_certificate/etc/nginx/ssl/example.crt;
ssl_certificate_key/etc/nginx/ssl/server.key;
ssl_ciphers RC4:HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers 开启;

位置/文件/{
根/家;
access_log off;
最大到期;
if ($request_filename !~* ^.*?\.(jpg)|(png)|(gif)|(pdf)){
add_header 内容处理:“$request_filename”;
}
}

地点/{
# proxy_pass http://site_url/;
proxy_http_version 1.1;
proxy_set_header 升级 $http_upgrade;
proxy_set_header 连接'升级';
proxy_set_header 主机 $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-示例“1”;
proxy_pass http://webapp_url/;
}

这可以很好地加载其他服务器,主页和链接都可以正常工作(尽管混合内容警告,因为我无法在管理员中更改它)。 WP siteurlhome都设置为新地址。

Server2 Nginx:

服务器 {
#听443 ssl;
听80;
server_name example.com test.example.com;
client_max_body_size 30M;
error_log/var/log/wordpress/error.log 信息;
地点/{
根/home/wordpress;
try_files $uri $uri//index.php?q=$request_uri;
index index.php index.html index.htm;
}

#ssl_certificate/etc/nginx/ssl/example.crt;
#ssl_certificate_key/etc/nginx/ssl/example.key;
#ssl_ciphers RC4:HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;

error_page 404/404.html;
位置 =/404.html {
根目录/usr/share/nginx/html;
}

error_page 500 502 503 504/50x.html;
位置 =/50x.html {
根目录/usr/share/nginx/html;
}

#
位置 ~\.php$ {
根/home/wordpress;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
包括 fastcgi_params;
}
}

加载中 /wp-admin/开始无限重定向(到同一个网址)。我已经在 wp-config.php 中定义了它也:

定义('WP_HOME','https://example.com');
定义('WP_SITEURL','https://example.com');

最佳答案

wp-admin检查连接是否安全,否则重定向到 https相同网址的版本。在像您这样的情况下,这会导致重定向循环。

您需要告诉 WordPress 连接是安全的。

我注意到您在服务器 1 上设置了适当的 header :

proxy_set_header X-Forwarded-Proto $scheme;

(在您的情况下, $scheme 的值被硬连接到 https )。

但是,您还需要以 HTTPS 的形式将其传递给 WordPress。参数,值为 on .

这是通过 map 实现的指令(在服务器 2 上):
map $http_x_forwarded_proto $https_flag {
    default off;
    https on;
}
server {
    ...
}

(map 指令放置在 http 块内。您可以将它放在 server 块的正上方,如上所示。有关详细信息,请参阅 this document)

此外,添加另一个 fastcgi_param通过 HTTPS WordPress 的参数(在服务器 2 上):
location ~ \.php$ {
    ...
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS $https_flag;
    ...
}

关于php - 使用 nginx 上游的 Wordpress 常量重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338854/

相关文章:

jquery - 重定向后从 XHR 对象获取最终 url

c# - Response.Redirect 动态传递一个值

php - 使用 php 处理来自 iphone 应用程序的 json base64 编码图像

css - 如何让大众字体停止增长

wordpress - 以管理员身份登录时无法升级 WordPress 或添加新插件(使用 Sage 主题)

html - 如何使用 float 使 Logo 图像居中

javascript - 表单提交后重定向

php - 如何仅使用核心 php 将图像上传到多个文件夹?

javascript - 在谷歌地图中显示 JSON 响应(纬度、经度)

php - 数组值被截断为一位数