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访问了。

相关推荐
_Evan_Yao21 分钟前
端口80之外:一个Java小白和HTTP、DNS、FTP、SSH的“隐秘”交手
网络协议·http·ssh
高木木的博客7 小时前
数字架构智能化测试平台(1)--总纲
人工智能·python·nginx·架构
徐子元竟然被占了!!8 小时前
Nginx
运维·nginx
图图玩ai9 小时前
SSH 命令管理工具怎么选?从命令收藏到批量执行一次讲清
linux·nginx·docker·ai·程序员·ssh·可视化·gmssh·批量命令执行
CXH72810 小时前
nginx——https
运维·nginx·https
Lentou11 小时前
nginx反向代理
运维·nginx
遇见火星12 小时前
linux设置开启启动服务
linux·运维·服务器·nginx
悟道子HD12 小时前
计算机网络端口记忆指南
计算机网络·http·https·ssh·ftp·端口号·smtp
咸鱼翻身小阿橙13 小时前
QT P4
数据库·qt·nginx
上海合宙LuatOS14 小时前
LuatOS扩展库API——【httpplus】HTTP客户端
网络·物联网·网络协议·http·lua·luatos