Wordpress常量重定向与nginx上游

时间:2016-09-05 23:47:18

标签: php wordpress redirect 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 siteurlhome都设置为新地址。

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/启动无限重定向(到同一个网址)。我也在wp-config.php中定义了它:

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

1 个答案:

答案 0 :(得分:0)

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

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

我注意到你在服务器1上设置了适当的标题:

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