1. 程式人生 > >[心得]http服務不通的解決之道

[心得]http服務不通的解決之道

背景

在公司的環境下搭建了一套pip私服。需要跨域訪問。走了彎路,希望後來者記住centOS下的防火牆可能做怪。

乾貨

自己想盡各種招數,就是如下:
本機:

curl http://localhost:3141
<html><head><title>Welcome to pypiserver!</title></head><body>
<h1>Welcome to pypiserver!</h1>
<p>This is a PyPI compatible package index serving 0 packages.</p
>
<p> To use this server with pip, run the the following command: <blockquote><pre> pip install --extra-index-url http://x.x.x.x:3141/ PACKAGE [PACKAGE2...] </pre></blockquote></p> <p> To use this server with easy_install, run the the following command: <blockquote
>
<pre> easy_install -i http://x.x.x.x:3141/simple/ PACKAGE </pre></blockquote></p> <p>The complete list of all packages can be found <a href="/packages/">here</a> or via the <a href="/simple/">simple</a> index.</p> <p>This instance is running version 1.2.0 of the <a
href="http://pypi.python.org/pypi/pypiserver">
pypiserver</a> software.</p> </body></html>

遠端:

curl 'http://x.x.x.x:3141'
curl: (7) Failed to connect to x.x.x.x port 3141: Connection refused

嘗試了一整天,google查到一種方法,伺服器上做一個server來轉發到3141埠。與其這樣,還不如直接用nginx做轉發:

server {
    server_name pypi.python.cm;
    listen 80;
    set $remote_adds $http_x_forwarded_for;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header Access-Control-Allow-Methods GET,POST;

    location / {
        proxy_pass http://127.0.0.1:3141;
    }
}

結果也不work。

最後,在給同事描述問題時,我得到了一句話的提醒,不用nginx轉發也行,可能是一個埠需要開啟。正是這個關鍵的提醒指明瞭方向。
我把部署機上所有防火牆全關掉。就看到了曙光:

curl 'http://x.x.x.x:3141'
<html><head><title>Welcome to pypiserver!</title></head><body>
<h1>Welcome to pypiserver!</h1>
<p>This is a PyPI compatible package index serving 0 packages.</p>

<p> To use this server with pip, run the the following command:
<blockquote><pre>
pip install --extra-index-url http://x.x.x.x:3141/ PACKAGE [PACKAGE2...]
</pre></blockquote></p>

<p> To use this server with easy_install, run the the following command:
<blockquote><pre>
easy_install -i http://x.x.x.x:3141/simple/ PACKAGE
</pre></blockquote></p>

<p>The complete list of all packages can be found <a href="/packages/">here</a>
or via the <a href="/simple/">simple</a> index.</p>

<p>This instance is running version 1.2.0 of the
  <a href="http://pypi.python.org/pypi/pypiserver">pypiserver</a> software.</p>
</body></html>

好了,問題轉換為如何配置防火牆,開啟3141埠了:
重啟後永久生效:

chkconfig iptables on/off
service iptables start/stop

修改 /etc/sysconfig/iptables 檔案,新增以下內容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3141 -j ACCEPT   #允許80埠通過防火牆
備註:如果把這兩條規則新增到防火牆配置的最後一行,導致防火牆啟動失敗,正確的應該是新增到預設的22埠這條規則的下面