用Nginx服务两个Django应用

问题描述 投票:0回答:1

我有一个使用Nginx和Gunicorn的Django应用,运行在以下平台上 mysite.com. 我想部署第二个Django应用,以便能在 mysite.comsecondapp 而我的第一个应用还只是通过 mysite.com.

我按照从DigitalOcean的教程和我的当前配置的应用程序1是。

等systemdsystemgunicorn.socket.

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

等systemdsystemgunicorn.service。

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

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

etcnginxsites-availablemyproject。

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/sammy/myprojectdir;
    }

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

我的问题是:我是否需要创建2个单独的 .socket.服务 我的App 2的文件,我如何使App 2可以通过以下方式访问 mysite.comsecondapp ?

django nginx gunicorn
1个回答
1
投票

是的,你需要创建2个独立的 .socket.service 文件,然后你的Nginx配置就会像

然后你的Nginx配置就会像这样。

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /static/ {
        root /home/sammy/myprojectdir;
    }
    location = /secondapp/static/ {
        root /home/sammy/myprojectdir-app2;
    }


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

注意,你必须配置你的第二个应用程序在正确的位置搜索静态文件。

更新。

第二个应用的所有路由也应该从原来的 /route_path//secondapp/route_path/.

© www.soinside.com 2019 - 2024. All rights reserved.