用 Nginx 实现 https 转 http

http://blog.csdn.net/lvye1221/article/details/53843607

缘由

当前公司服务器已经采用 http 协议的方式部署成功,可 App Store 要求必须采用 https 协议,那么,能否在不改变公司服务器代码的情况下,实现 https 的要求呢?

答案是肯定的,采用 Nginx 反向代理实现(以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器)。

windows 环境下安装

http://nginx.org/

一些代码

unzip nginx-1.3.13.zip

cd nginx-1.3.13

cd c:/nginx-1.10.2/
  • 1
  • 2
  • 3
  • 4
  • 5

启动

start nginx


// 临时代码
c:/nginx-1.10.2/start.bat
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

停止

停止:
nginx.exe -s stop

或
nginx.exe -s quit


批量删除 nginx 进程(可能点击太多引起的)
taskkill /F /IM nginx.exe > nul 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

重新载入

nginx.exe -s reload
  • 1

配置

nginx-1.10.2/conf/nginx.conf

    server {
        # 监听8080端口
        listen 8080;

        # 服务器根目录
        root html/data/up1;

        location / {
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

配置好后,打开地址即可访问页面( html/data/up1 下的默认首页)

http://localhost:8080

启动 ssl

    server {
        listen       443 ssl;

        # 域名,实际情况下时,将这个改成域名
        server_name  localhost;

        ssl on;

        # 证书位置
        ssl_certificate  ssl/server.crt;
        ssl_certificate_key ssl/server.key;
        location / {
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

可以打开这个地址 
https://localhost:8080

当然,浏览器会提示站点不安全,解决这个问题的方式是 选择官方颁布的 SSL 证书

反向代理

方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端

示例

将 1.gif 放在 html/data/up1 目录下

详细可参照帮助文档 
http://nginx.org/en/docs/beginners_guide.html

服务器1

server {
    listen 8080;
    root html/data/up1;

    location / {
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

服务器2

server {
    listen 82;

    location / {
        # 当前的转发到
        proxy_pass http://localhost:8080;
    }

    location /images/ {
        root html/data;
    }

    # location ~ \.(gif|jpg|png)$ {
    #   root html/data/images;
    # }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

http://localhost:82/1.gif 
如果最后配置打开后,相当于找 root 为 html/data/images/1.gif 的文件

http://localhost:82/images/1.gif ==> http://localhost:82/data/images/1.gif 
因为匹配到了 /images/ 相当于找 root 为 html/data/images/1.gif

http://localhost:82/data/images/1.gif ==> http://localhost:8080/data/images/data/images/1.gif 
所以,找不到了

https 转 http

转发测试示例

    server {
        listen 82;
        listen       443 ssl;

        ssl on;
        ssl_certificate  ssl/server.crt;
        ssl_certificate_key ssl/server.key;

        location / {
            # 转发到 http 服务器中
            proxy_pass http://localhost;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

苹果ATS验证

更新Nginx根目录下 conf/nginx.conf 文件如下:

server { 
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; 
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
}

允许windows 防火墙端口例外

443

nginx 配置

    server {
        listen       443 ssl;

        # server_name s4.url.cn; #填写绑定证书的域名
        server_name localhost; #匹配的域名名称

        ssl on;
        ssl_certificate ssl/1_s4.url.cn_bundle.crt;
        ssl_certificate_key ssl/2_s4.url.cn.key;

        ssl_session_timeout 5m;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
        ssl_prefer_server_ciphers on;       

        location / {
            proxy_pass http://s4.url.cn;
        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

技巧

发现错误的调试方法

查看错误日志,这里通常会写好路径转发的测试

c:/nginx-1.10.2/logs/error.log

IP地址的SSL可以访问,但域名不可以

找了好久,才发现测试的 域名 与 IP 地址不一致,当然域名访问不了啦

参考资料

反向代理:Web服务器的“经纪人” 
http://www.open-open.com/lib/view/open1417488526633.html


修改转发的信息 
http://blog.csdn.net/u010391029/article/details/50395680


SSL证书配置 
http://www.wosign.com/news/ios-app-https.htm



缘由

当前公司服务器已经采用 http 协议的方式部署成功,可 App Store 要求必须采用 https 协议,那么,能否在不改变公司服务器代码的情况下,实现 https 的要求呢?

答案是肯定的,采用 Nginx 反向代理实现(以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器)。

windows 环境下安装

http://nginx.org/

一些代码

unzip nginx-1.3.13.zip

cd nginx-1.3.13

cd c:/nginx-1.10.2/
  • 1
  • 2
  • 3
  • 4
  • 5

启动

start nginx


// 临时代码
c:/nginx-1.10.2/start.bat
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

停止

停止:
nginx.exe -s stop

或
nginx.exe -s quit


批量删除 nginx 进程(可能点击太多引起的)
taskkill /F /IM nginx.exe > nul 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

重新载入

nginx.exe -s reload
  • 1

配置

nginx-1.10.2/conf/nginx.conf

    server {
        # 监听8080端口
        listen 8080;

        # 服务器根目录
        root html/data/up1;

        location / {
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

配置好后,打开地址即可访问页面( html/data/up1 下的默认首页)

http://localhost:8080

启动 ssl

    server {
        listen       443 ssl;

        # 域名,实际情况下时,将这个改成域名
        server_name  localhost;

        ssl on;

        # 证书位置
        ssl_certificate  ssl/server.crt;
        ssl_certificate_key ssl/server.key;
        location / {
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

可以打开这个地址 
https://localhost:8080

当然,浏览器会提示站点不安全,解决这个问题的方式是 选择官方颁布的 SSL 证书

反向代理

方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端

示例

将 1.gif 放在 html/data/up1 目录下

详细可参照帮助文档 
http://nginx.org/en/docs/beginners_guide.html

服务器1

server {
    listen 8080;
    root html/data/up1;

    location / {
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

服务器2

server {
    listen 82;

    location / {
        # 当前的转发到
        proxy_pass http://localhost:8080;
    }

    location /images/ {
        root html/data;
    }

    # location ~ \.(gif|jpg|png)$ {
    #   root html/data/images;
    # }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

http://localhost:82/1.gif 
如果最后配置打开后,相当于找 root 为 html/data/images/1.gif 的文件

http://localhost:82/images/1.gif ==> http://localhost:82/data/images/1.gif 
因为匹配到了 /images/ 相当于找 root 为 html/data/images/1.gif

http://localhost:82/data/images/1.gif ==> http://localhost:8080/data/images/data/images/1.gif 
所以,找不到了

https 转 http

转发测试示例

    server {
        listen 82;
        listen       443 ssl;

        ssl on;
        ssl_certificate  ssl/server.crt;
        ssl_certificate_key ssl/server.key;

        location / {
            # 转发到 http 服务器中
            proxy_pass http://localhost;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

苹果ATS验证

更新Nginx根目录下 conf/nginx.conf 文件如下:

server { 
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; 
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
}

允许windows 防火墙端口例外

443

nginx 配置

    server {
        listen       443 ssl;

        # server_name s4.url.cn; #填写绑定证书的域名
        server_name localhost; #匹配的域名名称

        ssl on;
        ssl_certificate ssl/1_s4.url.cn_bundle.crt;
        ssl_certificate_key ssl/2_s4.url.cn.key;

        ssl_session_timeout 5m;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
        ssl_prefer_server_ciphers on;       

        location / {
            proxy_pass http://s4.url.cn;
        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

技巧

发现错误的调试方法

查看错误日志,这里通常会写好路径转发的测试

c:/nginx-1.10.2/logs/error.log

IP地址的SSL可以访问,但域名不可以

找了好久,才发现测试的 域名 与 IP 地址不一致,当然域名访问不了啦

参考资料

反向代理:Web服务器的“经纪人” 
http://www.open-open.com/lib/view/open1417488526633.html


修改转发的信息 
http://blog.csdn.net/u010391029/article/details/50395680


SSL证书配置 
http://www.wosign.com/news/ios-app-https.htm


猜你喜欢

转载自blog.csdn.net/mengzhengjie/article/details/79475037
今日推荐