美文网首页
Nginx做代理服务器代理支持HTTPS请求

Nginx做代理服务器代理支持HTTPS请求

作者: Sheeper丶 | 来源:发表于2019-01-21 17:39 被阅读0次

首先大家应该会用到过nginx做http的代理,这种情况网络上搜一下大把大把的文章都按照步骤都能做出http的代理。

还有一种不需要安装模块时用http做https的代理。举个例子,内网一台机子A不能访问https://www.baidu.com,另一台B能访问https://www.baidu.com,那在B上安装nginx并做一个正向代理,配置信息如下即可。

server {

listen 82;

location / {

resolver 8.8.8.8;

proxy_pass https://$http_host$request_uri;

}

但是这种情况A通过B的82端口代理时只能访问http://www.baidu.com,并不能访问https://www.baidu.com,注意少了一个s。我们发现在访问https://www.baidu.com时是报错curl: (56) Received HTTP code 400 from proxy after CONNECT。用curl http://www.baidu.com时我们发现是可输出百度的内容,但是在浏览器里输入却会默认添加s,这样还是无法正常访问https的域名。

后面我们将用另外一种方式做代理https的方式,可以直接访问https而不需要访问http来转发请求。

首先我们要做的是在nginx上添加一个大牛人写的ngx_http_proxy_connect_module模块,添加后在nginx的配置文件配置即可。

下面将提供用yum安装nignx方式下做https代理的步骤。

用yum安装下的nginx做https代理

先查看当前安装的nginx版本,命令:nginx -V

下载与当前版本一致的nginx以及ngx_http_proxy_connect_module模块

当前版本为1.12.2,命令:wget http://nginx.org/download/nginx-1.12.2.tar.gz

命令:wget https://github.com/chobits/ngx_http_proxy_connect_module/archive/master.zip

解压命令:

tar -xzvf nginx-1.12.2.tar.gz  

unzip master.zip

进入nginx文件夹并安装connect补丁

cd nginx-1.12.2

patch -p1 < /root/nginx/ngx_http_proxy_connect_module/patch/proxy_connect.patch

后面是ngx_http_proxy_connect_module模块具体的路径

补丁版本对应关系请参考作者主页:https://github.com/chobits/ngx_http_proxy_connect_module

根据第一步查到已有的模块,加上本次需新增的模块

执行的命令: ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --add-module=/root/nginx/ngx_http_proxy_connect_module-master

编译时如出问题则需要添加依赖,百度一下即可。

一般还需要这些命令:

yum -y install libxml2 libxml2-dev libxslt-devel

yum -y install gd-devel

yum -y install perl-devel perl-ExtUtils-Embed

yum -y install GeoIP GeoIP-devel GeoIP-data

编译完成后执行 make  

注意:千万别执行make install

编译成功移动新的nginx前先备份一个原来的nginx 出错了还可以还原回去

命令: mv /usr/sbin/nginx /usr/sbin/nginx.bak

然后把obj里的nginx移动到/usr/sbin/ 

配置nginx.conf文件

server {

listen 8443;

# dns resolver used by forward proxying

resolver 8.8.8.8;

# forward proxy for CONNECT request

proxy_connect;

proxy_connect_allow 443 563;

proxy_connect_connect_timeout 10s;

proxy_connect_read_timeout 10s;

proxy_connect_send_timeout 10s;

# forward proxy for non-CONNECT request

location / {

proxy_pass http://$host;

proxy_set_header Host $host;

}

}

重启nginx即可,测试结果

相关文章

  • Nginx做代理服务器代理支持HTTPS请求

    首先大家应该会用到过nginx做http的代理,这种情况网络上搜一下大把大把的文章都按照步骤都能做出http的代理...

  • 问题:使用不对的http协议发送请求

    情景描述 使用nginx反向代理服务器 服务器支持https协议 发送请求的时候将url写成了http开头 发现服...

  • CentOS LNMP (Linux+Nginx+MariaDB

    Nginx( engIne x)是一个高性能的 Web 和反向代理服务器 NginX支持 HTTP、Https 和...

  • Nginx反向代理服务器+负载均衡

    nginx反向代理服务器+负载均衡 用nginx做反向代理和负载均衡非常简单, 支持两个用法 1个proxy, 1...

  • Nginx的使用

    Nginx的使用 反向代理 代理服务器(Nginx)来接受internet上的连接请求,然后将请求转发给内部网络上...

  • https 的ssl 等级检测结果修复

    NGINX 中配置HTTPS正式后SSL等级升高解决方法 1. 需求 NGINX做代理服务器配置了HTTPS...

  • nginx

    Nginx就是反向代理服务器。一个完整的代理请求过程为:客户端首先与代理服务器创建连接,然后根据代理服务器所使用的...

  • Nginx负载均衡和动静分离

    简介 所谓负载均衡就是利用nginx的反向代理功能,将nginx作为代理服务器,只接受请求,但不处理,按照配置分给...

  • Nginx学习

    nginx 正向代理:用户通过代理服务器直接访问Internet,叫做正向代理。 反向代理:用户将请求发送给代理服...

  • Nginx 安装配置(Centos7)

    Nginx相关概念 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,...

网友评论

      本文标题:Nginx做代理服务器代理支持HTTPS请求

      本文链接:https://www.haomeiwen.com/subject/ijrcjqtx.html