NGINX:检查 SSL(HTTPS)是否可用,如果不可用,则重定向到 HTTP

NGINX:检查 SSL(HTTPS)是否可用,如果不可用,则重定向到 HTTP

当前配置是:

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;
    }

相关内容