Proxy 接続設定のまとめ


Proxy の設定方法をいつも忘れてしまうので、まとめ - 自分メモ。
ついでなので、設定に関する補足事項なども記載。

  1. CheatSheet
  2. 詳細 (補足事項)
    1. Python
      1. Python - requests
      2. conda/Anaconda
    2. apt/apt-get (Ubuntu/Debian 系)
    3. yum (CentOS/Red Hat系)
    4. curl
    5. wget
    6. Splunk
    7. docker

CheatSheet

対象
環境変数
http_proxy
https_proxy
HTTP_PROXY
HTTPS_PROXY
引数
設定ファイル
Python
requests
- -
requests.get/put(..., proxies={"http": "https://proxy.example.com:8080"})
-
Python
conda
- - - -
-
~/.condarc (conda config --stdin)
apt
apt-get
- -
-
/etc/apt/apt.conf
yum
- - - -
--setopt=proxy=https://proxy.example.com:8080
/etc/yum.conf
curl
- -
-x https://proxy.example.com:8080
~/.curlrc
wget
- -
-e http_proxy=http://proxy.example.com:8080
~/.wgetrc
Splunk
- - - -
-
${SPLUNK_HOME}/etc/system/local/server.conf
docker
dockerd
- -
--env HTTP_PROXY="http://proxy.example.com:8080"
--env HTTPS_PROXY="https://proxy.example.com:8080"
~/.docker/config.json
/etc/systemd/system/docker.service.d/http-proxy.conf

環境変数で指定できるもの

大文字の環境変数と小文字の環境変数で異なるが、主に下の4つを指定しておけば、ある程度のコマンドは網羅できる。

export HTTP_PROXY=http://username:[email protected]:8080
export http_proxy=${HTTP_PROXY}
export HTTPS_PROXY=https://username:[email protected]:8080
export https_proxy=${HTTPS_PROXY}

各種設定ファイル

conda/Anaconda

~/.condarc
proxy_servers:
    http: http://proxy.example.com:8080
    https: https://proxy.example.com:8080

apt/apt-get

/etc/apt/apt.conf
Acquire::http::Proxy "http://proxy.example.com:8080/";
Acquire::https::Proxy "https://proxy.example.com:8080/";
Acquire::ftp::Proxy "https://proxy.example.com:8080/";

yum

/etc/yum.conf
[main]
...
proxy = https://proxy.example.com:8080

curl

~/.curlrc
proxy = protocol://username:[email protected]:port

wget

~/.wgetrc or /etc/wgetrc
http_proxy = http://proxy.example.com:8080/
https_proxy = https://proxy.example.com:8080/
ftp_proxy = http://proxy.example.com:8080/

Splunk

${SPLUNK_HOME}/etc/system/local/server.conf
[proxyConfig]
http_proxy =  http://proxy.example.com:8080
https_proxy = https://proxy.example.com:8080

docker

~/.docker/config.json
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://proxy.example.com:8080",
     "httpsProxy": "http://proxy.example.com:8080",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}

docker daemon は systemctl 側で設定する。
参照: Control Docker with systemd | Docker Documentation

/etc/systemd/system/docker.service.d/http-proxy.conf (新規作成)
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=https://proxy.example.com:8080/"
/etc/systemd/system/docker.service.d/http-proxy.conf (新規作成)
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"
Environment="HTTPS_PROXY=https://proxy.example.com:8080/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

詳細 (補足事項)

Python

Python - requests

参照: Proxies - Advanced Usage — requests-docs-ja 1.0.4 documentation

  • requests.get()/post() の引数 proxies に辞書型で指定する。
  • 'http''https' いずれかの指定は必須。
  • 現在のところ、'no_proxy' (Proxy を使用しない例外指定)はない。
  • 外部から環境変数で指定する場合には HTTP_PROXYHTTPS_PROXY を使用する。
proxiesパラメータの指定(例1)
my_proxies = {
    'http':  '10.0.0.1:8080',
    'https': '10.0.0.1:8080',
}

requests.get(..., proxies = my_proxies )
proxiesパラメータの指定(例2)
my_proxies = {
    'http':  'https://user:[email protected]:8080',
    'https': 'https://user:[email protected]:8080',
}

requests.get(..., proxies = my_proxies )

下は、サンプルプログラム。独自 CA を使用するケースも想定。

requests_sample.py
import requests
import os

# TLS フォワード Proxy としてすべての TLS 接続が Proxy によって復号されるようなケース向け
ca_verify_file = '/some/where/local_ca.crt'
ca_verify = True

# 設定するデフォルト Proxy
my_http_proxy  = 'https://proxy.example.com:8080'
my_https_proxy = 'https://proxy.example.com:8080'

# 環境変数を参照
http_proxy  = os.getenv( 'HTTP_PROXY', default=my_http_proxy )
https_proxy = os.getenv( 'HTTPS_PROXY', default=my_https_proxy )

# requests に渡す Proxy のパラメータを生成
proxies = {
    'http':  http_proxy,
    'https': https_proxy,
}

# ターゲット URL
target_url = 'https://www.google.co.jp/search'

# Payload (ここでは、クエリ)
payload = {
    'q': 'python requests proxy'
}

try:
    # Proxy は proxies= 辞書型 を渡す
    r = requests.get(target_url, params=payload, proxies=proxies, verify=ca_verify)
except requests.ConnectionError as e:
    if type(e) == requests.exceptions.SSLError:
        # ローカルな root CA 証明書ファイルを指定
        # ca_verify_file を直接指定しないのは、後で ca_verify を検査することで、
        # どちらのモードで呼び出したのかを検査できるようにするため
        ca_verify = ca_verify_file
        r = requests.get(target_url, params=payload, proxies=proxies, verify=ca_verify)
    else:
        raise e

# 取得した内容の表示
print(r.text)

上のサンプルプログラムでは、明示的に環境変数を参照して設定しているが、proxies パラメータを指定しなければ、環境変数が requests.get()/post() で参照される。

Conda / Anaconda

conda は設定ファイルで proxy を指定する方法と、conda コマンドで設定する方法がある。

.condarc を直接編集する場合

参照: Configure conda for use behind a proxy server (proxy_servers) - Using the .condarc conda configuration file — conda documentation

conda の設定ファイルは ~/.conda
.condarcYAML 形式で記述する。

~/.condarc 設定例1
proxy_servers:
    http: https://proxy.example.com:8080
    https: https://proxy.example.com:8080

接続先によって Proxy サーバを変える場合には次のように記述する。

~/.condarc 設定例2
proxy_servers:
  'http://10.20.1.128': 'http://10.10.1.10:5323'

conda コマンドによる設定

参照: conda config - Command Reference

conda コマンドを用いて設定する場合、conda config コマンドを用いるが、--set オプションでは Boolean か文字列のみ指定可能なため、YAML の構造化された設定を指定することはできない模様。

この場合、--stdin オプションを用いて、 YAML 形式で標準入力から入力する。
確認には --show オプションで、proxy_servers を参照する。

MS Windows上における conda configコマンドによる設定例1
(base) C:\Users\localuser>conda config --stdin
proxy_servers:
  http: https://proxy.example.com:8080
  https: https://proxy.example.com:8080
^Z

(base) C:\Users\localuser>conda config --show proxy_servers
proxy_servers:
  http: https://proxy.example.com:8080
  https: https://proxy.example.com:8080

(base) C:\Users\localuser>

しかし、直接入力は入力間違いが発生するなど、現実的ではないので、予めファイルを用意しておいて、標準入力に渡すほうがよいと思われる。

inputfile.txt
proxy_servers:
  http: https://proxy.example.com:8080
  https: https://proxy.example.com:8080
MS Windows上における conda configコマンドによる設定例2
(base) C:\Users\localuser>type inputfile.txt
proxy_servers:
  http: https://proxy.example.com:8080
  https: https://proxy.example.com:8080

(base) C:\Users\localuser>type inputfile.txt | conda config --stdin

(base) C:\Users\localuser>conda config --show proxy_servers
proxy_servers:
  http: https://proxy.example.com:8080
  https: https://proxy.example.com:8080

(base) C:\Users\localuser>

ファイルを用意するのであれば、~/.condarc を直接編集したほうが良いように思えるが、バッチ処理や Ansible などによる管理の際に、予め設定部分のファイルを用意しておいて利用するなどが考えられる。

個人設定ではなく、システム設定を行う場合には、--system オプションを指定する。

apt/apt-get (Ubuntu/Debian 系)

参照: Configuration Options - 6.2. aptitude, apt-get, and apt Commands

apt/apt-get の設定ファイルは man ページ ( apt.conf(5) ) に記載がある。

  • apt/apt-get のデフォルト設定ファイルは /etc/apt/apt.conf
  • 設定ファイルは環境変数 APT_CONFIG で変更可能。
  • 設定ファイル名に含められるのは、英数字、ハイフン(-)、アンダースコア(_)、ピリオド(.)
  • 環境変数で指定する場合は http_proxyhttps_proxy

各行は

group::tool::directive "value";

という形になっている。クォーテーション(") と最後のセミコロン(;)は必須。
次の書き方 3つはいずれも同じ設定。

group {
   tool {
     directive1 "value1";
     directive2 "value2";
   };
};

group::tool {
  directive1 "value1";
  directive2 "value2";
};

group::tool::drective1 "value1";
group::tool::drective2 "value2";

proxy の設定は、パッケージのダウンロードに関する Acquire グループの httphttpsftp で設定する。
httphttps に関する説明は、apt-transport-http(1)apt-transport-https(1) の man ページに記載がある。

man apt.conf(5)
       ftp
           ftp::Proxy sets the default proxy to use for FTP URIs. It is
           in the standard form of ftp://[[user][:pass]@]host[:port]/.
           Per host proxies can also be specified by using the form
           ftp::Proxy::<host> with the special keyword DIRECT meaning
           to use no proxies. If no one of the above settings is
           specified, ftp_proxy environment variable will be used. To
           use an FTP proxy you will have to set the ftp::ProxyLogin
           script in the configuration file. This entry specifies the
           commands to send to tell the proxy server what to connect
           to.        以下略
  • 設定されていない場合には、環境変数 ftp_proxy を参照する、とある。
man apt-transport-http(1)
   Proxy Configuration
       The environment variable http_proxy is supported for system wide
       configuration. Proxies specific to APT can be configured via the
       option Acquire::http::Proxy. Proxies which should be used only
       for certain hosts can be specified via
       Acquire::http::Proxy::host. Even more finegrained control can be
       achieved via proxy autodetection, detailed further below. All
       these options use the URI format
       scheme://[[user][:pass]@]host[:port]/. Supported URI schemes are
       socks5h (SOCKS5 with remote DNS resolution), http and https.
       Authentication details can be supplied via apt_auth.conf(5)
       instead of including it in the URI directly.

       The various APT configuration options support the special value
       DIRECT meaning that no proxy should be used. The environment
       variable no_proxy is also supported for the same purpose.

       以下略
man apt-transport-https(1)
OPTIONS
       The HTTPS protocol is based on the HTTP protocol, so all options
       supported by apt-transport-http(1) are also available via
       Acquire::https and will default to the same values specified for
       Acquire::http. This manpage will only document the options
       unique to https.
  • Acquire::http::Proxy で Proxy を設定。
  • 接続先ごとに設定する場合には Acquire::http::Proxy::host を使用する(hostは接続先)。
  • http のオプションは https でも同じ。
  • 環境変数は http_proxy (いずれの man ページにも https_proxy の記述ないが、経験的に有効)
環境変数の設定例
$ export ftp_proxy="https://proxy.example.com:8080"
$ export http_proxy="https://proxy.example.com:8080"
$ export https_proxy="https://proxy.example.com:8080"
$ export no_proxy="192.168.0.1,172.17.0.3,10.0.0.5"

システムワイドの恒久的な設定は、/etc/apt/apt.conf に以下を追加。

/etc/apt/apt.conf
Acquire::ftp::Proxy "https://proxy.example.com:8080/";
Acquire::http::Proxy "https://proxy.example.com:8080/";
Acquire::https::Proxy "https://proxy.example.com:8080/";

yum (CentOS/Red Hat系)

参照: yum - Trac

  • コマンドラインのオプションで指定する方法
  • 設定ファイルで指定する方法

がある。

コマンドラインのオプションで指定する場合

  • オプションで指定する場合には、--setopt を使用する。
--setopt=proxy=https://proxy.example.com:8080
man yum(8)
       --setopt=option=value
              Set  any  config option in yum config or repo files. For options
              in the global config just use:  --setopt=option=value  for  repo
              options use: --setopt=repoid.option=value
  • --setopt で指定するのは、 [main]proxyrepositoryid.proxy になる。
    man ページの記載は以下の通り。
man yum.conf(5)
DESCRIPTION
       Yum uses a configuration file at /etc/yum.conf.

       Additional  configuration  files are also read from the directories set
       by the reposdir option (default is `/etc/yum.repos.d').  See the repos-
       dir option below for further details.
<略>
[main] OPTIONS
       The  [main]  section  must exist for yum to do anything. It consists of
       the following options:
       <略>
        proxy URL to the proxy server that yum should use.  Set this  to
              `libproxy'  to  enable  proxy  auto  configuration via libproxy.
              Defaults to direct connection.

        proxy_username username to use for proxy

        proxy_password password for this proxy
<略>

[repository] OPTIONS
       The repository section(s) take the following form:

              Example: [repositoryid]
              name=Some name for this repository
              baseurl=url://path/to/repository/

              repositoryid  Must  be  a  unique  name for each repository, one
              word.
              <略>

       proxy  URL  to  the  proxy  server  for  this repository. Set to
              '_none_' to disable the global proxy setting  for  this  reposi-
              tory. If this is unset it inherits it from the global setting

       proxy_username  username  to use for proxy.  If this is unset it
              inherits it from the global setting

       proxy_password password for this proxy.  If  this  is  unset  it
              inherits it from the global setting

(見やすくするために、man ページの出力を少し修正しています)

設定ファイルで指定する場合

恒久的な設定は、 /etc/yum.conf に記述する。

/etc/yum.conf
[main]
...
proxy = https://proxy.example.com:8080

curl

参照: man ページ

  • コマンドラインの引数で指定する方法
  • 設定ファイルで指定する方法
  • 環境変数で指定する方法

コマンドラインの引数で指定する場合

引数での指定は下の通り。

curl -x https://proxy.example.com:8080 ...

man ページでの説明は下のようになっている。

man curl(1)
   -x, --proxy [protocol://]host[:port]
              Use the specified proxy.

              The proxy string can be specified with a protocol:// pre‐
              fix.  No protocol specified or http:// will be treated as
              HTTP  proxy.  Use  socks4://,  socks4a://,  socks5://  or
              socks5h://  to  request  a  specific  SOCKS version to be
              used.  (The protocol support was added in curl 7.21.7)

              HTTPS proxy support  via  https://  protocol  prefix  was
              added in 7.52.0 for OpenSSL, GnuTLS and NSS.

              Unrecognized  and  unsupported  proxy  protocols cause an
              error since 7.52.0.  Prior versions may ignore the proto‐
              col and use http:// instead.

              If  the port number is not specified in the proxy string,
              it is assumed to be 1080.

              This option overrides existing environment variables that
              set  the proxy to use. If there's an environment variable
              setting a proxy, you can set proxy to "" to override it.

              All operations that are performed over an HTTP proxy will
              transparently be converted to HTTP. It means that certain
              protocol specific operations might not be available. This
              is  not  the case if you can tunnel through the proxy, as
              one with the -p, --proxytunnel option.

              User and password that might be  provided  in  the  proxy
              string  are  URL decoded by curl. This allows you to pass
              in special characters such as @ by using %40 or pass in a
              colon with %3a.

              The proxy host can be specified the exact same way as the
              proxy environment variables, including the protocol  pre‐
              fix (http://) and the embedded user + password.

              If  this  option is used several times, the last one will
              be used.

-x または --proxy で指定すれば良いことがわかる。

設定ファイルで指定する場合

恒常的に設定する場合には、ホームディレクトリの .curlrc に下を記載する。
(Windows では _curlrc)

~/.curlrc
proxy = protocol://username:[email protected]:port

man ページの記載は下の通り。

man curl(1)
              The default  config  file  is checked for in the following
              places in this order:

              1) curl tries to find the "home dir": It first  checks  for  the
              CURL_HOME and then the HOME environment variables. Failing that,
              it uses getpwuid() on Unix-like systems (which returns the  home
              dir  given the current user in your system). On Windows, it then
              checks for the APPDATA variable, or as a last resort the '%USER‐
              PROFILE%\Application Data'.

              2)  On  windows, if there is no _curlrc file in the home dir, it
              checks for one in the same dir the curl executable is placed. On
              Unix-like  systems,  it will simply try to load .curlrc from the
              determined home dir.

環境変数で指定する場合

環境変数で設定する場合は http_proxyHTTPS_PROXYurl-protocol_PROXY (FTP_PROXY など)、ALL_PROXYNO_PROXY を使用する。
注意が必要なのは、http_proxy のみ、小文字という点。

man curl(1)
ENVIRONMENT
       The environment variables can be  specified  in  lower  case  or
       upper case. The lower case version has precedence. http_proxy is
       an exception as it is only available in lower case.

       Using an environment variable to set  the  proxy  has  the  same
       effect as using the -x, --proxy option.

       http_proxy [protocol://]<host>[:port]
              Sets the proxy server to use for HTTP.

       HTTPS_PROXY [protocol://]<host>[:port]
              Sets the proxy server to use for HTTPS.

       [url-protocol]_PROXY [protocol://]<host>[:port]
              Sets  the  proxy  server to use for [url-protocol], where
              the protocol is a protocol  that  curl  supports  and  as
              specified  in  a  URL.  FTP, FTPS, POP3, IMAP, SMTP, LDAP
              etc.

       ALL_PROXY [protocol://]<host>[:port]
              Sets the proxy server  to  use  if  no  protocol-specific
              proxy is set.

       NO_PROXY <comma-separated list of hosts>
              list  of  host names that shouldn't go through any proxy.
              If set to a asterisk '*' only, it matches all hosts.

              Since 7.53.0, this environment variable disable the proxy
              even   if   specify   -x,   --proxy   option.   That   is
              NO_PROXY=direct.example.com  curl  -x  http://proxy.exam‐
              ple.com http://direct.example.com accesses the target URL
              directly,   and   NO_PROXY=direct.example.com   curl   -x
              http://proxy.example.com     http://somewhere.example.com
              accesses the target URL through proxy.

wget

参照: man ページ

  • コマンドラインのオプションで指定する方法
  • 環境変数で指定する方法
  • 設定ファイルで指定する方法

がある。

コマンドラインのオプションで指定する場合

-e オプションを使って、指定する

wgetの-eオプション
$ wget -e http_proxy=http://proxy.example.com:8080 ...
Connecting to proxy.example.com:8080... connected.
Proxy request sent, awaiting response... 200 OK
...

man ページの記載は下の通り。

man wget(1)
OPTIONS
   <略>
   Basic Startup Options
       <略>
       -e command
       --execute command
           Execute command as if it were a part of .wgetrc.  A command thus
           invoked will be executed after the commands in .wgetrc, thus taking
           precedence over them.  If you need to specify more than one wgetrc
           command, use multiple instances of -e.

basic 認証が必要な場合は、
--proxy-user=user
--proxy-pasword=password
をオプションで指定する。

環境変数で指定する場合

  • 環境変数 *_proxy (http_proxy, https_proxy, ftp_proxy, no_proxy) で指定。
man wget(1)
ENVIRONMENT
       Wget supports proxies for both HTTP and FTP retrievals.  The standard
       way to specify proxy location, which Wget recognizes, is using the
       following environment variables:

       http_proxy
       https_proxy
           If set, the http_proxy and https_proxy variables should contain the
           URLs of the proxies for HTTP and HTTPS connections respectively.

       ftp_proxy
           This variable should contain the URL of the proxy for FTP
           connections.  It is quite common that http_proxy and ftp_proxy are
           set to the same URL.

       no_proxy
           This variable should contain a comma-separated list of domain
           extensions proxy should not be used for.  For instance, if the
           value of no_proxy is .mit.edu, proxy will not be used to retrieve
           documents from MIT.

設定ファイルで指定する場合

恒久的には ~/.wgetrc (個人)、/etc/wgetrc (システム) に記載。

~/.wgetrc or /etc/wgetrc
https_proxy = http://proxy.example.com:8080/
http_proxy = http://proxy.example.com:8080/
ftp_proxy = http://proxy.example.com:8080/
man wget(1)
FILES
       /etc/wgetrc
           Default location of the global startup file.

       .wgetrc
           User startup file.

man ページには ~/.wgetrc とは書いてないが、ホームディレクトリに .wgetrc を置くことで読み込まれる。

Splunk

参照: Configure splunkd to use your HTTP Proxy Server - Splunk Documentation

server.conf に設定

${SPLUNK_HOME}/etc/system/local/server.conf
[proxyConfig]
http_proxy = <string that identifies the server proxy. When set, splunkd sends all HTTP requests through this proxy server. The default value is unset.> 
https_proxy = <string that identifies the server proxy. When set, splunkd sends all HTTPS requests through the proxy server defined here. If not set, splunkd uses the proxy defined in http_proxy. The default value is unset.>  
no_proxy = <string that identifies the no proxy rules. When set, splunkd uses the [no_proxy] rules to decide whether the proxy server needs to be bypassed for matching hosts and IP Addresses. Requests going to localhost/loopback address are not proxied. Default is "localhost, 127.0.0.1, ::1">

docker

参照: Configure Docker to use a proxy server | Docker Documentation
参照: Control Docker with systemd | Docker Documentation

docker クライアントの proxy 設定は、~/.docker/config.json に設定。

~/.docker/config.json
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:3001",
     "httpsProxy": "http://127.0.0.1:3001",
     "ftpProxy": "http://127.0.0.1:3001",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}
Variable Dockerfile example docker run Example
HTTP_PROXY ENV HTTP_PROXY "http://127.0.0.1:3001" --env HTTP_PROXY="http://127.0.0.1:3001"
HTTPS_PROXY ENV HTTPS_PROXY "https://127.0.0.1:3001" --env HTTPS_PROXY="https://127.0.0.1:3001"
FTP_PROXY ENV FTP_PROXY "ftp://127.0.0.1:3001" --env FTP_PROXY="ftp://127.0.0.1:3001"
NO_PROXY ENV NO_PROXY "*.test.example.com,.example2.com" --env NO_PROXY="*.test.example.com,.example2.com"

(Configure Docker to use a proxy server | Docker Documentationより引用)

dockerd の proxy 設定は、systemctl 側で行う。(daemon.json では設定しない)

/etc/systemd/system/docker.service.d/http-proxy.conf (新規作成)
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"
Environment="HTTPS_PROXY=https://proxy.example.com:8080/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

設定後は、dockerd の再起動。

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

設定の確認。

$ systemctl show --property=Environment docker
Environment=HTTP_PROXY=http://proxy.example.com:8080/