最近部署一个 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/; } }
求解,谢谢!
![]() | 1 youKnowDai 2016-08-28 12:27:42 +08:00 不是把本地服务反代到 80 端口了吗 |
2 tmackan OP ok 解决了,我的 unicorn 绑定的地址,应该填写为外网 |
3 laoyur 2016-08-28 17:32:23 +08:00 有点没明白,看你 op 的内容,猜想你是想用 nginx 把 8077 反代到外网 80 ? 可看你#2 的回复,解决方法又是把 gunicorn 绑定到外网,那到底用没用 nginx 啊 |
![]() | 5 sparkssssssss 2016-08-28 18:48:34 +08:00 via Android 不改动的情况下,直接 curl IP 就行,不加端口 |
7 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; } |
8 laoyur 2016-08-28 20:52:23 +08:00 |
9 tmackan OP @laoyur 感谢,因为我 settings 配置下面 ALLOWED_HOSTS 设置错了,所以 supervisor 起不来,现在好了= = |
10 julyclyde 2016-08-29 12:30:43 +08:00 @tmackan allowed_hosts 是 django 的选项吧。你遇到的问题是 gunicorn 的选项设置错误 |
14 julyclyde 2016-09-01 11:17:47 +08:00 @tmackan bind 127.0.0.1 显然是有问题的啊,别的机器连不过来。你的 nginx 在别的机器的话,就会稳定出现 502 |