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

nginx 配置 http 自动跳转到 https 问题

  •  
  •   chfight · 2019-09-11 13:03:08 +08:00 · 9666 次点击
    这是一个创建于 1660 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在 https 使用的是 8443 端口,想要实现访问 8443 端口的 http->https 自动跳转

    例如访问 http://aaa.com:8443 -> https://aaa.com:8443

    请问可以做到这样吗?

    34 条回复    2019-11-01 14:36:13 +08:00
    gstqc
        1
    gstqc  
       2019-09-11 13:08:54 +08:00 via Android
    不能
    ysc3839
        2
    ysc3839  
       2019-09-11 13:14:57 +08:00 via Android   ❤️ 1
    可以,搜索 nginx http 497。
    wnpllrzodiac
        3
    wnpllrzodiac  
       2019-09-11 13:16:38 +08:00 via Android
    一个端口还能搞两个服务?
    loading
        4
    loading  
       2019-09-11 13:18:30 +08:00 via Android
    我搜的关键字 https 非 80


    现在 v 站越来越难发回复了,你自己搜吧
    idclight
        5
    idclight  
       2019-09-11 13:24:22 +08:00
    rewire 写法:
    ```
    rewrite ^(.*)$ https://$host$1 permanent;
    ```
    return 写法:
    ```
    return 301 https://$server_name$request_uri;
    ```
    idclight
        6
    idclight  
       2019-09-11 13:27:08 +08:00
    没仔细审题(溜了
    littlespider89
        7
    littlespider89  
       2019-09-11 13:41:08 +08:00
    c2VydmVyIHsKICAgIGxpc3RlbiA4NDQzIHNzbDsKICAgIHNlcnZlcl9uYW1lIGFhYS5jb207CiAgICAKICAgICMgeW91ciBzZXJ2ZXIgY29uZmlnCgogICAgZXJyb3JfcGFnZSA0OTcgaHR0cHM6Ly9hYWEuY29tOjg0NDM7Cn0K
    tulongtou
        8
    tulongtou  
       2019-09-11 13:45:46 +08:00
    可以 error_page 497 https://$host:$server_port$request_uri;
    kaikai5601
        9
    kaikai5601  
       2019-09-11 14:03:51 +08:00
    server 段加上
    if ($server_port !~ 443){
    rewrite ^(/.*)$ https://aaa.com:8443$1 permanent;
    }
    markgor
        10
    markgor  
       2019-09-11 14:07:11 +08:00
    楼上的大哥,请问同域名同端口 nginx 是怎样区分服务?
    markgor
        11
    markgor  
       2019-09-11 14:07:54 +08:00
    题主的要求是
    http://aaa.com:8443 -> https://aaa.com:8443
    !(注意看端口号)
    qsnow6
        12
    qsnow6  
       2019-09-11 14:09:43 +08:00
    自己在代码上 listen 吧
    chfight
        13
    chfight  
    OP
       2019-09-11 14:25:37 +08:00
    mringg
        14
    mringg  
       2019-09-11 14:27:47 +08:00
    个人感觉这个问题比较难
    xfriday
        15
    xfriday  
       2019-09-11 14:43:09 +08:00
    楼上的一些同学可能没注意楼主的端口是同一个,一般的 http 跳 https 是因为端口 80 => 443 才能用,除非能同一个端口实现 2 种协议才行,否则 tls 握手都不行
    MrUser
        16
    MrUser  
       2019-09-11 14:50:26 +08:00
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    直接在 HTML 里加这个试试?
    tulongtou
        17
    tulongtou  
       2019-09-11 14:57:01 +08:00
    楼上一堆不看文档还乱讲话的,具体文档在这里: http://nginx.org/en/docs/http/ngx_http_ssl_module.html
    salmon5
        18
    salmon5  
       2019-09-11 14:57:18 +08:00
    同端口同域名同主机,技术上应该不行,实际也没必要,直接 http 请求就 400 了,老老实实的用 https
    nicoljiang
        19
    nicoljiang  
       2019-09-11 15:13:08 +08:00
    if ($scheme !~ 'https'){
    rewrite ^(/.*)$ https://$host$1 permanent;
    }

    这样?
    OldCarMan
        20
    OldCarMan  
       2019-09-11 15:43:39 +08:00
    8 楼的方式,我看行。
    weiruanniubi
        21
    weiruanniubi  
       2019-09-11 15:47:57 +08:00
    Nginx 可以一个端口同时提供 HTTP 和 HTTPS 服务!!??
    8848
        22
    8848  
       2019-09-11 15:52:54 +08:00
    server {
    listen 80;
    listen 443 ssl;
    server_name a.com;
    rewrite ^ $scheme://www.a/$request_uri permanent;
    }
    foxni
        23
    foxni  
       2019-09-11 16:01:01 +08:00
    有点蒙,一个端口上同时监听 http 和 https?
    zjsxwc
        24
    zjsxwc  
       2019-09-11 16:05:30 +08:00
    https 不是和 443 绑定的吗,怎么还能强制 https 与特定非 443 端口绑定?
    akira
        25
    akira  
       2019-09-11 16:12:28 +08:00
    没有什么需求是加一个中间层解决不了的。

    http 和 https 的协议是不一样的, http 上来就是发 get/post 请求,https 的话 应该是 ssl 的握手封包。 做个中间层,判断下上来的前几个字符,是 http 就做个 https 跳转,其他的全部放行。
    trafficMGR
        26
    trafficMGR  
       2019-09-11 16:16:32 +08:00
    楼主的问题是:针对 HTTPS 协议端口,客户端使用了错误的协议时该如何响应。这时候就需要把客户端引导到 HTTPS 协议上。

    解决办法详见 8 楼和 17 楼。
    xfriday
        27
    xfriday  
       2019-09-11 16:28:57 +08:00
    @zjsxwc 当然可以,协议是协议,端口是端口,80、443 只是 http 和 https 的默认端口而已
    lllllliu
        28
    lllllliu  
       2019-09-11 16:52:57 +08:00
    if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
    workspace
        29
    workspace  
       2019-09-11 17:12:32 +08:00
    无解
    Michaelssss
        30
    Michaelssss  
       2019-09-11 17:20:43 +08:00
    同一个端口搞两个?写 lua 呗,反正写规则肯定是不行。。。
    colorcat
        31
    colorcat  
       2019-09-12 10:29:34 +08:00
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;preload" always;
    Oysmart
        32
    Oysmart  
       2019-09-26 23:02:59 +08:00
    if ($scheme = "http") {

    rewrite ^/(.*)$ https://$server_name:$server_port/$1 permanent;

    }
    Anonym0u5
        33
    Anonym0u5  
       2019-11-01 13:22:56 +08:00
    同样遇到这个问题,443 和 80 不能用,ssl 监听了其它端口,后端 Java 域名+端口没问题。前端服务域名+端口访问报 400 Bad request。页面进不去
    Anonym0u5
        34
    Anonym0u5  
       2019-11-01 14:36:13 +08:00
    我是 nginx 分别反向代理到前后端
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   998 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 20:04 · PVG 04:04 · LAX 13:04 · JFK 16:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.