nginx 同一个端口支持http和https配置

原理:使用nginx的stream、 stream_ssl_preread模块

1.编译nginx

由于stream和stream_ssl_preread模块非默认引入,需要在编译安装nginx时引入;编译时添加配置参数 --with-stream --with-stream_ssl_preread_module

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module

执行make & make install

2.配置nginx.conf

添加stream配置,让其识别到http访问时默认走http,其余走https

复制代码
stream {
  upstream http_gateway {
    server  127.0.0.1:8077;
  }
  upstream https_gateway {
    server  127.0.0.1:8076;
  }
  map $ssl_preread_protocol $upstreama{
    default http_gateway;
    "TLSv1.0" https_gateway;
    "TLSv1.1" https_gateway;
    "TLSv1.2" https_gateway;
    "TLSv1.3" https_gateway;
  }

  server {
    listen 2345;
    ssl_preread on;
    proxy_pass $upstreama;
  }
}

http {
******
}

3.配置http和https访问资源

复制代码
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
     upstream gateway_service{
          server  127.0.0.1:8077  weight=1;
          server  127.0.0.1:8076  weight=2;
     }

    server {
        listen       8077;
        listen       8076 ssl;
        server_name  192.168.19.1;
        ssl_certificate /root/Public/ssl/cert.pem;
    ssl_certificate_key /root/Public/ssl/key.pem;
    ssl_prefer_server_ciphers  on;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;
    client_max_body_size 100M;
        #ssl_certificate      xxx.pem;
       # ssl_certificate_key  xxx.key;
        location / {
            proxy_pass http://gateway_service;
        }
    }

}

重启ng,即可同时通过http和https访问了。

相关推荐
2501_9151063225 分钟前
iOS 多技术栈混淆实现,跨平台 App 混淆拆解与组合
android·ios·小程序·https·uni-app·iphone·webview
HwJack201 小时前
HarmonyOS HTTP请求:从“能跑就行”到“优雅可靠”的进化之路
http·华为·harmonyos
00后程序员张1 小时前
有些卡顿不是 CPU 的问题,还要排查磁盘 I/O
android·ios·小程序·https·uni-app·iphone·webview
AnalogElectronic2 小时前
对https一系列问题的疑问与解答
网络协议·http·https
冉佳驹2 小时前
Linux ——— 网络开发核心知识与协议实现详解
linux·http·https·udp·json·tcp·端口号
末日汐2 小时前
应用层协议HTTP
网络·网络协议·http
wuyikeer2 小时前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
LSL666_2 小时前
nginx——方向代理和负载均衡
运维·nginx·负载均衡·反向代理
语戚3 小时前
Nginx vs Ribbon:负载均衡的两种核心范式(反向代理 vs 客户端负载)
java·nginx·spring·spring cloud·面试·ribbon·负载均衡
悟空宇4 小时前
关于minio桶文件备份到本地或NAS等其他设备的操作说明(http+https)
网络协议·http·https