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

355 天前
 JackHuang90

环境如下: 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/;  # 通过代理连接
}
1680 次点击
所在节点    NGINX
8 条回复
Jacksu
355 天前
试试:
proxy_pass http://www.cip.cc
JackHuang90
355 天前
@Jacksu 不行,nginx 不使用系统的代理设置
JackHuang90
355 天前
@Jacksu 还会提示没有 dns ,因为内网是没有 dns
lt0136
355 天前
rekulas
355 天前
纯 nginx 不清楚怎么设置,简单的 proxy_pass 应该不行,发送的 header 不完整代理不知道你要访问什么,得中间加一层封装下请求才行

如果是我会考虑 go 实现一个简单的代理然后设置上级代理就行了
ysc3839
355 天前
要把请求的 PATH 改成完整 URL ,比如原本 HTTP 请求头是 GET /test HTTP/1.1 ,实际去请求 http proxy 的话应该是 GET http://example.com/test HTTP/1.1
另外 https 是没法支持的,所以更建议用 netcat 之类的端口转发到代理的功能
yinmin
355 天前
先用 socat 将外部网站通过代理引到 127.0.0.1:10000 ,然后 nginx proxy_pass http://127.0.0.1:10000
JackHuang90
355 天前
@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/; # 通过代理连接
}
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/942771

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX