关于nginx:nginx-如何将-http-转成-https

3次阅读

共计 1098 个字符,预计需要花费 3 分钟才能阅读完成。

一、采纳 nginx 的 rewrite 办法

  1. 上面是将所有的 http 申请通过 rewrite 重写到 https 上。
    例如将所有的 dev.wangshibo.com 域名的 http 拜访强制跳转到 https。
    上面配置均能够实现:
server {
    listen 80;
    server_name dev.wangshibo.com;
    index index.html index.php index.htm;

    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;

    # 办法 1 这是 ngixn 早前的写法,当初还能够应用

    rewrite ^(.*)$  https://$host$1 permanent;

    # 办法 2 这是 nginx 最新反对的写法

    # return  301 https://$server_name$request_uri;

    # 办法 3 这种形式实用于多域名的时候,即拜访 wangshibo.com 的 http 也会强制跳转到 https://dev.wangshibo.com 下面
    # 例如 server_name dev.wangshibo.com wangshibo.com *.wangshibo.com;

    # if ($host ~* "^wangshibo.com$") {#   rewrite ^/(.*)$ https://dev.wangshibo.com/ permanent;
    # }

    # 上面是最简略的一种配置
    # if ($host = "dev.wangshibo.com") {#   rewrite ^/(.*)$ http://dev.wangshibo.com permanent;
    # }

    location ~ / {
      root /var/www/html/8080;
      index index.html index.php index.htm;
    }
  }

下面的跳转配置 rewrite ^(.*)$ https://$host$1 permanent;

也能够改为上面
rewrite ^/(.*)$ http://dev.wangshibo.com/$1 permanent;

或者
rewrite ^ http://dev.wangshibo.com$request_uri? permanent;

二、通过 proxy_redirec 形式

# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;

参考链接

  • Nginx 之 https 配置 – 运维笔记 (http->https 强转)
  • Nginx 百科全书 -nginx 这一篇就够了
正文完
 0