0

As the title says I want to host two different django project in the same droplet (NGINX, Gunicorn, ubuntu) with different subdomains. One will be our main site example.com. which is up and running and working perfectly. We want to host the staging site staging.example.com in the same droplet.

We have created new sockets and service files for the staging site and activated and enabled them but the issue is nginx still points to the files in main domain directory rather than the staging directory and hence we get this error below even though these domains have been added in the allowed hosts of settings.py of the staging site

DisallowedHost at / Invalid HTTP_HOST header: 'staging.example.com'. You may need to add >'staging.example.com' to ALLOWED_HOSTS

Here is our staging.guinicorn.service file

[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

Here is our staging.guicorn.socket file

[Unit]
Description=staging.gunicorn socket

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

[Install]
WantedBy=sockets.target

Lastly here is our nginx config

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

Any sort of help would be extremely appreciated!

2 Answers2

0

Both of your SSL configuration blocks are missing server_name directive. Therefore nginx uses the default virtual host for all requests, and in this case it is the first one that listens to port 443.

You need to add proper server_name directives to fix the issue.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

listen 443 ssl http2;

server_name example.com www.example.com;

listen 443 ssl http2;

server_name staging.example.com www.staging.example.com;

Do alter your nginx file in https section