如何使用NGINX作为任何请求位置的转发代理? [英] How to use NGINX as forward proxy for any requested location?

查看:106
本文介绍了如何使用NGINX作为任何请求位置的转发代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NGINX配置为正向代理,以替代我们用作正向代理的Fiddler.我们使用的Fiddler功能允许我们将所有传入请求代理到8888端口.如何使用NGINX做到这一点?

在NGINX作为反向代理的所有示例中,我看到 proxy_pass 始终定义为特定的上游/代理服务器.我该如何配置它,以便将其发送到请求的服务器,而与使用Fiddler作为转发代理的方式相同,而与服务器无关.

示例:

在我的代码中:

  WebProxy proxyObject = new WebProxy("http://mynginxproxyserver:8888/",true);WebRequest req = WebRequest.Create("http://www.contoso.com");req.Proxy = proxyObject; 

在mynginxproxyserver/nginx.conf中,我不想将代理委派给另一台服务器(例如proxy_pass设置为 http://someotherproxyserver ).相反,我希望它只是一个代理服务器,并将请求从我的客户端(请参见上文)重定向到请求主机.这是Fiddler在将其启用为代理时所做的事情:

Nginx最初被设计为反向代理,而不是正向代理.但是它仍然可以用作前进的目标.这就是为什么您可能找不到太多配置的原因.

这是一个理论上的答案,因为我自己从未做过,但是像下面这样的配置应该可以工作.

 服务器{听8888;地点/{解析器8.8.8.8;#可能有必要,也可能没有.proxy_pass http://$ http_host $ uri $ is_args $ args;}} 

这只是重要的部分,您需要配置其余部分.

这个想法是proxy_pass将传递给变量主机,而不是预定义的主机.因此,如果您请求 http://example.com/foo?bar ,则您的http标头将包含 example.com 的主机.这将使您的proxy_pass从 http://example.com/foo?bar 检索数据.

您链接的文档正在将其用作反向代理.等同于

  proxy_pass http://localhost:80; 

I am trying to configure NGINX as a forward proxy to replace Fiddler which we are using as a forward proxy. The feature of Fiddler that we use allows us to proxy ALL incoming request to a 8888 port. How do I do that with NGINX?

In all examples of NGINX as a reverse proxy I see proxy_pass always defined to a specific upstream/proxied server. How can I configure it so it goes to the requested server, regardless of the server in the same way I am using Fiddler as a forward proxy.

Example:

In my code:

WebProxy proxyObject = new WebProxy("http://mynginxproxyserver:8888/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;

In mynginxproxyserver/nginx.conf I do not want to delegate the proxying to another server (e.g. proxy_pass set to http://someotherproxyserver). Instead I want it to just be a proxy server, and redirect requests from my client (see above) to the request host. That's what Fiddler does when you enable it as a proxy: http://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/UseFiddlerAsReverseProxy

解决方案

Your code appears to be using a forward proxy (often just "proxy"), not reverse proxy and they operate quite differently. Reverse proxy is for server end and something client doesn't really see or think about. It's to retrieve content from the backend servers and hand to the client. Forward proxy is something the client sets up in order to connect to rest of the internet. In turn, the server may potentially know nothing about your forward proxy.

Nginx is originally designed to be a reverse proxy, and not a forward proxy. But it can still be used as a forward one. That's why you probably couldn't find much configuration for it.

This is more a theory answer as I've never done this myself, but a configuration like following should work.

server {
    listen       8888;

    location / {
        resolver 8.8.8.8; # may or may not be necessary.
        proxy_pass http://$http_host$uri$is_args$args;
    }
}

This is just the important bits, you'll need to configure the rest.

The idea is that the proxy_pass will pass to a variable host rather than a predefined one. So if you request http://example.com/foo?bar, your http header will include host of example.com. This will make your proxy_pass retrieve data from http://example.com/foo?bar.

The document that you linked is using it as a reverse proxy. It would be equivalent to

        proxy_pass http://localhost:80;

这篇关于如何使用NGINX作为任何请求位置的转发代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆