![NGINX:检查 SSL(HTTPS)是否可用,如果不可用,则重定向到 HTTP](https://linux22.com/image/678084/NGINX%EF%BC%9A%E6%A3%80%E6%9F%A5%20SSL%EF%BC%88HTTPS%EF%BC%89%E6%98%AF%E5%90%A6%E5%8F%AF%E7%94%A8%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%B8%8D%E5%8F%AF%E7%94%A8%EF%BC%8C%E5%88%99%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%20HTTP.png)
当前配置是:
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
listen 192.168.0.5:80 default_server;
listen 192.168.0.5:443 ssl default_server;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate /usr/share/nginx/default/ssl/default.crt;
ssl_certificate_key /usr/share/nginx/default/ssl/default.key;
server_name _;
root /usr/share/nginx/default/www/;
index index.php index.html;
error_log /usr/share/nginx/default/log/error.log;
access_log /usr/share/nginx/default/log/access.log combined;
error_page 404 /404.html;
error_page 403 /403.html;
error_page 500 502 503 504 /50x.html;
recursive_error_pages on;
location = /404.html {
internal;
}
location = /403.html {
internal;
}
location = /50x.html {
internal;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files /1a9fa3dd0cfcb3bfa9827857c25d35d7.htm @php;
}
location @php {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
当有 https 调用时,在浏览器验证证书是否有效甚至可用之前,服务器就会重定向到 HTTP 协议。
答案1
只要提出这个条件
if ($ssl_protocol = "") {
return 301 http://$server_name$request_uri;
}