【问题标题】:How to host 2 Django Application using gunicorn & nginx in Production如何在生产环境中使用 gunicorn 和 nginx 托管 2 个 Django 应用程序
【发布时间】:2021-09-22 03:31:42
【问题描述】:

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

【问题讨论】:

  • 解决方案是为每个 Django 站点配置一个单独的 Gunicorn 实例(在不同的端口上运行每个实例),然后使用 NGINX 作为反向代理将特定的 URI 重定向到每个站点。例如,您可以将 site1.example.com 指向一个站点的根目录,将 site2.example.com 指向另一个站点,或者使用 example.com/site1/ 和 example.com/site2/。

标签: python django nginx gunicorn web-hosting


【解决方案1】:

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

选项 1:子域

要使用子域通过 NGINX 进行反向代理,请参阅 this answer

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

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

【讨论】:

    猜你喜欢
    • 2013-06-12
    • 1970-01-01
    • 2017-01-13
    • 2013-10-03
    • 2020-05-31
    • 2014-10-17
    • 2018-09-21
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多