django - 使用Nginx和Gunicorn运行多个Django项目

标签 django nginx gunicorn nginx-config

我正在使用 Ubuntu 18服务器,并使用 nginx gunicorn 我遵循Digitalocean教程进行服务器设置。我成功完成了一个项目,但是现在我需要在服务器下运行多个项目。

这是我的 Gunicorn设置

命令:

sudo nano /etc/systemd/system/gunicorn.service

文件:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=rfr
Group=www-data
WorkingDirectory=/home/rfr/helpdesk/helpdesk
ExecStart=/home/rfr/helpdesk/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          helpdesk.wsgi:application


[Install]
WantedBy=multi-user.target

这也是我的 nginx设置

命令:
sudo nano /etc/nginx/sites-available/helpdesk

文件:
server {
    listen 80;
    server_name 192.168.11.252;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /assets/ {
        root /home/rfr/helpdesk/helpdesk;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

现在如何在以下 IP 下添加另一个项目?我想像这样为访问项目配置Nginx设置
192.168.11.252/firstProject

192.168.11.252/secoundproject

我尝试了一些Google,但对我却没有更多帮助。

最佳答案

您可以将proxy_pass与一起使用两个不同的套接字。在第一个项目上设置gunicorn,以监听名为first_project.sock的套接字,在第二个项目上设置gunicorn,以监听名为second_project.sock的套接字。

gunicorn for first project
[Unit] Description=gunicorn for firstProject Requires=gunicorn.socket After=network.target [Service] User=rfr Group=www-data WorkingDirectory=/home/rfr/first_project/first_project ExecStart=/home/rfr/first_project/env/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/first_project.sock \ first_project.wsgi:application [Install] WantedBy=multi-user.target
gunicorn for second project
[Unit] Description=gunicorn for secondProject Requires=gunicorn.socket After=network.target [Service] User=rfr Group=www-data WorkingDirectory=/home/rfr/second_project/second_project ExecStart=/home/rfr/second_project/env/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/second_project.sock \ second_project.wsgi:application [Install] WantedBy=multi-user.target
nginx configuration
server { listen 80; server_name 192.168.11.252; location = /favicon.ico { access_log off; log_not_found off; } location /firstProject/assets/ { root /home/rfr/first_project/first_project; } location /secondProject/assets/ { root /home/rfr/second_project/second_project; } location /firstProject { include proxy_params; rewrite /firstProject(.*) $1; proxy_pass http://unix:/run/first_project.sock; } location /secondProject { include proxy_params; rewrite /secondProject(.*) $1; proxy_pass http://unix:/run/second_project.sock; } }

这里的繁重工作是由nginx rewrite指令完成的,它将使您的应用将url视为url中firstProjectsecondProject之后的所有内容。

关于django - 使用Nginx和Gunicorn运行多个Django项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375834/

相关文章:

Python Celery 线程、工作线程和 vCPU

nginx - 指定--restart开关后,为什么我的Nginx docker 容器无法启动

django - 我如何以受限访问用户的身份运行 uWSGI?

python - Nginx 如何为 Django 应用获取 SSL 证书

python - Gunicorn 服务环境文件格式?

python - 我可以删除迁移目录中的 django 迁移文件吗

python - 在所有管理页面中将模型字段设置为只读

python - Django 和 Python 6 兼容模块

django - 我不知道为什么 Nginx 服务器不工作

django - 在 Nginx 配置中设置自定义 header 并将其传递给 gunicorn