WordPress的常数重定向与Nginx上游
运行Nginx的server1会将所有“/”位置转发到server2,同时在server1上保留“/ api”和其他几个位置。 这也是为了保持SSL的正常运行。 试图将WP网址从http://test.example.com移动到https://example.com时,首页加载正常,但加载wp-admin会提供太多重定向。
Server1 Nginx:
upstream webapp_url {
server IP:80;
}
server {
listen 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 on;
location /files/ {
root /home;
access_log off;
expires max;
if ($request_filename !~* ^.*?.(jpg)|(png)|(gif)|(pdf)){
add_header Content-Disposition: "$request_filename";
}
}
location / {
# proxy_pass http://site_url/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $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-Example "1";
proxy_pass http://webapp_url/;
}
这加载其他服务器罚款,主页和链接所有工作(虽然混合内容警告,因为我无法在管理中更改它)。 WP siteurl和home都设置为新地址。
Server2 Nginx:
server {
#listen 443 ssl;
listen 80;
server_name example.com test.example.com;
client_max_body_size 30M;
error_log /var/log/wordpress/error.log info;
location / {
root /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;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#
location ~ .php$ {
root /home/wordpress;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
加载/wp-admin/启动无限重定向(到相同的url)。 我也在wp-config.php定义了它:
define('WP_HOME','https://example.com');
define('WP_SITEURL','https://example.com');
wp-admin检查连接是否安全,否则会重定向到相同URL的https版本。 在诸如你的情况下,这会导致重定向循环。
你需要告诉WordPress,连接是安全的。
我注意到你在服务器1上设置了适当的头文件:
proxy_set_header X-Forwarded-Proto $scheme;
(在你的情况下, $scheme的值被硬连线到https )。
但是,您还需要将其传递给WordPress,其格式为HTTPS参数,值为on 。
这是通过map指令实现的(在服务器2上):
map $http_x_forwarded_proto $https_flag {
default off;
https on;
}
server {
...
}
( map指令放在http块中,你可以把它放在server块的上面,如上所示。
另外,添加另一个fastcgi_param将HTTPS参数传递给WordPress(在服务器2上):
location ~ .php$ {
...
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $https_flag;
...
}
链接地址: http://www.djcxy.com/p/32547.html
