Nginx: http 跳转 https

时间:2022-07-17 07:56:22

参考:博文

参考:HTTP 状态码解读

Nginx - rewrite 方式

Nginx Server 配置

server {
listen ;
server_name www.test.com test.com;
rewrite ^(.*)$ https://$host$1 permanent; } server {
listen ssl;
server_name www.ourdax.com; ssl_certificate /usr/local/openresty/nginx/conf/ssl/test.pem;
ssl_certificate_key /usr/local/openresty/nginx/conf/ssl/test.key; root /usr/local/openresty/nginx/html;
index index.html;
location / { } }

Nginx - 状态码 497

关于 Nginx 状态码 497

497 - normal request was sent to HTTPS

当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码

实现跳转思路

利用 error_page 命令将 497 状态码的链接重定向到指定 URL

Nginx Server 配置

server {
listen ssl;
listen ;
server_name www.test.com; ssl_certificate /usr/local/openresty/nginx/conf/ssl/test.pem;
ssl_certificate_key /usr/local/openresty/nginx/conf/ssl/test.key; root /usr/local/openresty/nginx/html;
index index.html; location / { } error_page https://$host$uri?$args;
}

总结

关于 原博文 在 html 里用 refresh 的方式做跳转的方式我不是很认可,还是把这些事情交给 web server 处理好一些。