Redirect localhost through proxy?

2019-03-20 03:54发布

问题:

I have a HTTP proxy running at localhost:1234. The proxy works fine for all web requests I make.

I have a server running at localhost:4567 . I want HTTP requests to my server to go through my proxy. Should be simple, right? Is there a way to make IE or any other browser do this?

回答1:

Generally you can configure your browser settings for this. In Firefox it's Options -> Advanced -> Network -> Connection (Settings).

IE automatically ignores proxies if it detects a localhost URL. This has always been a thorn in the side of tools like Fiddler.

However, you can often get around that by literally going to http://somesite.com:1234. Someone has taken the time to register the "somesite.com" domain to automatically route to 127.0.0.1. This fools IE into thinking it's an outside domain, but should run through your proxy while redirecting to your local server.

Good luck.



回答2:

Yes, there is a way!

In IE9 if you have the proxy manually configured in Internet Options, you can click on Advanced and simply add <-loopback> to the proxy bypass list. In IE6, localhost URLs go through the proxy when the proxy is manually configured. It is only versions IE7+ that don't send localhost requests to the proxy server.

If you want a more global solution, you can create a automatic proxy configuration script. It is basically a javascript file that contains the function FindProxyForURL. You can configure Internet Options with the URL of that script. All HTTP requests will query FindProxyForURL for the proxy server it needs. So if you want all URLs to go through the proxy you would do something like:

function FindProxyForURL(url, host) {
    return "PROXY localhost:1234";
}

If you only want external addresses to go to your localhost proxy then you would do something like:

function FindProxyForURL(url, host) {
    if (isPlainHostName(host)) { 
          return "DIRECT"; 
    }
    return "PROXY localhost:1234";
}


回答3:

On Windows:

Go to Windows/System32/Drivers/Etc

in notepad running as administrator

Add something like this to your hosts file:

127.0.0.1 mysite.local

then all data to that host at http://mysite.local will be picked up by the proxy.

Ubuntu: /etc/hosts

Mac: http://decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/



回答4:

It depends on your browser. In Firefox, check to see that "no proxy" is empty. By default Firefox blocks proxy of URLs to localhost and 127.0.0.1.

mozilla.org