我拥有一个网站,例如HTTP的example.com。考虑到安全的东西,现在我想将HTTP更改为HTTPS。我希望所有的老客户仍然可以访问我的网站,即使他们使用example.com,它将通过Nginx重定向到https。如何在将HTTP重定向到HTTPS时处理Nginx中的400错误
当然,我用Google搜索了很多,然后我的解决办法是:
upstream www {
server 127.0.0.1:4000;
}
server {
listen 80;
listen 443 ssl;
server_name localhost www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
if ($ssl_protocol = "") {
rewrite^https://$host$request_uri? permanent;
}
# below are some other stuff
# ...
}
但是,当我访问example.com,我得到:
400错误请求的普通HTTP请求被发送到HTTPS端口
然后我改变了nginx.conf,阅读Redirect in nginx后,和配置由497 error_page:
upstream www {
server 127.0.0.1:4000;
}
server {
listen 80;
listen 443 ssl;
server_name localhost www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
error_page 497 https://$host$request_uri;
# below are some other stuff
# ...
}
然后它工作,一切都很好。但我只是不知道为什么和error_page的解决方案似乎是werid。所以 看完Dealing with nginx 400 “The plain HTTP request was sent to HTTPS port” error后,我加了默认并删除了ssl。
upstream www {
server 127.0.0.1:4000;
}
server {
listen 80;
listen 443 default ssl;
server_name localhost www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
if ($ssl_protocol = "") {
rewrite^https://$host$request_uri? permanent;
}
# below are some other stuff
# ...
}
太棒了!它再次运作。但我不确定:
- 哪个解决方案是正确的?
- 如果两者都正确,哪个更适合SEO?
它的工作原理,谢谢 – banruosheng 2014-12-07 14:41:17