搭建Nginx正向代理服务器,轻松实现外部网络请求的转发


本文将介绍如何使用Nginx搭建一个简单的正向代理服务器,实现外部网络请求的转发。通过设置正向代理,我们可以隐藏真实的服务器地址,提高访问速度,以及增强网络安全性

一、Nginx正向代理简介

正向代理(Forward Proxy)是一种网络服务,它位于客户端和服务器之间,客户端通过正向代理向服务器发送请求,服务器将请求转发给目标服务器,并将目标服务器的响应返回给客户端。正向代理可以隐藏真实的服务器地址,实现客户端对目标服务器的访问。

由于nginx正向代理的功能指令较少,只需要进行简单的配置即可

server {  
    resolver 114.114.114.114;       #指定DNS服务器IP地址  
    listen 8080;  
    location / {  
        proxy_pass http://$http_host$request_uri;
    }  
}  

以上的配置只能访问80 端口的网站,而不能访问https443端口的网站,现在的网站基本上都是https的要解决技能访问http80端口也能访问https443端口的网站,需要置两个SERVER节点,一个处理HTTP转发,另一个处理HTTPS转发,而客户端都通过HTTP来访问代理,通过访问代理不同的端口,来区分HTTP和HTTPS请求。

由于原生 nginx 只支持 http 正向代理,为了 nginx 支持 https 正向代理,可以打 ngx_http_proxy_connect_module 补丁+ ssl 模块支持。

准备环境:

确保系统中安装了编译所需的工具和依赖,例如GCC、make、pcre-devel( pere 库 )、zlib-devel、openssl-devel(https)等。可以通过以下命令安装:

yum install gcc make pcre-devel zlib-devel openssl-devel

1.2.1 下载对应版本的nginx(源码编译)

wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar -zxvf nginx-1.15.12.tar.gz -C /usr/local/src
cd /usr/local/src
[root@web01 src] ls
nginx-1.20.2  

当前目录ngx_http_proxy_connect_module下包含了一些文件和子目录,这些内容是与Nginx的ngx_http_proxy_connect_module第三方模块相关的。这个模块用于增强Nginx,使其支持HTTP CONNECT方法,从而能够作为正向代理处理SSL/TLS连接,尤其是对HTTPS流量的代理。
下面是列出的各文件和目录的简要说明:

config:这是配置文件,用来定义编译该模块时的一些特定配置或宏定义。 LICENSE:包含该模块的许可协议信息。
ngx_http_proxy_connect_module.c:核心源代码文件,实现了模块的功能。
patch:这个子目录内包含了用于将该模块集成到Nginx源代码的补丁文件,
README.md:自述文件,通常会包含模块的简介、安装指南、配置示例等重要信息。
t:这个子目录可能包含一些测试脚本或示例,用于验证模块功能是否正常工作。 如果打算编译并安装这个模块到Nginx,一般流程包括:
阅读README.md:了解详细的安装步骤、依赖关系以及任何特殊的配置要求。 应用补丁:根据README.md的指导,选择合适的补丁文件,并应用到Nginx源代码中。
配置Nginx:在Nginx的./configure命令中确保指定了该模块,例如,如果模块的配置文件或说明中有特定的–add-module参数,需要在配置时加入。
编译并安装Nginx:执行make和make install命令完成编译和安装。
配置Nginx配置文件:在Nginx的配置文件(如nginx.conf)中,根据模块的文档添加相应的配置指令以启用和配置HTTP
CONNECT代理功能。

[root@web01 src] yum install git
[root@web01 src] git clone https://github.com/chobits/ngx_http_proxy_connect_module
[root@web01 src] cd nginx-1.20.2
[root@web01 nginx-1.20.2] ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  ngx_http_proxy_connect_module  objs  README  src

​1.2.3 使用https代理模块对源代码修改,选择增加模块需要打补丁的版本
![在请添加图片描述
对 nginx 源码修改,这一步很重要,不然后面的 make 过不去

[root@web01 patch] patch -d /usr/local/src/nginx-1.20.2 -p 1 < /usr/local/src/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
patching file src/http/ngx_http_core_module.c
patching file src/http/ngx_http_parse.c
patching file src/http/ngx_http_request.c
patching file src/http/ngx_http_request.h
patching file src/http/ngx_http_variables.c

1.2.4 源码安装
指定安装的路径和关联的模块并且增加所需的模块

[root@web01 nginx-1.20.2] ./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-file-aio --with-http_realip_module \
--add-module=/usr/local/src/ngx_http_proxy_connect_module/
[root@web01 nginx-1.20.2] make
[root@web01 nginx-1.20.2] make install

查看安装情况:

[root@web01 ~] tree /usr/local/nginx/
/usr/local/nginx/
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf       # nginx主配置文件
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── html
│   ├── 50x.html
│   └── index.html
├── logs
└── sbin
    └── nginx    #  Nginx 的二进制可执行文件,这是通过源码编译得到的 nginx 可执行程序,可以直接在这个目录下通过 ./nginx 命令启动 Nginx 服务器

查看版本信息:

[root@web01 ~] /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC) 
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module 
--with-http_ssl_module --with-file-aio 
--with-http_realip_module --add-module=/usr/local/src/ngx_http_proxy_connect_module/

修改nginx的主配置文件

[root@web01 ~] vim /usr/local/nginx/conf/nginx.conf
 server {
        listen 8787;
        resolver  223.5.5.5 114.114.114.114 valid=300s;
        resolver_timeout 10s;
        proxy_connect;
        proxy_connect_allow 443 80;
        proxy_connect_connect_timeout 10s;
        proxy_connect_read_timeout 10s;
        proxy_connect_send_timeout 10s;

        location / {
                proxy_set_header Host $host;
                proxy_pass $scheme://$http_host$request_uri;
                proxy_buffers 256 4k;
                proxy_max_temp_file_size 0k;
                proxy_connect_timeout 30;
                proxy_send_timeout 60;
                proxy_read_timeout 60;
                proxy_next_upstream error timeout invalid_header http_502;
        }
    }


   listen 8787; 表示服务器监听8787端口接收请求。 resolver 8.8.8.8;   
   配置DNS解析器,使用Google的公共DNS服务器进行域名解析。 proxy_connect;   
   开启代理连接模块,通常用于处理HTTPS代理。 proxy_connect_allow 443 563;   
   允许代理连接到443(HTTPS标准端口)和563(通常用于某些加密的实时通信协议)端口   
   proxy_connect_connect_timeout 10s;, proxy_connect_read_timeout 10s
   proxy_connect_send_timeout 10s; 分别设置了HTTPS代理连接的建立、读取和发送超时时间,均为10秒
   location / { ... } 匹配所有请求,并使用proxy_pass指令将请求代理到相同方案(HTTP或HTTPS)、相同主机和相同URI的地址

请注意,proxy_pass语句中使用的变量$scheme, $http_host和$request_uri是从请求中捕获的,这样可以保持原始请求的完整性,当转发请求到后端时,这些变量将被替换为实际的请求值。
[root@web01 ~] nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动命令

nginx -c /usr/local/nginx/conf/nginx.conf   
or
systemctl start nginx 
or
nginx -s reload
  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值