nginx 怎么自动跳转到 https 而不允许 http 访问 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kenvix
V2EX    问与答

nginx 怎么自动跳转到 https 而不允许 http 访问

  •  
  •   kenvix 2014-12-30 20:50:54 +08:00 14458 次点击
    这是一个创建于 3945 天前的主题,其中的信息可能已经有所发展或是发生改变。

    http访问还没来得及设置就直接400

    liuhaotian
        1
    liuhaotian  
       2014-12-30 20:54:42 +08:00   12
    listen 443 ssl;
    if ($scheme!=https){rewrite xxx;}
    palytoxin
        2
    palytoxin  
       2014-12-30 21:04:42 +08:00
    server {
    listen 80;
    server_name example.us;
    rewrite ^/(.*) https://$server_name/$1 permanent;
    }
    kenvix
        3
    kenvix  
    OP
       2014-12-30 21:04:57 +08:00   1
    @liuhaotian 不行。。还是400
    liuhaotian
        4
    liuhaotian  
       2014-12-30 21:05:31 +08:00   12
    @kenvix 难道你这个不是80端口?
    kenvix
        5
    kenvix  
    OP
       2014-12-30 21:05:52 +08:00
    @liuhaotian 自定义的web端口 你懂的
    kenvix
        6
    kenvix  
    OP
       2014-12-30 21:06:10 +08:00
    @palytoxin 同。自定义web端口 不行
    palytoxin/td>
        7
    palytoxin  
       2014-12-30 21:10:33 +08:00
    @kenvix 用了好长时间了,看看是不是应用服务器有问题
    liuhaotian
        8
    liuhaotian  
       2014-12-30 21:22:23 +08:00 via iPhone   11
    @palytoxin 我觉得他的意思是非80端口要开https不能够这么跳转
    kenvix
        9
    kenvix  
    OP
       2014-12-30 21:32:57 +08:00   2
    @palytoxin 就是@liuhaotian 说的那样
    Starduster
        10
    Starduster  
       2014-12-30 21:33:32 +08:00
    所以你没说清楚你改了什么地方啊。。。改了端口贴配置啊
    kenvix
        11
    kenvix  
    OP
       2014-12-30 21:35:43 +08:00
    @Starduster 我就是一个listen xxxx;这个端口开启ssl 要求http请求自动跳转...
    server {
    listen 1234 ssl;
    if ($scheme!=https){rewrite...}
    }
    Starduster
        12
    Starduster  
       2014-12-30 21:41:29 +08:00
    @kenvix server {
    listen 80;
    server_name XXX;
    rewrite ^(.*) https://$server_name:1234$1 permanent;
    }


    # HTTPS server
    #
    server {
    listen 1234 ssl spdy;
    server_name XXX;

    实测可行,至于 SPDY 是我原来就开了的
    Keyes
        13
    Keyes  
       2014-12-30 21:47:23 +08:00   1
    那,真相是:

    用error 497做跳

    首先
    listen 80;
    listen 443;

    然後配置497面
    error_page 497 https://$host$request_uri;

    最後使http路的求到https(加入到主定行的最後位置):
    if ($server_port = 80) {
    return 497;
    }
    Keyes
        14
    Keyes  
       2014-12-30 21:52:00 +08:00   1
    WIKIPEDIA於497的解(link: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes):

    497 HTTP to HTTPS (Nginx)
    Nginx internal code used for the plain HTTP requests that are sent to HTTPS port to distinguish it from 4XX in a log and an error page redirection.
    superw
        15
    superw  
       2014-12-31 00:00:00 +08:00
    你是无名智者吧
    BinbinWang
        16
    BinbinWang  
       2014-12-31 00:13:46 +08:00
    1. listen 1234;
    server_name xxx.xxx;
    ssl on;

    2. listen 80;
    server_name xxx.xxx;
    rewrite 1234

    done
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2535 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 06:29 PVG 14:29 LAX 23:29 JFK 02:29
    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