一直学一直嗨,一直嗨一直学

NGINX使用rewrite实现http 跳转 https

关于使用HTTPS/SSL的必要性,可以自行baidu,援引的说法,EFF(Electronic Frontier Foundation),全球过半流量采用https。下面我们介绍使用rewrite 方式实现http 跳转 https。

Nginx – rewrite 方式

Nginx Server 配置

server {      listen  80;      server_name www.test.com test.com;      rewrite ^(.*)$  https://$host$1 permanent;  }    server {      listen       443 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       443 ssl;      listen       80;      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 497  https://$host$uri?$args;  }

Tags:,