【发布时间】:2015-07-17 21:21:54
【问题描述】:
请帮帮我,我使用 django + gunicorn + nginx,但出现错误 nginx:连接到上游时连接()失败(111:连接被拒绝) 我认为问题是9000端口?如何解决这个问题呢? 502 Bad Gateway
/var/log/nginx/error.log
2015/05/07 04:57:28 [error] 11439#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 5.178.184.108, server: 45.55.254.196, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:9000/favicon.ico", host: "45.55.254.196"
sudo netstat -plntu
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 28846/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 946/sshd
tcp6 0 0 :::80 :::* LISTEN 28846/nginx
tcp6 0 0 :::22 :::* LISTEN 946/sshd
我的配置
/etc/nginx/sites-available/hotel
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name 45.55.254.196;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/hotel/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/hotel/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/local/lib/python3.4/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
/etc/init/gunicorn.conf
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
respawn
setuid django
setgid django
chdir /home/django
exec gunicorn \
--name=hotel \
--pythonpath=hotel \
--bind=127.0.0.1:9000 \
--config /etc/gunicorn.d/gunicorn.py \
hotel.wsgi:application
/etc/gunicorn.d/gunicorn.py
from multiprocessing import cpu_count
from os import environ
def max_workers():
return cpu_count() * 2 + 1
max_requests = 1000
worker_class = 'gevent'
workers = max_workers()
【问题讨论】:
-
如果直接连接到 9000 端口会发生什么?
-
@Pixou 坦克回复,我尝试连接 telnet 127.0.0.1 9000 并获得结果 Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection denied 我认为问题是这样的,你有其他意见吗?有什么问题?
-
所以这意味着你有 gunicorn 的问题,而不是 nginx。我真的不是gunicorn专家,但我会看日志。也许你也可以尝试运行一个 django 开发服务器来检查你的 django 是否运行顺利
-
@Pixou 当我运行这个命令“gunicorn hotel.wsgi:application -b 127.0.0.1:9000”它工作
-
那我猜丹尼尔的回答应该有效!
标签: python django ubuntu python-3.x nginx