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

求 Nginx 大佬指点一下,关于反代 http 自动跳转 https 的

  •  
  •   GoodRui · 2022-09-25 11:50:47 +08:00 · 2867 次点击
    这是一个创建于 572 天前的主题,其中的信息可能已经有所发展或是发生改变。

    首先感谢🙏下大佬~ 我基本是 Nginx 0 基础小白,就是为了自己做反代用,想用一个域名不同前缀来区分自己搭的各种应用。 配置上了 ssl 证书,现在基本能正常工作,但是有个问题是每次访问网址,都需要输全 https://111.domain.com:8888 搜了很多 http 重定向到 https 的,基本都是 listen 80 然后 return 或 rewrite URL 到 https 的 port 链接。 我想问的是,因为我端口转发只有一个 8888 ,没有根本没有开监听 http 的端口。我应该如何才能正确的只输入 http://111.domain.com:8888就访问https://111.domain.com:8888

    以下是一个示范:

    ### bark-server 反向代理
    
    server {
        listen 8888;
        # Replace bark.app.dev with your real domain name.
        server_name bark.my.com;
    
        return 301 https://$host$request_uri;
    }
    
    server {
        listen 8888 ssl http2;
        server_name bark.my.com;
        location / {
            proxy_pass http://192.168.0.10:55002;
        }
    }    
    

    感谢🙏!!!

    8 条回复    2022-09-26 12:07:18 +08:00
    LinePro
        1
    LinePro  
       2022-09-25 11:55:02 +08:00   ❤️ 2
    error_page 497 =301 https://$host:$server_port$request_uri;
    这一句加到 server 里
    GoodRui
        2
    GoodRui  
    OP
       2022-09-25 12:27:44 +08:00
    @LinePro 感谢大佬~!实测可行,效果拔群~
    Microcharon
        3
    Microcharon  
       2022-09-25 18:13:19 +08:00   ❤️ 2
    ysc3839
        4
    ysc3839  
       2022-09-25 19:16:04 +08:00 via Android   ❤️ 1
    顺带一提,497 是 nginx 自己定义的非标准 http 状态码,当 http 请求发送到 https 端口上会触发这个错误,因此将这个错误设置为 301 跳转即可实现当 http 请求发送到 https 端口时跳转为 https 协议。
    liuyinltemp
        5
    liuyinltemp  
       2022-09-26 09:22:59 +08:00
    @GoodRui 是不是放在第二个 server ?
    server {
    listen 8888 ssl http2;
    server_name bark.my.com;
    error_page 497 =301 https://$host:$server_port$request_uri;
    location / {
    proxy_pass http://192.168.0.10:55002;
    }
    }
    GoodRui
        6
    GoodRui  
    OP
       2022-09-26 12:03:12 +08:00 via iPhone
    @ysc3839 感谢大佬,这下我是懂了,吾了,nginx 到头了~
    GoodRui
        7
    GoodRui  
    OP
       2022-09-26 12:06:37 +08:00 via iPhone
    @liuyinltemp 对,对我提的这种情况,第一个 server 没有用。意思就是只输域名的时候访问 http 的这个端口 nginx 正常是会返回一个错误,当遇到这个错误的时候重定向到 https ,域名和端口还是用之前输的。
    GoodRui
        8
    GoodRui  
    OP
       2022-09-26 12:07:18 +08:00 via iPhone
    @Microcharon 感谢大佬,我们伸手党就喜欢这种东西😍
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5079 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 09:34 · PVG 17:34 · LAX 02:34 · JFK 05:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.