我擁有一個網站,例如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