V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
ErnieSauce
V2EX  ›  NGINX

NGINX 如何强制 https 301 跳转到 https

  •  
  •   ErnieSauce · 2016-10-20 18:18:13 +08:00 · 8105 次点击
    这是一个创建于 2717 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用 https 后网站上转载的优酷视频显示空白
    所以想取消掉 https
    但是百度已经收录了 https
    现在想把 https 强制跳转到 http
    百度了很多方法都重定向循环了
    19 条回复    2016-10-22 00:00:42 +08:00
    qgy18
        1
    qgy18  
       2016-10-20 18:31:29 +08:00 via iPhone
    你之前用 301 永久重定向把 http 跳往 https 了吧。
    浏览器会记住 301 跳转,所以你再从 https 跳到 http 就循环了。
    试试在 https 跳往 http 时加一个参数。
    ErnieSauce
        2
    ErnieSauce  
    OP
       2016-10-20 19:09:30 +08:00
    @qgy18 浏览器记住的跳转清楚 cookie 后还会存在吗?我需要怎么修改
    ansheng
        3
    ansheng  
       2016-10-20 19:13:21 +08:00
    晒下我的配置文件把,希望对你有帮助:
    ```nginx
    server {
    server_name blog.ansheng.me;
    listen 80;
    listen 443 ssl;

    set $rewriterule https;

    if ($scheme = https) {
    set $rewriterule "${rewriterule}7";
    }

    if ($host ~* ^blog.ansheng.me) {
    set $rewriterule "${rewriterule}8";
    }
    if ($rewriterule != "https78") {
    return 301 https://blog.ansheng.me$request_uri;
    break;
    }

    ssl_certificate /.../xxx.crt;
    ssl_certificate_key /.../xxx.key;
    ssl_trusted_certificate /.../xxx.crt;

    ......
    }
    ```
    xiujie_JIANG
        4
    xiujie_JIANG  
       2016-10-20 19:24:59 +08:00
    rewrite ^ https://$server_name$request_uri? permanent;
    JJaicmkmy
        5
    JJaicmkmy  
       2016-10-20 20:08:56 +08:00 via iPhone
    首先,你标题写错了。
    第二,优酷是可以用 HTTPS 引用的。你可以用 https 打开优酷,然后把播放器的地址复制下来。
    rainfox
        6
    rainfox  
       2016-10-20 20:32:42 +08:00
    @JJaicmkmy , HTTPS 打不开优酷的吧!但是优酷确实是可以 https 引用的。地址: http://cloud.youku.com/tools/
    ErnieSauce
        7
    ErnieSauce  
    OP
       2016-10-20 22:22:19 +08:00
    @JJaicmkmy https 也不支持手机端播放 所以不纠结这个问题 我想解决跳转的问题 大神会吗 *- -
    ErnieSauce
        8
    ErnieSauce  
    OP
       2016-10-20 22:23:02 +08:00
    @ansheng 你这个是从 http 跳转到 https 吧? -
    rainfox
        9
    rainfox  
       2016-10-20 22:26:10 +08:00
    @ErnieSauce 然而,优酷的 https 那个其实是支持手机播放的,亲测。
    MaiCong
        10
    MaiCong  
       2016-10-20 22:34:54 +08:00
    if ($ssl_protocol = '') {
       return 301 https://$server_name$request_uri;
    }
    error_page 497 https://$server_name$request_uri;
    ErnieSauce
        11
    ErnieSauce  
    OP
       2016-10-20 22:40:37 +08:00
    @rainfox 确实可以 你有用过 wordress 的视频插件吗
    xiaoz
        12
    xiaoz  
       2016-10-20 22:52:49 +08:00 via iPhone
    ansheng
        13
    ansheng  
       2016-10-20 23:12:59 +08:00
    @ErnieSauce 是的……
    happywowwow
        14
    happywowwow  
       2016-10-20 23:17:31 +08:00
    if ($server_port = "80") {
    return 301 https://$host$request_uri;
    }

    最近写的. 因为没写 server_name 所以用了$host
    jqw1992
        15
    jqw1992  
       2016-10-21 00:51:42 +08:00
    server {
    listen 80;
    server_name XXXX.com www.XXXX.com;
    rewrite ^/(.*) https://www.laba.me/$1 permanent;
    }
    msg7086
        16
    msg7086  
       2016-10-21 00:58:28 +08:00
    @happywowwow
    @jqw1992
    @MaiCong
    @xiujie_JIANG
    看题要审题……?

    @ErnieSauce
    一定要重写成 http 的话,随便加一个无用参数应该就可以了。
    清 Cookie 是不会清访问缓存的。
    ErnieSauce
        17
    ErnieSauce  
    OP
       2016-10-21 08:20:55 +08:00
    @msg7086 终于看到了一个明白人 - - 随便加一个参数是指?
    我在使用 if ($server_port = "80") {
    return 301 https://$host$request_uri;
    }
    这个的时候 只有首页会自动跳转到 https 请问该如何设置
    w99wen
        18
    w99wen  
       2016-10-21 10:38:05 +08:00
    server {
    listen *:80;
    server_name git.w99wen.tk;
    server_tokens off; ## Don't show the nginx version number, a security best practice
    return 301 https://git.w99wen.tk:443$request_uri;
    access_log /var/log/gitlab/nginx/gitlab_access.log gitlab_access;
    error_log /var/log/gitlab/nginx/gitlab_error.log;
    }
    msg7086
        19
    msg7086  
       2016-10-22 00:00:42 +08:00
    @ErnieSauce 一来别用 if ,二来调试的时候先用 302 做,没问题了再上 301 。
    做 https 跳转到 http ,试试
    rewrite ^(.*)$ http://example.com$1?http;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2570 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:39 · PVG 23:39 · LAX 08:39 · JFK 11:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.