Nginx接收Http协议请求转发使用Https协议

Nginx接收Http协议请求转发使用Https协议

缘起

公司使用阿里的apigateway,规定不太友好,同是SIT环境,A系统的SIT1环境居然不能调用B系统的SIT2环境的接口。因为各个系统之间部署的SIT环境数量不同A系统可能只有1套,B系统可能有8套,这样的话,可能会随时切换调用B系统的环境,管理员不允许,于是想着用Nginx做下转发。因为A系统调用B系统是内部调用,不计划使用HTTPS,因为还要去申请证书,但是B系统调用入口必须使用HTTPS,这样就要求Nginx可以接收HTTP协议的请求,转发出去的协议是HTTPS。

第一次配置Nginx

server {
        listen       10000;
        server_name  192.168.1.2;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location / {
            proxy_pass https://aaa.bbb.com:9000;
        }
    }

以为这样就可以直接转发了,但是执行nginx -t直接报错:

nginx: [emerg] https protocol requires SSL support in /data/nginx/conf/nginx.conf:224
nginx: configuration file /data/nginx/conf/nginx.conf test failed

224行就是我上面的proxy_pass https://aaa.bbb.com:9000;这一行
搜了一下说是nginx当时编译的时候没有http_ssl_module模块,使用nginx -V查看一下当时编译的参数:

nginx version: nginx/1.21.5
built by gcc 4.8.5 (SUSE Linux) 
configure arguments: --prefix=/data/nginx --with-pcre=/data/software/pcre-8.21 --with-zlib=/data/software/zlib-1.2.11 --with-openssl=/etc/ssl

果然没有http_ssl_module模块,于是决定重新编译一下nginx。

重新编译Nginx

注意:我的输出是/data/nginx,和当前正在跑的Nginx是同一个目录,先使用nginx -s stop停止nginx,然后备份conf/nginx.conf文件,防止被覆盖。

先安装依赖:pcre-8.21,zlib-1.2.11,openssl-1.0.2t
我都是下载的源码,然后编译并安装的

# pcre-8.21 使用以下命令
cd pcre-8.21 && ./configure && make && make install
# zlib-1.2.11 使用以下命令
cd zlib-1.2.11 && ./configure && make && make install
# openssl-1.0.2t 比较特殊 使用
cd openssl-1.0.2t && ./config && make && make install

进入Nginx源码目录然后使用以下命令configure:

./configure --prefix=/data/nginx --with-pcre=/data/software/pcre-8.21 --with-zlib=/data/software/zlib-1.2.11 --with-openssl=/data/software/openssl-1.0.2t --with-http_ssl_module

然后执行编译和安装:

make && make install

重启Nginx

编译完成后发现之前的Nginx二进制文件变成了nginx.old,新的Nginx文件叫nginx,给这个nginx二进制加执行权限,然后执行

nginx -t

此时不再报错,提示成功:

nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful

然后查看conf/nginx.conf,发现没有被覆盖,可以直接启动Nginx了:

nginx -c /data/nginx/conf/nginx.conf

启动完成后用ps命令查看以下进程果然在。
然后使用postman测试,发现可以正确转发,大功告成。

猜你喜欢

转载自blog.csdn.net/u013014691/article/details/131194536