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

nginx 强制 https 浏览器总是提醒重定向过多不能正常访问网页

  •  
  •   ITOutsider · 2016-04-28 14:42:06 +08:00 · 22795 次点击
    这是一个创建于 2919 天前的主题,其中的信息可能已经有所发展或是发生改变。
    rewrite ^(.*)$ https://$host$1 permanent;

    return 301 https://$server_name$request_uri;

    都试过了
    17 条回复    2017-04-12 18:15:53 +08:00
    cxbig
        1
    cxbig  
       2016-04-28 15:00:29 +08:00   ❤️ 2
    加个条件试试
    if ($scheme = http) {
    return 301 https://$server_name$request_uri;
    }
    stabc
        2
    stabc  
       2016-04-28 15:09:34 +08:00   ❤️ 1
    提这种问题记得要贴全配置,免得让想帮你的人瞎猜。
    lslqtz
        3
    lslqtz  
       2016-04-28 15:12:41 +08:00   ❤️ 1
    你这是填在 listen 443;内了吧。。
    我是直接用 listen 80; 然后直接 return 301 。
    listen 443 则按正常来,贴一下我的配置文件
    server {
    listen 80;
    index index.php index.htm index.html;
    root /var/www/html;

    return 301 https://www.hardwareunion.com$request_uri;

    location ~ .*\.(php|php5)?$ {
    fastcgi_pass unix:/var/run/phpfpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
    }

    server {
    listen 443 ssl http2;
    index index.php index.htm index.html;
    root /var/www/html;

    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_certificate /etc/nginx/1.crt;
    ssl_certificate_key /etc/nginx/1.key;
    ssl_ct on;
    ssl_ct_static_scts /etc/nginx/sct1;
    location ~ .*\.(php|php5)?$ {
    fastcgi_pass unix:/var/run/phpfpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
    }
    lslqtz
        4
    lslqtz  
       2016-04-28 15:13:45 +08:00   ❤️ 1
    妈蛋。。下面的 server_name 去了,上面 return 的忘去了。。噗
    knightdf
        5
    knightdf  
       2016-04-28 15:45:49 +08:00   ❤️ 1
    循环重定向了吧。。
    usernametoolong
        6
    usernametoolong  
       2016-04-28 15:46:15 +08:00   ❤️ 1
    一楼已给答案
    xustrive
        7
    xustrive  
       2016-04-28 16:10:15 +08:00   ❤️ 1
    留个记号。 下次好找~~~ 一脸闷 B 看不懂 逃····
    ceoimon
        8
    ceoimon  
       2016-04-28 16:11:40 +08:00   ❤️ 1
    为什么不用 HSTS
    lHUAC
        9
    lHUAC  
       2016-04-28 16:59:37 +08:00   ❤️ 2
    @ITOutsider

    这或者是正确代码( 100%解决~),参考

    if ($server_port = 80){
    return 301 https://$server_name$request_uri;}
    if ($scheme = http){
    return 301 https://$server_name$request_uri;}
    error_page 497 https://$server_name$request_uri;
    ldsink
        10
    ldsink  
       2016-04-29 09:13:04 +08:00   ❤️ 2
    推荐一个网站
    https://mozilla.github.io/server-side-tls/ssl-config-generator/
    自动生成正确的 Nginx SSL 配置,默认开启了 HSTS 。
    你用这个配置按需求减就好。
    ITOutsider
        11
    ITOutsider  
    OP
       2016-04-29 14:59:12 +08:00
    非常感谢回复本帖的答主
    @cxbig
    @stabc
    @lslqtz
    @lslqtz
    @knightdf
    @usernametoolong
    @xustrive
    @xustrive
    @ceoimon
    @lHUAC
    kmdd33
        12
    kmdd33  
       2017-04-08 21:54:51 +08:00
    @cxbig 请问你这个 if ($scheme = http) {
    return 301 https://$server_name$request_uri;
    } 可以添加在 nginx.conf 里面的 server {}段里面的任意位置吗?我也遇到了重定向次数过多的问题。
    kmdd33
        13
    kmdd33  
       2017-04-08 22:08:15 +08:00
    @lHUAC 请问你这个 代码
    if ($server_port = 80){
    return 301 https://$server_name$request_uri;}
    if ($scheme = http){
    return 301 https://$server_name$request_uri;}
    error_page 497 https://$server_name$request_uri;
    可以添加在 nginx.conf 里面的 server {}段里面的任意位置吗?
    cxbig
        14
    cxbig  
       2017-04-08 23:50:16 +08:00
    @kmdd33 从你另一个帖子 https://www.v2ex.com/t/353371 看,配置文件很乱。
    通常我们很少直接去改 nginx.conf 文件
    我不知道你用的哪个 Linux 发行版,最好在你那个帖子说明一下。

    拿 Ubuntu Server 来说,不用改主配置文件。
    有个目录用来放你自己的网站定义:/etc/nginx/sites-available
    当你需要把一个网站上线的时候,把该目录的配置文件( vhost )做 Link 到:/etc/nginx/sites-enabled
    再 reload 或 restart nginx 即可,你还可以用 service nginx configtest 来检查有没有语法错误
    这是第一步,建议你弄清楚 nginx 的基本配置结构再说。
    kmdd33
        15
    kmdd33  
       2017-04-09 01:36:42 +08:00 via iPad
    @cxbig centos7 , vhost 里面是空的,没有配置虚拟主机
    lHUAC
        16
    lHUAC  
       2017-04-12 09:53:30 +08:00
    @kmdd33 您好,可以,如果你只是为了将 HTTP 强制跳转到 HTTPS ,或者这样来会更好。
    server{
    listen 80;
    server_name xxx.com;#你的域名
    return 301 https://$server_name$request_uri;
    }
    kmdd33
        17
    kmdd33  
       2017-04-12 18:15:53 +08:00
    @lHUAC 谢谢,问题解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3946 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.