nginx 正向代理转发http跟https

对于nginx,可能大家用的最多的是反向代理,正向代理用的不多。又或者 ,使用正向代理,可能会选择squid作为方案。但是,我这次就用nginx作为正向代理,为啥,因为squid我找不到资料可以获取post请求的参数,又或者说获取不到requestBody的数据,不然何苦来哉折腾nginx的正向代理。。。。。。。如果有大佬知道,squid的access.log日志如何能记录post的请求参数,一定要评论指导一下。

进入正文,使用nginx的正向代理,是为了搭建代理服务器,然后客户端设置代理为我的ip,这样我就能获取客户端发出的http(非https)请求的一些参数,然后做一些不可描述的事情。

首先nginx是天然支持http转发的,但是不支持https转发,这里我们可以安装阿里的nginx模块。这里是入口哟https://zhuanlan.zhihu.com/p/70459013。但是目前是无法转发除了80跟443端口的请求,因为nginx内置的变量无法获取目标服务器的端口,需要搭配lua脚本。下篇文章介绍。

#下载
wget http://nginx.org/download/nginx-1.14.0.tar.gz
#解压
tar -xzf nginx-1.14.0.tar.gz
cd nginx-1.14.0

#安装编译环境
yum update
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

#打补丁 支持https正向代理,补丁地址:https://github.com/chobits/ngx_http_proxy_connect_module直接整下下载下来就行,记得地址匹配上,我的是下载在/path/to/下面
#因为我是1.14版本,所以选择这个补丁,不同版本记得去文档看看对应的补丁
patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1014.patch

#配置
./configure \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--add-module=/path/to/ngx_http_proxy_connect_module

#编译
make

#安装
make install

#验证
/usr/local/nginx/sbin/nginx -V

修改nginx的配置文件:

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
   server {
     listen  12306;
	 charset utf-8;

     # dns resolver used by forward proxying
     resolver  114.114.114.114;

     # forward proxy for CONNECT request
     proxy_connect;
     proxy_connect_allow            all;    #设置为all,允许转发所有的端口
     proxy_connect_connect_timeout  10s;
     proxy_connect_read_timeout     10s;
     proxy_connect_send_timeout     10s;

     # forward proxy for non-CONNECT request
	 location / {
        #试过使用 $scheme://$host$request_uri;好像不起作用
		if ($scheme = 'http') {
			proxy_pass http://$host$request_uri;
		}
		if ($scheme = 'https') {
			proxy_pass https://$host$request_uri;
		}
		 proxy_set_header Host $host;		 
		 proxy_buffers 256 4k;
		 proxy_max_temp_file_size 0k; 
		 
	 }
	}
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值