Nginx & Django & Gunicorn – Redirecting subdomain to app

djangogunicornnginxsubdomainUbuntu

I did an extensive search on the topic but nothing could help me.

I am currently building a Django site, and using Nginx to reverse proxy to Gunicorn.

I am trying to separate my apps into subdomains. (blog.example.com, admin.example.com, user.example.com etc.)

I took care of the DNS records and they are working accordingly since my initial project was using only Nginx.

I can access the apps by navigating to the "subfolder" example.com/blog.

The config looks like this:

/etc/systemd/system/gunicorn.socket:



[Unit]
Description=Gunicorn socket for example.com

[Socket]
ListenStream=/run/example.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service:

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

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

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/example:

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/example/project;
    }

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

I did add another serverblock for blog.example.com and added /blog on the proxy_pass line: proxy_pass http://unix:/run/gunicorn.sock/blog; hoping it will redirect to the app folder.

I am new to the socket method and do I don't know how to achieve the redirect.

I am aware that Nginx should handle the redirect and everything, but don't know to proceed further.

Any help is greatly appreciated.

Best Answer

You could have one gunicorn service per app:

/etc/systemd/system/blog.example.com.service

[Unit]
Description=gunicorn daemon for blog.example.com
Requires=gunicorn.socket
After=network.target

[Service]
User=example
Group=www-data
WorkingDirectory=/home/example/project/blog
ExecStart=/home/sammy/project/projectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/blog.example.com.sock \
          project.wsgi:application

[Install]
WantedBy=multi-user.target

/etc/systemd/system/admin.example.com.service

[Unit]
Description=gunicorn daemon for admin.example.com
Requires=gunicorn.socket
After=network.target

[Service]
User=example
Group=www-data
WorkingDirectory=/home/example/project/admin
ExecStart=/home/sammy/project/projectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/admin.example.com.sock \
          project.wsgi:application

[Install]
WantedBy=multi-user.target

Then point nginx to the respective .sock files:

/etc/nginx/sites-available/blog.example.com

server {
    listen 80;
    server_name blog.example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/example/project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/blog.example.com.sock;
    }
}

/etc/nginx/sites-available/admin.example.com

server {
    listen 80;
    server_name admin.example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/example/project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/admin.example.com.sock;
    }
}
Related Topic