首页 > 系统相关 >nginx 正向代理

nginx 正向代理

时间:2024-03-18 12:55:06浏览次数:23  
标签:http -- 代理 module nginx proxy 正向 connect

环境规划

序号

IP地址

操作系统

用途

1

192.168.174.126(外网)

192.168.80.128 (内网)

ubuntu 23.10

代理服务器

2

192.168.80.129 (内网)

ubuntu 23.10

Linux客户端

软件版本

nginx:1.24.0
代理模块:proxy_connect_rewrite_102101.patch

安装 nginx

# cd nginx-1.24.0
# patch -p1 < /root/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.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
Hunk #1 succeeded at 1104 (offset -14 lines).
Hunk #2 succeeded at 1742 (offset -14 lines).
Hunk #3 succeeded at 2045 (offset -30 lines).
patching file src/http/ngx_http_request.h
Hunk #1 succeeded at 416 (offset 5 lines).
patching file src/http/ngx_http_variables.c
./configure  --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-stream_ssl_preread_module --with-poll_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-stream --with-stream_ssl_module --with-cc-opt=-Wno-error --with-ld-opt= --user=nginx --group=nginx --with-threads --with-file-aio --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock --with-pcre --add-module=/root/ngx_http_proxy_connect_module
make -j 8 && make install

proxy.conf

server {
    listen       88;
    server_name  localhost;
    resolver 202.106.0.20 ipv6=off;    # DNS解析, 禁用 ipv6
    # 配置代理请求的缓冲区大小
    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;

    # ngx_http_proxy_connect_module 参数配置
    proxy_connect;
    proxy_connect_allow 443 563;
    proxy_connect_timeout 30s;
    proxy_connect_send_timeout 60s;
    proxy_connect_read_timeout 60s;
    
    # 设置代理访问日志
    #access_log  /var/log/nginx/proxy.access.log  main;

    location / {
        # 配置需要代理的目标服务器地址和端口
        proxy_pass http://$http_host;
        # 配置代理请求的头信息
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 配置代理超时时间
        proxy_connect_timeout 5s;
        proxy_send_timeout 10s;
        proxy_read_timeout 10s;

        
        # 配置代理请求的缓冲区大小
        proxy_buffering on;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;

        # 可选:禁用缓存
        # proxy_cache off;
    }
}

客户端代理配置

export http_proxy=http://192.168.80.128:88
export https_proxy=http://192.168.80.128:88

客户端HTTP测试

# curl -I http://www.baidu.com
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 18 Mar 2024 04:47:20 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache

客户端HTTPS测试

# curl -I https://www.baidu.com
HTTP/1.1 200 Connection Established
Proxy-agent: nginx

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Mon, 18 Mar 2024 04:48:14 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

参考文档

https://github.com/chobits/ngx_http_proxy_connect_module

标签:http,--,代理,module,nginx,proxy,正向,connect
From: https://www.cnblogs.com/wangguishe/p/18080096

相关文章

  • Nginx底层基础数据结构
    基础数据结构ngx_int_t32位操作系统4字节,64位操作系统8字节解决跨平台以及,普通int类型在x86和x64操作系统上面是4字节,在类型转换时造成内存浪费(如在x64下面转换long类型)typedefintptr_tngx_int_t;#ifdef_WIN64typedef__int64intptr_t;#elsetype......
  • 解释nginx.conf.default 中关于fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_sc
    在nginx配置中,fastcgi_param指令用于定义传递给FastCGI服务器(如PHP-FPM)的参数。这些参数是FastCGI协议的一部分,用于在nginx和PHP-FPM之间传递关于请求的信息。在给出的配置片段中:nginxfastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;这一行定义了一个FastCGI......
  • nginx 反向代理注意
    1.Nginx中正则捕获组在proxy_pass中不可用有一个需求,根据不同的路径代理到后端问题出在$1这个正则捕获组上。在Nginx中,正则表达式中的捕获组是根据()定义的,但这个捕获组只在location内部有效,并且不会传递到proxy_pass中。所以当你在proxy_pass中使用$1时,它实......
  • Nginx服务启动失败
    问题描述:Nginx服务启动失败解决方案:查看是否有如下服务起来,有需要停止服务并改为手动SQLServer导致。其中很有可能是SQLServerReportingServices(MSSQLSERVER),它是SQLServer的日志系统。IIS服务。如果你电脑安装了这个,很有可能它在运行着,那么它就占用着80端口Worl......
  • nginx
    .配置文件路径:Nginx的配置文件通常位于/etc/nginx目录下。主要的配置文件是nginx.conf。2.静态文件配置:在nginx.conf文件中,可以通过server块来配置静态文件的访问。例如,可以添加以下配置来指定静态文件的根目录和访问路径server{listen80;server_nameexample.com;locati......
  • 如何查找访问 Nginx 的前 10 个 IP?
    在管理和维护Web服务器时,了解谁正在访问您的网站是非常重要的。Nginx是一个流行的Web服务器,通过分析其访问日志,您可以了解访问者的来源、频率以及他们的行为。有时候,您可能希望查找访问量最高的IP地址,以便进一步分析或采取措施,比如加强安全性或优化性能。本文将详细......
  • nginx基础篇
    web服务器介绍Unix和Linux平台下的常用Web服务器常见有:ApacheNginxLighttpdTomcatIBMWebSphere其中最为广泛的是Nginx,在Windows平台上最常用的是微软的IIS(InternetInformationServer,互联网信息服务)是Windows系统中默认的Web服务程序。nginx介绍nginx是什么1.......
  • 动态代理和反射的基本学习
    今天在跟着视频学习的时候发现老师讲的知识点都只简单的了解过但是没有深入学习,导致在跟着视频敲代码的时候完全不知道自己是在写什么东西。所以决定先把基础补一补再继续跟老师做项目。打算先把自定义注解的编写和解析学好,想要学号这一块,又涉及到了Aop和java中反射的学习,那么话......
  • Ubuntu,centos安装nginx
    安装包:https://pan.baidu.com/s/1kVCEBcVnginx需要依赖openssl,pcre,zlib包,加上nginx包一共4个包,放在同一目录,依次执行命令:tarxzfnginx-1.12.2.tar.gztarxzfopenssl-fips-2.0.9.tar.gztarxzfpcre-8.41.tar.gztarxzfzlib-1.2.11.tar.gzcdopenssl-fips-2.0.9/./config......
  • Nginx的安装和使用
    Nginx的安装和使用Nginx主要功能:反向代理、负载均衡、动静分离使用docker-compose安装cd/opt/docker_composemkdirdocker_nginxcddocker_nginxtouchdocker-compose.yml编写docker-compose.yml后保存version:'3.1'services:nginx:restart:always......