简体   繁体   English

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

[英]Run multiple django project with nginx and gunicorn

I am using Ubuntu 18 servers and using nginx with gunicorn I follow Digitalocean tutorial for server setup.我正在使用Ubuntu 18 台服务器并使用nginxgunicorn我按照Digitalocean教程进行服务器设置。 I successfully did for one project but now I need to run multiple projects under by server.我成功地完成了一个项目,但现在我需要在服务器下运行多个项目。

Here is my gunicorn setup这是我的gunicorn 设置

command:命令:

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

file:文件:

[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

And also here is my nginx setup这也是我的nginx 设置

command:命令:

sudo nano /etc/nginx/sites-available/helpdesk

file:文件:

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;
    }
}

Now how can I add another project under the following IP ?现在如何在以下IP下添加另一个项目? I want to configure my nginx setup for access project like this我想像这样为访问项目配置我的 nginx 设置

192.168.11.252/firstProject

192.168.11.252/secoundproject

I try a few googles but not help me more.我尝试了一些谷歌,但没有帮助我更多。

You use a proxy_pass with two different sockets . 您使用带有两个不同套接字的proxy_pass。 Setup gunicorn on the first project to listen on a socket called first_project.sock, and setup gunicorn on the second project to listen on a socket called second_project.sock. 在第一个项目上设置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; } }

The heavy lifting here is by the nginx rewrite directive which will let your app think of the url as everything after firstProject or secondProject in the url. 这里的繁重工作是通过nginx rewrite指令,它会让你的应用程序将url视为url中firstProjectsecondProject之后的所有内容。

It worked for me with this config:它使用此配置对我有用:

1. sudo nano /etc/systemd/system/firstsite.socket

    [Unit]
    Description=firstsite socket
    
    [Socket]
    ListenStream=/run/firstsite.sock
    
    [Install]
    WantedBy=sockets.target

2. sudo nano /etc/systemd/system/secondsite.socket

    [Unit]
    Description=secondsite socket
    
    [Socket]
    ListenStream=/run/secondsite.sock
    
    [Install]
    WantedBy=sockets.target

3. sudo nano /etc/systemd/system/firstsite.service

    [Unit]
    Description=gunicorn daemon
    Requires=firstsite.socket
    After=network.target
    
    [Service]
    User=non-root-user
    Group=www-data
    WorkingDirectory=/home/non-root-user/firstsite/
    ExecStart=/home/non-root-user/firstsite/env/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/firstsite.sock \
    firstsite.wsgi:application
    
    [Install]
    WantedBy=multi-user.target

4. sudo nano /etc/systemd/system/secondsite.service
    [Unit]
    Description=gunicorn daemon
    Requires=secondsite.socket
    After=network.target
    
    [Service]
    User=non-root-user
    Group=www-data
    WorkingDirectory=/home/non-root-user/secondsite/
    ExecStart=/home/non-root-user/secondsite/env/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/secondsite.sock \
    secondsite.wsgi:application
    
    [Install]
    WantedBy=multi-user.target

5. NGINX firstsite.ru

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


6. NGINX secondsite.ru


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


sudo systemctl start firstsite.socket
sudo systemctl enable firstsite.socket
sudo systemctl status firstsite.socket
sudo systemctl restart firstsite

sudo systemctl start secondsite.socket
sudo systemctl enable secondsite.socket
sudo systemctl status secondsite.socket
sudo systemctl restart secondsite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM