首页 文章

Wordpress常量重定向与nginx上游

提问于
浏览
1

出现了一种情况,即运行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

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

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

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

    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_paramHTTPS 参数传递给WordPress(在服务器2上):

    location ~ \.php$ {
        ...
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS $https_flag;
        ...
    }
    

相关问题