在Nginx中將http://zh30.com:443跳轉到https://zh30.com:443
作者: 鄭曉 分類: nginx, 建站經驗 發布于: 2020-05-22 11:55 瀏覽:8,120 評論(3)
有小伙伴反應我博客半年沒更新了,借此機會趕緊水一篇。
另有小伙伴求助于我一個這樣的問題,說在使用http://協議外加443端口訪問時,nginx會報錯提示:
“400 Bad Request The plain HTTP request was sent to HTTPS port”
這個錯誤是指請求錯誤,http協議的請求被發送到了https的端口。在Nginx中,不能在一個端口同時處理http和https請求。按正常瀏覽來說也不可能會有這種鏈接(請求)的存在。
nginx在使用https的端口的http訪問時,會產生一個內部報錯碼497,所以在監聽443端口的server中,將這個497錯誤跳轉出去即可。
使用nginx的error_page指令來捕捉497錯誤并重定向如下:
error_page 497 https://$server_name$request_uri;
如果是非標準端口,可能需要在$server_name后追加:$server_port。
error_page 默認使用302跳轉,如果想指定其它狀態碼,如301,可以在錯誤碼后添加 =301,如:
error_page 497 =301 https://$server_name$request_uri;
示例如我的博客:
listen 443 ssl;
server_name zh30.com www.twogeaux.com;
error_page 497 https://$server_name$request_uri;
index index.html index.htm index.php;
加上上面這句后,重新reload配置后,瀏覽器訪問http://zh30.com:443時將直接跳轉到https://zh30.com, 不再會有400 bad request錯誤產生了,至此問題解決。
加一個參考鏈接,顯得專業:
關于ssl及497錯誤碼:http://nginx.org/en/docs/http/ngx_http_ssl_module.html
關于error_page:http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
本文采用知識共享署名-非商業性使用 3.0 中國大陸許可協議進行許可,轉載時請注明出處及相應鏈接。
本文永久鏈接: http://www.twogeaux.com/nginx-http-port-443.html
在Nginx中將http://zh30.com:443跳轉到https://zh30.com:443:目前有3 條留言
測試
持續更新不容易,博主可以留個郵箱么,想深入和你交流下(從畢業那會兒仿了一個你的網站,咱們友鏈已經三年啦)
漂亮叔