Tagged: HTTPS

Nginx同时支持http和https访问请求

server {
    listen  80; 
    listen  443;
    server_name 979137.com;
    ...
    ssl  on
    ssl_certificate  /home/www/hosts/979137.com.crt;
    ssl_certificate_key  /home/www/hosts/979137.com.key;
    ssl_session_timeout  5m;
    ssl_protocols SSLv2 SSLv3 TLSv1;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;
    ...
}

给 nginx 配置SSL证书之后,https可以正常访问,http访问显示400错误(400 Bad Request)
报错如下:

The plain HTTP requset was sent to HTTPS port

说是 http 的请求被发送到https的端口上去了,所以才会出现这样的问题。

把 ssl on;这行去掉,ssl 写在443端口后面。这样http和https的链接都可以用,完美解决!

server {
    listen  80; 
    listen  443 ssl;
    server_name 979137.com;
    ...
    #ssl  on
    ssl_certificate  /home/www/hosts/979137.com.crt;
    ssl_certificate_key  /home/www/hosts/979137.com.key;
    ssl_session_timeout  5m;
    ssl_protocols SSLv2 SSLv3 TLSv1;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;
    ...
}

如果你想强制使用https访问,对端口进行判断,然后rewrite即可:

if ($server_port != 443) {
    rewrite (.*) https://$host$1 permanent;
}

最新文章

Return Top