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

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

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

1、编译nginx

./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

bash 复制代码
stream {
  upstream  http_gateway {
    server  127.0.0.1:80801;
  }
  upstream  https_gateway {
    server  127.0.0.1:80802;
  }
  map $ssl_preread_protocol $upstream {
    default http_gateway;
    "TLSv1.0" https_gateway;
    "TLSv1.1" https_gateway;
    "TLSv1.2" https_gateway;
    "TLSv1.3" https_gateway;
  }
  
  server {
    listen 8080;
    ssl_preread on;
    proxy_pass $upstream;
  }
  
  upstream http_gateway_8081 {
    server 127.0.0.1:80811;
  }
  upstream https_gateway_4664 {
    server 127.0.0.1:80812;
  }
  map $ssl_preread_protocol $upstream_8081 {
    default http_gateway_8081;
    "TLSv1.0" https_gateway_8081;
    "TLSv1.1" https_gateway_8081;
    "TLSv1.2" https_gateway_8081;
    "TLSv1.3" https_gateway_8081;
  }

  server {
    listen 8081;
    ssl_preread on;
	proxy_pass $upstream_8081;
  }
}

3、简单的nginx.conf示例供参考

bash 复制代码
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    server {
       access_log logs/demo-info.log;
		listen 80801;
		listen 80802 ssl;
		server_name localhost;
		ssl_certificate      /usr/local/nginx/conf/ssl/server.crt;
		ssl_certificate_key  /usr/local/nginx/conf/ssl/server.key;
		ssl_session_cache    shared:SSL:1m;
		ssl_session_timeout  10m;
		ssl_protocols  TLSv1.2;  
		ssl_prefer_server_ciphers  on;
		ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
		location / {
            proxy_pass http://127.0.0.1:4399;
        }
    }
	server {
		access_log logs/demo-test.log;
		listen 80811;
		listen 80812 ssl;
		server_name localhost;
		ssl_certificate      /usr/local/nginx/conf/ssl/server.crt;
		ssl_certificate_key  /usr/local/nginx/conf/ssl/server.key;
		ssl_session_cache    shared:SSL:1m;
		ssl_session_timeout  10m;
		ssl_protocols  TLSv1.2;  
		ssl_prefer_server_ciphers  on;
		ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

		location / {
            proxy_pass http://127.0.0.1:4340;
        }
	}

}
相关推荐
shamalee44 分钟前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
爱莉希雅&&&1 小时前
haproxy安装以及haproxy+nginx简单案例详解
linux·运维·nginx·haproxy
qiuyuyiyang1 小时前
Nginx 反向代理之upstream模块以及完整配置反向代理示例
git·nginx·github
yangyanping201081 小时前
Vue入门到精通六之一个简单的请求HTTP接口
前端·vue.js·http
w1225h2 小时前
Nginx环境安装
运维·nginx
zephyr052 小时前
TCP/IP协议族详解:应用层协议HTTP
网络协议·tcp/ip·http
qq_283720053 小时前
WebGL基础教程(十三) :玩转矩阵,从 0 到 1 玩转 3D 动画(新手也能秒懂矩阵变换)
运维·nginx
我爱学习好爱好爱3 小时前
ELK日志分析平台(五):Filebeat 部署与 Nginx 日志采集(输出至 Logstash)(基于Rocky Linux 9.6)
linux·nginx·elk
A10169330713 小时前
Nginx与frp结合实现局域网和公网的双重https服务
数据库·nginx·https
无心水3 小时前
【java开发常见错误】5、HTTP调用避坑指南:超时、重试、并发,一个都不能少
java·开发语言·后端·http·架构师·http调用·后端开发错误