让http和https在nginx中共存
有个小项目要用到https,我就弄了个证书,配置上去后https可以正常访问,但是之前的http的都是显示400错误,nginx的配置如下:
server {
listen 80 default backlog=2048;
listen 443;
server_name iw3c.com;
root /var/www/html;
ssl on;
ssl_certificate /usr/local/nginx/sslcrt/linuxyan.com.crt;
ssl_certificate_key /usr/local/nginx/sslcrt/linuxyan.com.key;
}
http访问的时候,报错如下:
400 Bad Request
The plain HTTP requset was sent to HTTPS port. Sorry for the inconvenience.
Please report this message and include the following information to us.
Thank you very much!
说是http的请求被发送到https的端口上去了,所以才会出现这样的问题。那这样不是很坑了,,,我只是一部分需要https,但是这样搞的话,http的链接都不能用了,后来经查询资料调整配置文件如下:
server {
listen 80 default backlog=2048;
listen 443 ssl;
server_name iw3c.com;
root /var/www/html;
ssl_certificate /usr/local/nginx/sslcrt/linuxyan.com.crt;
ssl_certificate_key /usr/local/nginx/sslcrt/linuxyan.com.key;
}
把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用,完美解决。