bash Curl:绕过本地主机的代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13559377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:00:19  来源:igfitidea点击:

Curl: Bypass proxy for localhost

bashcurlproxy

提问by Tom Sarduy

I'm under a proxy, and if I try curl http://localhost/mysiteor curl http://127.0.0.1/mysitecurl try to resolve it with the proxy. So I tried with --noproxyoption, but doesn't work. Is working ok for external servers with the proxy as curl http://mysite.com.

我在代理下,如果我尝试curl http://localhost/mysitecurl http://127.0.0.1/mysitecurl 尝试使用代理解决它。所以我尝试了--noproxy选项,但不起作用。对于代理为curl http://mysite.com.

My configuration:

我的配置:

  • Cygwin (bash) under Windows 8 with curl extension.
  • Proxy: proxy.domain.xx:1080without authentication
  • http_proxy=http://proxy.domain.xx:1080
  • Local Server: XAMP Version 1.8.0
  • Apache ports: 80,443
  • Browser: Chrome with proxy, but configured to access to localhostand *.dev
  • Windows 8 下的 Cygwin (bash) 带有 curl 扩展。
  • 代理:proxy.domain.xx:1080无需身份验证
  • http_proxy=http://proxy.domain.xx:1080
  • 本地服务器:XAMP 版本 1.8.0
  • Apache 端口:80,443
  • 浏览器:带有代理的 Chrome,但配置为访问localhost*.dev

From the curl --help

从 curl --help

--noproxy : Comma-separated list of hosts which do not use proxy

--noproxy :逗号分隔的不使用代理的主机列表

What I tried:

我试过的:

  • I have deactivated the firewall and nothing
  • $ curl -v http://localhost/mysite-> Debug:

    Response

    Connected to proxy.domain.xx (200.55.xxx.xx) port 1080 (#0)
    GET http://localhost/mysite HTTP/1.1
    User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
    Host: localhost
    Accept: */*
    Proxy-Connection: Keep-Alive
    The system returned: <PRE><I>(111) Connection refused</I></PRE>
    
  • curl -v --noproxy localhost, http://localhost/muestra

    Response

    About to connect() to localhost port 80 (#0)
    * Trying 127.0.0.1... 
    * Connected to localhost (127.0.0.1) port 80 (#0)
    > GET /mysite HTTP/1.1
    > User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
    > Host: localhost
    > Accept: */*
    >
    < HTTP/1.1 301 Moved Permanently
    < Server: Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4
    < Location: http://localhost/mysite
    < Content-Length: 331
    < Content-Type: text/html; charset=iso-8859-1
    
  • 我已经停用了防火墙,什么也没有
  • $ curl -v http://localhost/mysite-> 调试:

    回复

    Connected to proxy.domain.xx (200.55.xxx.xx) port 1080 (#0)
    GET http://localhost/mysite HTTP/1.1
    User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
    Host: localhost
    Accept: */*
    Proxy-Connection: Keep-Alive
    The system returned: <PRE><I>(111) Connection refused</I></PRE>
    
  • curl -v --noproxy localhost, http://localhost/muestra

    回复

    About to connect() to localhost port 80 (#0)
    * Trying 127.0.0.1... 
    * Connected to localhost (127.0.0.1) port 80 (#0)
    > GET /mysite HTTP/1.1
    > User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
    > Host: localhost
    > Accept: */*
    >
    < HTTP/1.1 301 Moved Permanently
    < Server: Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4
    < Location: http://localhost/mysite
    < Content-Length: 331
    < Content-Type: text/html; charset=iso-8859-1
    

Any idea how to fix this?

知道如何解决这个问题吗?

回答by Udo Klein

After

curl -v --noproxy localhost, http://localhost/muestra

curl responded with

curl 回应了

About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 80 (#0)

So it clearly stated that it connected to localhost.

所以它清楚地说明它连接到本地主机。

回答by jigar137

use

curl -v --noproxy '*' http://abc.com

curl -v --noproxy '*' http://abc.com

to completely disable proxy.

完全禁用代理。

or if you want to disable proxy for only abc.com destination

或者如果您只想为 abc.com 目的地禁用代理

curl -v --noproxy "abc.com" http://abc.com

curl -v --noproxy "abc.com" http://abc.com

where abc.com is the url you want to go

abc.com 是你想去的网址

回答by borlafu

As some others said, the --noproxyoptions is what you are looking for. https://curl.haxx.se/docs/manpage.html#--noproxy

正如其他人所说,--noproxy选项就是您要寻找的。 https://curl.haxx.se/docs/manpage.html#--noproxy

Apparently, the second request you tried was receiving a HTTP 301 response, so you probably also want to use the -Loption to follow redirects: https://curl.haxx.se/docs/manpage.html#-L

显然,您尝试的第二个请求是接收 HTTP 301 响应,因此您可能还想使用-L跟随重定向的选项:https: //curl.haxx.se/docs/manpage.html#-L

You can alias curl to always ignore proxies for localhost requests.

您可以使用 curl 别名来始终忽略 localhost 请求的代理。

alias curl='curl --noproxy localhost,127.0.0.1'

alias curl='curl --noproxy localhost,127.0.0.1'

Add it to your .bashrc file for convenience:

为方便起见,将其添加到您的 .bashrc 文件中:

echo "alias curl='curl --noproxy localhost,127.0.0.1'" >> ~/.bashrc

echo "alias curl='curl --noproxy localhost,127.0.0.1'" >> ~/.bashrc

回答by emeralddove

In Windows, the following option worked for me for connecting to localhost.

在 Windows 中,以下选项适用于我连接到 localhost。

curl --proxy "" --location http://127.0.0.1:8983

curl --proxy "" --location http://127.0.0.1:8983

回答by Hasitha

try below, to bypass proxy service for local ips

尝试下面,绕过本地ips的代理服务

export no_proxy=localhost,127.0.0.1

回答by Aftab Naveed

Curl expects the port to be specified with proxy this solution worked for me

Curl 期望使用代理指定端口此解决方案对我有用

export http_proxy="http://myproxy:80"

导出 http_proxy="http://myproxy:80"

回答by Mssoubri Omar

As a workaround, you can just unset the variable http_proxy before running your curl without the option --noproxy.

作为一种解决方法,您可以在不使用 --noproxy 选项的情况下运行 curl 之前取消设置变量 http_proxy。