Run multiple django project with nginx and gunicorn

别说谁变了你拦得住时间么 提交于 2020-01-01 03:24:26

问题


I am using Ubuntu 18 servers and using nginx with gunicorn I follow Digitalocean tutorial for server setup. I successfully did for one project but now I need to run multiple projects under by server.

Here is my gunicorn setup

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

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? I want to configure my nginx setup for access project like this

192.168.11.252/firstProject

192.168.11.252/secoundproject

I try a few googles but not help me more.


回答1:


You use a proxy_pass with two different sockets. 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 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.



来源:https://stackoverflow.com/questions/54375834/run-multiple-django-project-with-nginx-and-gunicorn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!