nginx http https共用同一个端口
2023-03-08 本文已影响0人
叶迎宪
nginx大于1.15.2版本
stream {
upstream http {
server localhost:8000;
}
upstream https {
server localhost:8001;
}
map $ssl_preread_protocol $upstream {
default https;
"" http;
}
server {
listen 8080;
listen [::]:8080;
proxy_pass $upstream;
ssl_preread on;
}
}
server {
listen 8000;
listen [::]:8000;
listen 8001 ssl;
listen [::]:8001 ssl;
...
关键设置 ssl_preread on。有了这个设置之后,可以使用map指令,通过 $ssl_preread_protocol 判断不同的ssl协议进行分发
参考 https://serverfault.com/questions/47876/handling-http-and-https-requests-using-a-single-port-with-nginx
http://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html