nginx 取消 https 后发现 chrome 访问提示重定向过多无法访问。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
smallaccount
V2EX    NGINX

nginx 取消 https 后发现 chrome 访问提示重定向过多无法访问。

  •  
  •   smallaccount 2016-12-14 19:29:15 +08:00 12727 次点击
    这是一个创建于 3229 天前的主题,其中的信息可能已经有所发展或是发生改变。
    取消了https rewrite 到了http,但是发现 chrome 访问提示重定向过多无法访问。
    自己可以清楚 cookie 解决,但是用户怎么办啊。
    或者是我哪里设置有问题?
    比如说,如何告诉浏览器我修改了nginx的配置文件?
    第 1 条附言    2016-12-14 21:58:48 +08:00
    nginx 配置

    server {
    listen 80;
    server_name domain.com www.domain.com;
    if ($host != 'www.domain.com')
    {
    rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
    }
    location / {
    proxy_pass http://127.0.0.1:8000/;
    limit_req zOne=one burst=20 nodelay;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    }
    location /static {
    alias /var/www/domain/static;
    }
    }
    server {
    listen 443 ssl;
    server_name domain.com www.domain.com;

    rewrite ^/(.*)$ http://www.domain.com/$1 permanent;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
    }
    第 2 条附言    2016-12-16 10:06:04 +08:00
    @allenhu 31 楼大神指明了原因。。。具体看 34 楼。
    36 条回复    2016-12-16 12:15:29 +08:00
    lslqtz
        1
    lslqtz  
       2016-12-14 20:03:31 +08:00   1
    这就是你配置有问题。
    还有,有任何问题贴配置是常识了,大量答案都提到了。
    stabc
        2
    stabc  
       2016-12-14 20:19:23 +08:00   1
    换个浏览器正常么?
    raysonx
        3
    raysonx  
       2016-12-14 20:23:24 +08:00   1
    你这是设置了从 HTTPS 跳转到 HTTP 了吧?
    目测浏览器缓存了 HTTP 到 HTTPS 的跳转( 301 跳转会缓存),然后你再设置从 HTTPS 跳到 HTTP ,不断循环
    smallaccont
        4
    smallaccount  
    OP
       2016-12-14 20:27:19 +08:00
    @raysonx 是这样的,如果清除 cookie ,缓存消失就正常了。但是如何无痛换到 http 呢?用户不会清楚 cookie 。
    @stabc 清除 cookie 后正常
    @lslqtz 最讨厌的就是『大量答案都提到了』,然后还不给解决办法的回答。
    lhbc
        5
    lhbc  
       2016-12-14 20:36:37 +08:00 via iPhone   1
    https 里面不要直接跳 /,改为跳 http://www.example.com/?from_https 这样
    后面加个尾巴
    lslqtz
        7
    lslqtz  
       2016-12-14 20:38:16 +08:00   1
    @smallaccount 最讨厌的就是『大量答案都提到了』
    配置都不贴,都靠猜着给你回答?
    lslqtz
        8
    lslqtz  
       2016-12-14 20:39:53 +08:00   1
    @raysonx 是的, 301 跳转缓存时间很长。
    一般来说,我在 301 时都会提供 no-cache 的头。
    @lhbc 我习惯加时间戳 :v
    raysonx
        9
    raysonx  
       2016-12-14 20:42:37 +08:00   1
    题外话,话说我自己的域名已经设置了 HSTS ,而且已经内置在了 Chrome 的静态 HSTS 名单里,永远不可能改回 HTTP 了。
    lslqtz
        10
    lslqtz  
       2016-12-14 20:44:55 +08:00
    对于一键不确定的事情,请用 302 。
    @raysonx 我有可能域名会过期,不给后来人添麻烦就没加 HSTS :a
    tomczhen
        11
    tomczhen  
       2016-12-14 20:48:31 +08:00 via Android
    @lslqtz 小心人家说你小气,说你不想回答就别回答。:doge:
    mkeith
        12
    mkeith  
       2016-12-14 21:02:17 +08:00
    1L 的意思 『大量答案都提到了』问问题要贴配置
    stabc
        13
    stabc  
       2016-12-14 21:13:32 +08:00
    @lslqtz 感觉 @lhbc 说的方法不错,你说“我习惯加时间戳”,具体是怎样加的?提示跳转过多时浏览器地址栏上是什么?
    lslqtz
        14
    lslqtz  
       2016-12-14 21:21:01 +08:00
    @stabc ?时间戳
    或者?版本号都行 :D
    anheiyouxia
        15
    anheiyouxia  
       2016-12-14 21:24:45 +08:00
    @smallaccount 连配置都不贴,还怪别人说不?
    stabc
        16
    stabc  
       2016-12-14 21:25:45 +08:00
    @lslqtz 那你提示跳转过多时浏览器地址栏上是什么?(可以把域名隐掉)
    smallaccount
        17
    smallaccount  
    OP
       2016-12-14 21:55:16 +08:00 via iPhone
    @lslqtz 不好意思…稍等。
    smallaccount
        18
    smallaccount  
    OP
       2016-12-14 22:00:28 +08:00
    flynaj
        19
    flynaj  
       2016-12-14 22:06:45 +08:00 via Android   1
    curl 看一下
    smallaccount
        20
    smallaccount  
    OP
       2016-12-14 22:14:52 +08:00
    @flynaj curl 结果是:
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.4.6 (Ubuntu)</center>
    </body>
    </html>
    loveyu
        21
    loveyu  
       2016-12-14 22:33:32 +08:00   1
    lslqtz
        22
    lslqtz  
       2016-12-14 22:35:58 +08:00 via iPhone
    lslqtz
        23
    lslqtz  
       2016-12-14 22:37:43 +08:00 via iPhone
    顺便向各位道个歉:我说配置有问题可能太果断,但以后可以在跳转的域名中加入以下指令:
    add_header Cache-Control no-cache
    smallaccount
        24
    smallaccount  
    OP
       2016-12-14 22:42:21 +08:00
    @loveyu http 的话直接显示网页内容了, https 的话和上面的那个一样。。。
    loveyu
        25
    loveyu  
       2016-12-14 22:54:43 +08:00
    @smallaccount 我指这类数据
    curl -I http://www.v2ex.com
    HTTP/1.1 302 Moved Temporarily
    Server: nginx/1.11.6
    Date: Wed, 14 Dec 2016 14:54:04 GMT
    Content-Type: text/html
    Content-Length: 161
    Connection: keep-alive
    Location:
    X-ORCA-Accelerator: from 093.chn.fuo01.cn.krill.c3edge.net
    smallaccount
        26
    smallaccount  
    OP
       2016-12-15 10:09:11 +08:00
    @flynaj

    @loveyu 纯小白,刚才写成 curl -l 了,应该是 curl -I.

    http
    HTTP/1.1 200 OK
    Server: nginx/1.4.6 (Ubuntu)
    Date: Wed, 14 Dec 2016 14:46:53 GMT
    Content-Type: text/html; charset=
    Content-Length: 7996
    Connection: keep-alive
    Cache-Control: no-cache

    https
    HTTP/1.1 302 Moved Temporarily
    Server: nginx/1.4.6 (Ubuntu)
    Date: Wed, 14 Dec 2016 14:48:01 GMT
    Content-Type: text/html
    Content-Length: 169
    Connection: keep-alive
    Location: http://www.domain.com/

    重新设置为 302 了,目前依旧无法正常访问。

    @lslqtz 方法没用,加了,加在 proxy_set_header X-Real-IP $remote_addr;下面了,还不行。。。

    目前设置:
    server {
    listen 80;
    server_name domain.com www.domain.com;
    if ($host != 'www.domain.com')
    {
    rewrite ^/(.*)$ http://www.domain.com/$1 redirect;
    }
    proxy_cache_valid 301 1m;
    location / {
    proxy_pass http://127.0.0.1:8000/;
    limit_req zOne=one burst=20 nodelay;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    add_header Cache-Control no-cache;
    }
    location /static {
    alias /var/www/domain/static;
    }
    }
    server {
    listen 443 ssl;
    server_name domain.com www.domain.com;
    proxy_cache_valid 301 1m;

    rewrite ^/(.*)$ http://www.domain.com/$1 redirect;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
    }
    smallaccount
        27
    smallaccount  
    OP
       2016-12-15 10:16:37 +08:00
    @raysonx 我觉得你说的最靠谱,主要是 chrome 缓存了 301 的问题,但是怎么清楚客户端 chrome 的 301 缓存呢?
    figofuture
        28
    figofuture  
       2016-12-15 10:36:55 +08:00   1
    这篇帖子有回答 t/314217
    lslqtz
        29
    lslqtz  
       2016-12-15 11:38:32 +08:00
    @smallaccount 我觉得没什么办法。
    因为 https 到 http 缓存了, http 到 https 又缓存了。
    给你的配置是避免以后出问题的。。
    smallaccount
        30
    smallaccount  
    OP
       2016-12-15 18:23:55 +08:00
    @figofuture 问题和我一样,很多人都答非所为,目测没有题主解决。
    allenhu
        31
    allenhu  
       2016-12-16 07:39:43 +08:00 via Android   2
    hsts 设置了 Strict-Transport-Security: max-age 吧
    lslqtz
        32
    lslqtz  
       2016-12-16 09:35:00 +08:00
    @allenhu 我觉得这样还是没法解决 https 跳转到 http
    lslqtz
        33
    lslqtz  
       2016-12-16 09:35:36 +08:00
    @smallaccount 我建议通知客户手动清除,下次加 no-cache 。。
    smallaccount
        34
    smallaccount  
    OP
       2016-12-16 10:00:32 +08:00
    @allenhu 非非非常感谢!!!
    纯小白什么也不懂,我是按照 digitalocean 上给的教程配置的
    https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04

    然后这个教程设置了 Strict-Transport-Security: max-age ,所以导致了上述的情况, http 直接无法访问,全部都跳转到 https 了,取消设置之后 http 和 https 都可以访问了。 digitalocean 设置的是 15768000 , 182.5 天。。。好吧,只能等用户自己清除了,我这边先把 https 加上去好了,等到半年后再把 https 撤下来,估计差不多。。


    @lslqtz chrome 客户端缓存可能是无法解决了,但是终于知道原因了。
    lslqtz
        35
    lslqtz  
       2016-12-16 11:51:38 +08:00   1
    @smallaccount chrome://net-internals/#hsts
    这里可以清理,只要没有加入列表。
    我以为你是普通的 301 。。没想到你开了 HSTS
    smallaccount
        36
    smallaccount  
    OP
       2016-12-16 12:15:29 +08:00
    @lslqtz 的确应该把配置 https 和取消 https 之后的两个配置文件都贴上去的。。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2615 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 05:16 PVG 13:16 LAX 22:16 JFK 01:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86