首页 > 解决方案 > 如何在生产环境中使用 gunicorn 和 nginx 托管 2 个 Django 应用程序

问题描述

您好,我想使用 Gunicorn 和 Nginx 托管 2 个 djagno 网站,但我不知道该怎么做这是我第一次在一个服务器和 2 个域中托管 2 个 django 网站,所以请告诉我如何托管 2 个 django 网站。这是位于 /var/www/site1 的我的 1 个文件,这是我的 2 个文件 /var/www/site2

标签: pythondjangonginxgunicornweb-hosting

解决方案


假设您在端口 8081 和 8082 上运行 gunicorn 实例,您有两个选择:

选项 1:子域

要使用子域通过 NGINX 进行反向代理,请参阅此答案

http://site1.example.com/重定向到站点 1,http ://site2.example.com/ 重定向到站点 2。

NGINX 配置:

server {
    listen 80;
    server_name site1.example.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}
server {
    listen 80;
    server_name site2.example.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}

选项 2:地点

http://example.com/site1/现在重定向到站点 1 和http://example.com/site2/到站点 2。

NGINX 配置:

server {
    listen 80;
    server_name example.com;
    location /site1/ {
        proxy_pass http://127.0.0.1:8081;
    }
    location /site2/ {
        proxy_pass http://127.0.0.1:8082;
    }
}

编辑 - 选项 3:域

您也可以像选项 1 一样为不同的站点使用不同的域。

http://site1.com/重定向到站点 1,http ://site2.com/ 重定向到站点 2。

NGINX 配置:

server {
    listen 80;
    server_name site1.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}
server {
    listen 80;
    server_name site2.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}

注意:这些是最低限度的设置,可能需要额外的配置才能与您的特定服务一起使用。


推荐阅读