我没有主域的证书,所以我为我的几个开发域使用了自签名证书。
我是这样使用的:
server {
listen 443;
server_name secure.somehost.com;
ssl on;
ssl_certificate /etc/nginx/ssl/secure.crt;
ssl_certificate_key /etc/nginx/ssl/secure.key;
}
server {
listen 80;
server_name secure.somehost.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
现在我所有的 https 请求(例如 https://anyhost.somehost.com/
)都以某种方式重写为 https://secure.somehost.com
。所以我想禁用此行为并关闭默认服务器的 HTTPS,理想情况下将重写发送到简单的 HTTP(或简单地发送 500 错误)。
我试过这段代码。但是由于我没有证书,我想我无法发送正确的响应。
server {
listen 443 default;
rewrite ^(.*) http://$host$1 permanent;
}
我必须使用默认
服务器,因为我还要处理很多子域和外部域。
最佳答案
如果您想将 https
请求(在端口 443 上)重定向到 http
(在端口 80 上),您应该在服务器中包含 SSL 证书和 key 指令 block ,像这样:
# redirect all default http requests to http://example.com
server {
listen 80 default_server;
server_name _;
access_log off;
return 301 $scheme://example.com$request_uri;
}
# redirect all https requests to http://example.com
server {
listen 443 ssl;
server_name _;
# ssl certificate and private key
ssl_certificate /etc/nginx/ssl/secure.crt;
ssl_certificate_key /etc/nginx/ssl/secure.key;
# you can add additional ssl parameters here (like the protocols
# and ciphers you want nginx to use).
return 301 http://example.com$request_uri;
}
只需将重定向的目标 URL 更改为您要使用的默认域/子域。
如果您使用的是自签名证书,浏览器仍然会提示您说该证书不受信任。一旦您将它添加为浏览器缓存中的异常,连接应该可以正常工作,而浏览器不会每次都停止将其标记为不受信任。
关于ssl - 如何在默认 nginx 服务器中完全禁用/重写 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448014/