curl 无法连接到主机

2016-08-28 12:03:00 +08:00
 tmackan

最近部署一个 django 项目, virtualenv + supervisor + unicorn

在服务器上 cmd 执行 curl '127.0.0.1:8077'可以访问

在服务器上 cmd 执行 curl '服务器 IP:8077'显示 curl: (7) Failed connect to 120.27.202.78:8077; 拒绝连接

执行 netstat -apn|grep 80 显示

tcp        0      0 127.0.0.1:8077          0.0.0.0:*               LISTEN      10599/python3.4
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10642/nginx: master

然后 nginx 的配置

server {
     listen 80;

     server_name 服务器 IP;
     access_log /var/log/LandsBlog/access.log;
     error_log /var/log/LandsBlog/error.log;

     location / {
         proxy_pass http://127.0.0.1:8077;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
     location = /50x.html {
              root html;
     }
     location /static/ {
              alias /alidata/websites/LandsBLog/LandsBlog/Blog/static/;
              index index.html index.htm;
     }
     location /media/ {
              alias /alidata/websites/LandsBLog/LandsBlog/Blog/upload/;
     }

}

求解,谢谢!

6950 次点击
所在节点    Python
14 条回复
youKnowDai
2016-08-28 12:27:42 +08:00
不是把本地服务反代到 80 端口了吗
tmackan
2016-08-28 12:52:45 +08:00
ok 解决了,我的 unicorn 绑定的地址,应该填写为外网
laoyur
2016-08-28 17:32:23 +08:00
有点没明白,看你 op 的内容,猜想你是想用 nginx 把 8077 反代到外网 80 ?
可看你#2 的回复,解决方法又是把 gunicorn 绑定到外网,那到底用没用 nginx 啊
tmackan
2016-08-28 18:15:54 +08:00
@laoyur 你好,我本意是要 nginx 的。那该如何做啊?
coolloves
2016-08-28 18:48:34 +08:00
不改动的情况下,直接 curl IP 就行,不加端口
tmackan
2016-08-28 19:27:54 +08:00
@coolloves 咳,现在 supervisor 一直起不来了= =
laoyur
2016-08-28 20:50:00 +08:00
> 在服务器上 cmd 执行 curl '服务器 IP:8077'显示 curl: (7) Failed connect to 120.27.202.78:8077; 拒绝连接
那是因为你 gunicorn 绑定的是 127.0.0.1:8077 ,并没有绑 0.0.0.0:8077 ,所以当然通过外网 ip:8077 不通了

gunicorn 绑 127.0.0.1:8077 不变
nginx 配置类似:
listen 80;

location / {
try_files $uri @proxy_to_django_app;
}

location @proxy_to_django_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://127.0.0.1:8077;
}
laoyur
2016-08-28 20:52:23 +08:00
@tmackan 正好刚写了个 Ubuntu 下的 Supervisor 一键安装配置脚本,有兴趣可以试用下:
https://laoyur.com/archives/853/
tmackan
2016-08-28 21:41:27 +08:00
@laoyur 感谢,因为我 settings 配置下面 ALLOWED_HOSTS 设置错了,所以 supervisor 起不来,现在好了= =
julyclyde
2016-08-29 12:30:43 +08:00
@tmackan allowed_hosts 是 django 的选项吧。你遇到的问题是 gunicorn 的选项设置错误
tmackan
2016-08-29 12:57:58 +08:00
@julyclyde 不好意思,你从哪里看出是 gunicorn 选项设置错误
julyclyde
2016-08-29 13:47:26 +08:00
@tmackan 从 netstat 看出
tmackan
2016-08-29 16:45:12 +08:00
@julyclyde 不太懂啊,我绑定的 127.0.0.1:8077 有问题吗? bind
julyclyde
2016-09-01 11:17:47 +08:00
@tmackan bind 127.0.0.1 显然是有问题的啊,别的机器连不过来。你的 nginx 在别的机器的话,就会稳定出现 502

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/302245

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX