首页 文章

Gunicorn没有使用套接字与nginx绑定

提问于
浏览
2

这就是我的配置的样子

#/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 .

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

1 回答

  • 0

    我可以帮助你使用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 testapp并修改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

相关问题