【问题标题】:Gunicorn not binding with nginx using socketGunicorn 未使用套接字与 nginx 绑定
【发布时间】:2018-08-21 20:53:07
【问题描述】:

这就是我的配置的样子

#/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=django
Group=www-data
WorkingDirectory=/home/django/testapp
ExecStart=/home/django/venvs/testenv/bin/gunicorn --workers 3 --bind unix:/home/django/testapp/testapp.sock testapp.wsgi:application

[Install]
WantedBy=multi-user.target

这是我的 nginx 配置:

# /etc/nginx/sites-enabled/testapp
server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/django/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/django/testapp/testapp.sock;
    }
}

我注意到,如果我从控制台运行此命令:

/home/django/venvs/testenv/bin/gunicorn --workers 3 --bind unix:/home/django/testapp/testapp.sock testapp.wsgi:application

然后它显示就像它在控制台中工作一样,但 URL 仍然无法访问。

这是输出的样子:

[2016-07-11 16:29:41 -0400] [1545] [INFO] Starting gunicorn 19.6.0
[2016-07-11 16:29:41 -0400] [1545] [INFO] Listening at: unix:/home/django/testapp/testapp.sock (1545)
[2016-07-11 16:29:41 -0400] [1545] [INFO] Using worker: sync
[2016-07-11 16:29:41 -0400] [1548] [INFO] Booting worker with pid: 1548
[2016-07-11 16:29:41 -0400] [1550] [INFO] Booting worker with pid: 1550
[2016-07-11 16:29:41 -0400] [1551] [INFO] Booting worker with pid: 1551

但是,我仍然无法访问服务器的 url。我尝试使用端口 80、8000 和 9001 打开 url,但都没有工作。 (nginx 在 80 上运行)。

我正在使用 python3.5 和 gunicorn 19.6.0。

有人有什么建议吗?非常感谢!

【问题讨论】:

标签: python-3.x nginx gunicorn


【解决方案1】:

我可以帮你配置 nginx。不确定插座,但对我来说看起来不错。

你需要:

# /etc/nginx/sites-available/testapp
server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/django/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/django/testapp/testapp.sock;
    }
}

然后创建从站点可用到站点启用使用的链接:

ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/testapp

运行命令:

sudo service nginx configtest 

它测试您的 nginx 配置文件是否正常运行。如果提示[OK]则:

sudo servie nginx restart

另外,nginx 的思想是监听域名,而不是端口。这就是为什么我会建议你修改你的主机文件 Windows C:\Windows\System32\drivers\etc\hosts 添加如下一行: 127.0.0.1 测试应用 并修改 nginx 配置文件:

# /etc/nginx/sites-available/testapp
server {
    server_name testapp;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/django/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/django/testapp/testapp.sock;
    }
}

重复步骤检查 nginx 配置文件并重启 nginx

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 2021-01-06
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多