更改为HTTPS后重定向太多

时间:2018-01-10 23:28:58

标签: php wordpress ssl nginx

我们收到有关"重定向您次数太多的错误。"将Wordpress从HTTP更改为HTTPS后。 WP使用Nginx在AWS上,我们尝试在数据库和wp-config文件中同时更改define(home)和define(siteurl),但没有帮助。

下面是当前的nginx配置文件,我们也尝试在Google上关注一些帖子,将HTTP更改为HTTPS,将端口更改为443,但仍然没有运气。

提前致谢。

server {
    listen 443;
    server_name wp.mywebsite.com;

    server_name www.mywp.mywebsite.com;


    include /etc/nginx/common_server_settings;

    location /wp-content/ { root /var/www/mywp.mywebsite.com; }
    location /wp-includes/ { root /var/www/mywp.mywebsite.com; }

    set $no_cache 0;
    # POST requests should always go to PHP
    if ($request_method = POST) { set $no_cache 1; }
    # Don't cache uris containing the following segments
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $no_cache 1;
    }
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $no_cache 1; }

    location / {
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        #proxy_set_header Host "mywp.mywebsite.com";
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-Proto http;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_hide_header X-Powered-By;
    proxy_pass http://127.0.0.1:1080;

        proxy_cache_bypass $no_cache;
        proxy_no_cache $no_cache;
        proxy_cache_lock on;
        proxy_cache BLOG;
        proxy_cache_valid 5m;

        add_header X-Raw $no_cache;
    }
}

2 个答案:

答案 0 :(得分:0)

我使用这种类型的配置来代理https:

server {
    listen          443 ssl;
    server_name     wp.mywebsite.com;
    server_tokens off;
    ssl on;
    ssl_certificate     /etc/nginx/ssl/cert.crt;
    ssl_certificate_key /etc/nginx/ssl/cert.key;
    ssl_protocols        SSLv3 TLSv1.2;
    ssl_ciphers          HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    set $ssl off;
    etag on;
    if ($scheme = https) {
        set $ssl on;
    }

    location / {
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_set_header        X-Forwarded-Ssl $ssl;

        proxy_pass          http://127.0.0.1:1080;
        proxy_read_timeout  90;
        proxy_redirect      http://127.0.0.1:1080 wp.mywebsite.com;
    }
}

另外不要忘记强制使用https:

server {
    server_tokens off;
    listen          80;
    server_name     wp.mywebsite.com;
    return          301 https://wp.mywebsite.com$request_uri;
}

答案 1 :(得分:0)

proxy_set_header X-Forwarded-Proto http;

标题导致此重定向循环,因为wordpress仍然认为正在使用" http" scheme(因为您的反向代理服务器没有在标头中发送正确的方案。

您可能还需要在wordpress配置文件中进行更改,以便正确检测HTTPS。正如described here

  

支持HTTP_X_FORWARDED_PROTO的负载平衡器或反向代理背后的网站可以通过将以下代码添加到require_once调用上方的wp-config.php文件来修复:

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
    $_SERVER['HTTPS'] = 'on';