V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kenvix
V2EX  ›  问与答

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

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

    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
        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   ·   我们的愿景   ·   实用小工具   ·   5298 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:53 · PVG 15:53 · LAX 00:53 · JFK 03:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.