编译安装NGINX——为NGINX添加正向代理模块转发局域网其他主机流量、Stream模块转发TCP/UDP请求 参考文档:
使用Nginx正向代理让内网主机通过外网主机访问互联网
为yum源配置代理
nginx | 转发 tcp 端口流量
编译安装 NGINX 为了给 NGINX 添加这些额外的功能, 需要在编译 NGINX 的时候额外添加对应的功能模块
下载所需的源码和依赖 选择NGINX 1.20.2
的源码 进行编译
正向代理模块proxy_connect_rewrite_1018
下载 , 详细用例也可以参考它的Github页面
安装编译所需的依赖
1 2 3 4 yum install pcre pcre-devel openssl openssl-devel -y apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev -y
修补 NGINX 源码并编译安装 首先解压 NGINX 源码和模块
1 2 3 tar -xzf nginx-1.20.2.tar.gz ngx_http_proxy_connect_module-0.0.7.tar.gz root@ecs-nginx-master:/home/source# ls nginx-1.20.2 nginx-1.20.2.tar.gz ngx_http_proxy_connect_module-0.0.7 ngx_http_proxy_connect_module-0.0.7.tar.gz
封入 patch
1 2 3 4 cd /home/source/nginx-1.20.2/patch -p1 < /home/source/ngx_http_proxy_connect_module-0.0.7/patch/proxy_connect_rewrite_1018.patch
Bash 中输出的内容示例
1 2 3 4 5 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 3 4 5 6 7 8 9 # --with-http_ssl_module:启用SSL模块,用于支持HTTPS。 # --with-http_stub_status_module:启用状态统计模块,提供Nginx的状态信息。 # --with-http_gzip_static_module:启用对静态文件的Gzip压缩支持。 # --with-http_realip_module:启用Real IP模块,用于获取真实的客户端IP地址。 # --with-stream:启用TCP和UDP代理模块 ./configure --prefix=/etc/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-stream --add-module=/home/source/ngx_http_proxy_connect_module-0.0.7 # 编译和安装Nginx make && make install
使用 Systemd 管理 NGINX 创建软链接, 将应用链接到Linux PATH
, 如果编译过程中修改了路径参数, 记得同步修改
1 2 cd /etc/bin ln -s /etc/nginx/sbin/nginx nginx
创建 systemd 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo "[Unit] Description=The NGINX HTTP and reverse proxy server After=network.target Wants=network.target [Service] # 解除 65535 线程的限制, 不需要可以注释掉 # LimitNOFILE=65535 Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/etc/nginx/sbin/nginx -t ExecStart=/etc/nginx/sbin/nginx ExecReload=/etc/nginx/sbin/nginx -s reload ExecStop=/etc/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target" | tee /etc/systemd/system/nginx.service
同样, 如果编译过程中修改了编译生成文件的路径, 那么在systemd
配置中也需要同步替换Exec*
等参数
配置的时候确保其中LimitNOFILE, PIDFile
和nginx.conf
中的配置保持一致, 更多关于LimitNOFILE
的内容可以参考配置limits.conf解除Linux中用户或进程的资源限制
根据业务情况, 修改/etc/nginx/nginx/conf
文件, 以下配置可供参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 user root; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/conf/mime.types; default_type text/html; 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 300s; client_body_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; proxy_connect_timeout 300s; client_body_buffer_size 1024k; gzip on; gzip_min_length 10k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 5; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript; gzip_vary on; server_names_hash_max_size 1024; server_names_hash_bucket_size 512; client_max_body_size 1024m; # 引入 项目的配置文件 和 工具网站 include /etc/nginx/sites-enabled/*/*/*.conf; include /etc/nginx/tools/*.conf; # 配置NGINX的 正向代理 forward-proxy server { # 配置服务器监听的端口号 listen 8081; # 服务器名称为localhost server_name localhost; # 指定DNS服务器地址, 禁用IPv6解析 resolver 223.5.5.5 ipv6=off; # 开启HTTP CONNECT方法支持, 用于建立与后端服务器的TCP连接 proxy_connect; # 允许通过代理的端口, 这里允许443和80端口 proxy_connect_allow 443 80; # 建立连接的超时时间为30秒 proxy_connect_connect_timeout 30s; # 读取数据的超时时间为30秒 proxy_connect_read_timeout 30s; location / { # 将请求转发到代理目标 proxy_pass $scheme://$http_host$request_uri; } } } stream { # 引入 TCP/UDP 的四层代理配置文件 include /etc/nginx/stream-enabled/*/*.conf; }
正向代理配置和验证、TCP/UDP转发配置和验证 正向代理的配置和验证 代理的 IP 是 NGINX 的内网 IP 地址, PORT 是 NGINX 监听正向代理的端口
修改 http 和 https 的代理
1 2 3 4 5 6 vim /etc/profile --- export http_proxy=IP:PORTexport https_proxy=IP:PORT--- source /etc/profile
修改 yum 源的代理
1 2 3 4 5 6 vim /etc/yum.repos.d/openEuler.repo --- # 在文件末尾追加 proxy=http://IP:PORT --- yum makecache
修改 docker 代理配置
可以参考为Docker配置代理-详解Docker的三种网络代理配置
使用 curl 验证 http 代理
1 curl -I https://www.baidu.com/ -v -x IP:PORT
TCP/UDP转发的配置和验证 1 2 3 4 stream { # 引入 TCP/UDP 的四层代理配置文件 include /etc/nginx/stream-enabled/*/*.conf; }
由上面 NGINX 的配置文件可知, Stream
的配置文件存放在/etc/nginx/stream-enabled/*/*.conf
参考示例
1 2 3 4 5 6 7 8 9 10 11 upstream ssh { server 192.168.6.141:22; } server { # 公网IP 58.x.y.z listen 20721; proxy_connect_timeout 60s; proxy_pass ssh; }