在同一个 droplet 中托管两个具有不同子域的不同 django 项目(NGINX、Gunicorn、ubuntu)

在同一个 droplet 中托管两个具有不同子域的不同 django 项目(NGINX、Gunicorn、ubuntu)

正如标题所述,我想在同一个 droplet(NGINX、Gunicorn、ubuntu)中托管两个不同的 django 项目,并使用不同的子域。一个将是我们的主要站点 example.com。它已启动并运行良好。我们希望在同一个 droplet 中托管暂存站点 staging.example.com。

我们已经为暂存站点创建了新的套接字和服务文件,并激活并启用了它们,但问题是 nginx 仍然指向主域目录中的文件,而不是暂存目录中的文件,因此我们得到下面的错误,即使这些域已经添加到暂存站点的 settings.py 的允许主机中

DisallowedHost 位于 / 无效的 HTTP_HOST 标头:“staging.example.com”。您可能需要将 >“staging.example.com”添加到 ALLOWED_HOSTS

这是我们的 staging.guinicorn.service 文件

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

[Service]
User=admin
Group=www-data
WorkingDirectory=/home/admin/example1staging
ExecStart=/home/admin/example1staging/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/staging.gunicorn.sock djangoproject.wsgi:application

[Install]
WantedBy=multi-user.target

这是我们的 staging.guicorn.socket 文件

[Unit]
Description=staging.gunicorn socket

[Socket]
ListenStream=/run/staging.gunicorn.sock

[Install]
WantedBy=sockets.target

最后这是我们的 nginx 配置

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 302 https://$server_name$request_uri;
}

server {
    # SSL configuration

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate         /etc/ssl/cert.pem;
    ssl_certificate_key     /etc/ssl/key.pem;
    ssl_client_certificate /etc/ssl/cloudflare.crt;
    ssl_verify_client on;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;    

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/admin/example1;
    }

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

server {
    listen 80;
    listen [::]:80;
    server_name staging.example.com www.staging.example.com;
    return 302 https://$server_name$request_uri;
}

server {
    # SSL configuration

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate         /etc/ssl/cert.pem;
    ssl_certificate_key     /etc/ssl/key.pem;
    ssl_client_certificate /etc/ssl/cloudflare.crt;
    ssl_verify_client on;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;    

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/admin/example1staging;
    }

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

如能提供任何帮助我将非常感激!

答案1

您的两个 SSL 配置块都缺少server_name指令。因此,nginx 对所有请求都使用默认虚拟主机,在本例中,它是第一个侦听端口 443 的虚拟主机。

您需要添加适当的server_name指令来修复该问题。

答案2

监听443 ssl http2;

服务器名称 example.com www.example.com;

监听443 ssl http2;

服务器名称 staging.example.com www.staging.example.com;

修改 nginx 文件中的 https 部分

相关内容