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

一、采纳 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 这一篇就够了

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理