V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
JackHuang90
V2EX  ›  NGINX

nginx 如何使用系统 http_proxy 代理访问外部网络,从而代理外部服务进内网?

  •  
  •   JackHuang90 · 339 天前 · 1642 次点击
    这是一个创建于 339 天前的主题,其中的信息可能已经有所发展或是发生改变。

    环境如下: nginx 在内网,但是内网有一个代理,可以通过代理访问外网。系统上使用

    http_proxy=http://10.10.2.10:8080/
    https_proxy=http://10.10.2.10:8080/
    

    没问题。但是 nginx 却不能使用这个配置访问外网。 想通过 nginx 代理外网的服务进入内网,供内网机器使用,想问下需要如何配置。

    试过这个配置好像不行的样子

    location /cip/ {
        proxy_set_header Host www.cip.cc;
        proxy_pass http://10.10.2.10:8080/;  # 通过代理连接
    }
    
    8 条回复    2023-05-25 14:28:26 +08:00
    Jacksu
        1
    Jacksu  
       339 天前
    试试:
    proxy_pass http://www.cip.cc
    JackHuang90
        2
    JackHuang90  
    OP
       339 天前
    @Jacksu 不行,nginx 不使用系统的代理设置
    JackHuang90
        3
    JackHuang90  
    OP
       339 天前
    @Jacksu 还会提示没有 dns ,因为内网是没有 dns
    rekulas
        5
    rekulas  
       339 天前
    纯 nginx 不清楚怎么设置,简单的 proxy_pass 应该不行,发送的 header 不完整代理不知道你要访问什么,得中间加一层封装下请求才行

    如果是我会考虑 go 实现一个简单的代理然后设置上级代理就行了
    ysc3839
        6
    ysc3839  
       339 天前 via Android
    要把请求的 PATH 改成完整 URL ,比如原本 HTTP 请求头是 GET /test HTTP/1.1 ,实际去请求 http proxy 的话应该是 GET http://example.com/test HTTP/1.1
    另外 https 是没法支持的,所以更建议用 netcat 之类的端口转发到代理的功能
    yinmin
        7
    yinmin  
       339 天前 via iPhone   ❤️ 1
    先用 socat 将外部网站通过代理引到 127.0.0.1:10000 ,然后 nginx proxy_pass http://127.0.0.1:10000
    JackHuang90
        8
    JackHuang90  
    OP
       339 天前
    @lt0136 @yinmin 这个可行,代码如下
    ```
    socat TCP4-LISTEN:28080,reuseaddr,fork PROXY:10.10.2.10:www.cip.cc:80,proxyport=8080
    ```
    nginx
    ```
    location ^~ /cip/ {
    proxy_set_header Host www.cip.cc;
    proxy_pass http://localhost:28080/; # 通过代理连接
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1067 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:53 · PVG 02:53 · LAX 11:53 · JFK 14:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.