例如,分別使用預設的 80、443 PORT
http://example.com 的網址要強制轉到 https://example.com
可直接在 HTTP 的設定中,return 301 轉址到 HTTPS
server { listen 80; server_name example.com; return 301 https://$host$request_uri; } server { listen 443 ssl; ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; server_name example.com; }
[情況2]:將原本 HTTP 使用的 PORT,直接改給 HTTPS 使用。
修改前後 HTTP、HTTPS 使用相同的 PORT,例如都使用 8080 PORT
處理方式:若在 HTTPS 的 PORT 上,使用 HTTP 訪問,會出現 error_page 497 的錯誤。當發生此錯誤時,將頁面轉址到 HTTPS 的網址。
server { listen 8080 ssl; ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; server_name example.com; #error_page 497 301 =307 https://$host:$server_port$request_uri; error_page 497 https://$host:$server_port$request_uri; }
參考:
Handling http and https requests using a single port with nginx - Server Fault
http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
ngxin做http强制跳转https,接口的POST请求变成了GET
nginx error_page - IT閱讀
nginx不得不说的参数之 为什么nginx error_page遇到后端错误时不跳转?-运维者说:从菜鸟到老鸟-51CTO博客
你的筆記很多都是很好的重點
回覆刪除簡潔明瞭
謝謝,你的留言讓我有點感動 哈
刪除^^
刪除