给nginx配置SSL证书之后,https可以正常访问,http访问显示404错误(400错误),nginx的配置如下:
server {
listen 80 default backlog=2048;
listen 443;
server_name lvtao.net;
root /var/www/html;
//这里注意ssl on,如果存在,则访问http提示400,如果不存在,则提示404
ssl on;
ssl_certificate /usr/local/Tengine/sslcrt/***.crt;
ssl_certificate_key /usr/local/Tengine/sslcrt/***.key;
}
原因是http的请求被发送到https的端口上去了,所以才会出现这样的问题。
把ssl on;这行去掉,修改为下面的配置:
server {
//测试了一下listen 80 后面不加 default backlog=2048 也可以
listen 80 default backlog=2048;
//测试了一下listen 443 后面不加 ssl 也可以
listen 443 ssl ;
server_name lvtao.net;
root /var/www/html;
ssl_certificate /usr/local/Tengine/sslcrt/***.crt;
ssl_certificate_key /usr/local/Tengine/sslcrt/***.key;
}
nginx的rewrite方法,即访问http跳转到https,配置如下
server {
listen 80;
server_name lvtao.net;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
index index.html index.htm;
}
}
server {
listen 443 ssl ;
server_name lvtao.net;
root /var/www/html;
ssl_certificate /usr/local/Tengine/sslcrt/***.crt;
ssl_certificate_key /usr/local/Tengine/sslcrt/***.key;
}
参考文章