背景
在公司的环境下搭建了一套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端口这条规则的下面