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

打印 上一主题 下一主题

主题 893|帖子 893|积分 2679


本文将先容如何利用Nginx搭建一个简单的正向代理服务器,实现外部网络哀求的转发。通过设置正向代理,我们可以隐藏真实的服务器地点,进步访问速率,以及增强网络安全性
一、Nginx正向代理简介
正向代理(Forward Proxy)是一种网络服务,它位于客户端和服务器之间,客户端通过正向代理向服务器发送哀求,服务器将哀求转发给目标服务器,并将目标服务器的相应返回给客户端。正向代理可以隐藏真实的服务器地点,实现客户端对目标服务器的访问。
由于nginx正向代理的功能指令较少,只需要进行简单的配置即可
  1. server {  
  2.     resolver 114.114.114.114;       #指定DNS服务器IP地址  
  3.     listen 8080;  
  4.     location / {  
  5.         proxy_pass http://$http_host$request_uri;
  6.     }  
  7. }  
复制代码
以上的配置只能访问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)等。可以通过以下命令安装:
  1. yum install gcc make pcre-devel zlib-devel openssl-devel
复制代码
1.2.1 下载对应版本的nginx(源码编译)
  1. wget https://nginx.org/download/nginx-1.20.2.tar.gz
  2. tar -zxvf nginx-1.15.12.tar.gz -C /usr/local/src
  3. cd /usr/local/src
  4. [root@web01 src] ls
  5. 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代理功能。

  1. [root@web01 src] yum install git
  2. [root@web01 src] git clone https://github.com/chobits/ngx_http_proxy_connect_module
  3. [root@web01 src] cd nginx-1.20.2
  4. [root@web01 nginx-1.20.2] ls
  5. 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 过不去
  1. [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
  2. patching file src/http/ngx_http_core_module.c
  3. patching file src/http/ngx_http_parse.c
  4. patching file src/http/ngx_http_request.c
  5. patching file src/http/ngx_http_request.h
  6. patching file src/http/ngx_http_variables.c
复制代码
1.2.4 源码安装
指定安装的路径和关联的模块而且增加所需的模块
  1. [root@web01 nginx-1.20.2] ./configure --prefix=/usr/local/nginx \
  2. --with-http_stub_status_module \
  3. --with-http_ssl_module \
  4. --with-file-aio --with-http_realip_module \
  5. --add-module=/usr/local/src/ngx_http_proxy_connect_module/
  6. [root@web01 nginx-1.20.2] make
  7. [root@web01 nginx-1.20.2] make install
复制代码
查看安装情况:
  1. [root@web01 ~] tree /usr/local/nginx/
  2. /usr/local/nginx/
  3. ├── conf
  4. │   ├── fastcgi.conf
  5. │   ├── fastcgi.conf.default
  6. │   ├── fastcgi_params
  7. │   ├── fastcgi_params.default
  8. │   ├── koi-utf
  9. │   ├── koi-win
  10. │   ├── mime.types
  11. │   ├── mime.types.default
  12. │   ├── nginx.conf       # nginx主配置文件
  13. │   ├── nginx.conf.default
  14. │   ├── scgi_params
  15. │   ├── scgi_params.default
  16. │   ├── uwsgi_params
  17. │   ├── uwsgi_params.default
  18. │   └── win-utf
  19. ├── html
  20. │   ├── 50x.html
  21. │   └── index.html
  22. ├── logs
  23. └── sbin
  24.     └── nginx    #  Nginx 的二进制可执行文件,这是通过源码编译得到的 nginx 可执行程序,可以直接在这个目录下通过 ./nginx 命令启动 Nginx 服务器
复制代码
查看版本信息:
  1. [root@web01 ~] /usr/local/nginx/sbin/nginx -V
  2. nginx version: nginx/1.20.2
  3. built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
  4. built with OpenSSL 3.0.7 1 Nov 2022
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module
  7. --with-http_ssl_module --with-file-aio
  8. --with-http_realip_module --add-module=/usr/local/src/ngx_http_proxy_connect_module/
复制代码
修改nginx的主配置文件
  1. [root@web01 ~] vim /usr/local/nginx/conf/nginx.conf
复制代码
  1. server {
  2.         listen 8787;
  3.         resolver  223.5.5.5 114.114.114.114 valid=300s;
  4.         resolver_timeout 10s;
  5.         proxy_connect;
  6.         proxy_connect_allow 443 80;
  7.         proxy_connect_connect_timeout 10s;
  8.         proxy_connect_read_timeout 10s;
  9.         proxy_connect_send_timeout 10s;
  10.         location / {
  11.                 proxy_set_header Host $host;
  12.                 proxy_pass $scheme://$http_host$request_uri;
  13.                 proxy_buffers 256 4k;
  14.                 proxy_max_temp_file_size 0k;
  15.                 proxy_connect_timeout 30;
  16.                 proxy_send_timeout 60;
  17.                 proxy_read_timeout 60;
  18.                 proxy_next_upstream error timeout invalid_header http_502;
  19.         }
  20.     }
复制代码
  1.    listen 8787; 表示服务器监听8787端口接收请求。 resolver 8.8.8.8;   
  2.    配置DNS解析器,使用Google的公共DNS服务器进行域名解析。 proxy_connect;   
  3.    开启代理连接模块,通常用于处理HTTPS代理。 proxy_connect_allow 443 563;   
  4.    允许代理连接到443(HTTPS标准端口)和563(通常用于某些加密的实时通信协议)端口   
  5.    proxy_connect_connect_timeout 10s;, proxy_connect_read_timeout 10s
  6.    proxy_connect_send_timeout 10s; 分别设置了HTTPS代理连接的建立、读取和发送超时时间,均为10秒
  7.    location / { ... } 匹配所有请求,并使用proxy_pass指令将请求代理到相同方案(HTTP或HTTPS)、相同主机和相同URI的地址
  8. 请注意,proxy_pass语句中使用的变量$scheme, $http_host和$request_uri是从请求中捕获的,这样可以保持原始请求的完整性,当转发请求到后端时,这些变量将被替换为实际的请求值。
复制代码
  1. [root@web01 ~] nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
复制代码
启动命令
  1. nginx -c /usr/local/nginx/conf/nginx.conf   
  2. or
  3. systemctl start nginx
  4. or
  5. nginx -s reload
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表