nginx 怎么配置一个端口支持 http 和 https
首先,需要在 nginx.conf 文件中添加一个新的 server 块,同时监听 http 和 https 端口。示例配置如下:
server {
listen 80;
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
# 配置 http 和 https 的处理逻辑
}
}
其中,listen 指令同时监听 http 和 https 端口,server_name 指令指定服务器名字,ssl_certificate 和 ssl_certificate_key 指令配置 SSL 证书和私钥。
在 location 块中,可以配置 http 和 https 的处理逻辑。例如,可以使用 if ($scheme = http) 判断协议类型,然后根据需要进行跳转或者其他处理。示例配置如下:
location / {
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
# 处理 https 请求
}
``
原文地址: https://www.cveoy.top/t/topic/czS5 著作权归作者所有。请勿转载和采集!