Nginx代理HTTPS

1,501 阅读2分钟

公司一个系统需要对接一个第三方HTTPS接口,由于公司是内网,调用第三方接口都是由一个nginx代理服务器实现,现在要让这台nginx能够代理https

需求描述

公司一个系统需要对接一个第三方HTTPS接口,由于公司是内网,调用第三方接口都是由一个nginx代理服务器实现,现在要让这台nginx能够代理https

实现步骤

安装openssl

首先确保机器上安装了openssl和openssl-devel

yum install -y openssl
yum install -y openssl-devel

或者选择编译安装,最近还遇到过一个 Sweet32(CVE-2016-2183,CVE-2016-6329)漏洞,这两天再整理一下吧

tar zxvf openssl-1.1.1h.tar.gz
cd openssl-1.1.1h
./config --prefix=/usr/local/openssl
make && make install

重新编译nginx支持https

查看当前安装的nginx是否已经有 http_ssl_module 模块

/usr/local/nginx/sbin/nginx -V

在 Nginx 的安装目录里面,执行如下命令。--prefix 是代表的 Nginx 的安装目录,后面的 --with- 是代表的目前的编译参数。以前的参数要记得复制下来,然后在这个参数基础上增加 ssl 模块需要的参数 --with-http_ssl_module

# 重新编译nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make编译

执行 make 命令编译 Nginx,make 完之后在 objs 目录下就多了个 nginx。这个就是我们新的 Nginx 程序。

make

备份原 Nginx,用刚生成的覆盖老的

注: 虽然这里的备份不是必须的,但是强烈建议备份。这是个好习惯。

复制原来的 Nginx 作为备份。

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

用上个步骤生成的新的 Nginx 程序覆盖老的。覆盖之前请先停掉你的 Nginx 服务

scriptcp objs/nginx /usr/local/nginx/sbin/nginx
# 查看 Nginx 信息
/usr/local/nginx/sbin/nginx -V

配置nginx代理

    server {
        listen       8091;
        server_name  localhost;
        location / {
           proxy_pass https://xxx.com;

           proxy_next_upstream     error timeout;
           proxy_connect_timeout 3600;
           proxy_read_timeout 3600;
           proxy_send_timeout 3600;

           proxy_intercept_errors  on;
           #proxy_set_header        Host $host:$server_port;
           #proxy_set_header        X-Forwarded-Host $host:$server_port;
           #proxy_set_header        X-Forwarded-Port $server_port;
           #proxy_set_header        X-Forwarded-Server $host;
           #proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
           #proxy_set_header        X-Real-IP $remote_addr;
           #proxy_set_header        Cookie $http_cookie;

           client_max_body_size    1000m;
        }
    }

注意:一定要把上面注释的部分注释掉哦,如果第三方接口设置了白名单,会报405

区分正向代理与反向代理

理解下正向代理与反向代理 正向代理,几个人团购一个商品 客户端---代理---互联网---服务器

反向代理,一个商城有多个供应商注册并提供商品 客户端---互联网---代理---服务器