本文将先容如何利用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
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |