使用 nginx 和 gunicorn 运行多个 django 项目

Ant*_*ntu 9 django nginx gunicorn nginx-config

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

这是我的gunicorn 设置

命令:

sudo nano /etc/systemd/system/gunicorn.service
Run Code Online (Sandbox Code Playgroud)

文件:

[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
Run Code Online (Sandbox Code Playgroud)

这也是我的nginx 设置

命令:

sudo nano /etc/nginx/sites-available/helpdesk
Run Code Online (Sandbox Code Playgroud)

文件:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在如何在以下IP下添加另一个项目?我想为这样的访问项目配置我的 nginx 设置

192.168.11.252/firstProject

192.168.11.252/secoundproject
Run Code Online (Sandbox Code Playgroud)

我尝试了一些谷歌,但没有帮助我更多。

2ps*_*2ps 7

您将 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; } }
Run Code Online (Sandbox Code Playgroud)

这里的繁重工作是通过 nginxrewrite指令实现的,它会让您的应用程序将 url 视为 url 之后firstProject或url 中的所有内容secondProject