Nginx, Django y Gunicorn: redireccionamiento del subdominio a la aplicación

Nginx, Django y Gunicorn: redireccionamiento del subdominio a la aplicación

Hice una búsqueda exhaustiva sobre el tema pero nada pudo ayudarme.

Actualmente estoy construyendo un sitio Django y usando Nginx para invertir el proxy de Gunicorn.

Estoy intentando separar mis aplicaciones en subdominios. (blog.ejemplo.com, administrador.ejemplo.com, usuario.ejemplo.com, etc.)

Me ocupé de los registros DNS y están funcionando en consecuencia ya que mi proyecto inicial usaba solo Nginx.

Puedo acceder a las aplicaciones navegando a la "subcarpeta" example.com/blog.

La configuración se ve así:

/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/sitios-disponibles/ejemplo:

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

Agregué otro bloque de servidor para blog.example.com y agregué /blog en la línea proxy_pass: proxy_pass http://unix:/run/gunicorn.sock/blog;con la esperanza de que se redirija a la carpeta de la aplicación.

Soy nuevo en el método socket y no sé cómo lograr la redirección.

Soy consciente de que Nginx debería encargarse de la redirección y todo, pero no sé cómo continuar.

Cualquier ayuda es muy apreciada.

Respuesta1

Podrías tener un servicio gunicorn por aplicación:

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

[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.servicio

[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

Luego apunte nginx a los .sockarchivos respectivos:

/etc/nginx/sitios-disponibles/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/sitios-disponibles/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;
    }
}

Respuesta2

Deberías usararchivos de servicio y socket separadospara cada aplicación y usohosts explícitos en lugar de sockets Unix.

Cambie los términos como unix:/run/blogcon algo como 127.0.0.1:8000en suenchufeyservicioarchivos. WorkingDirectoryEl parámetro debe ser el mismo en todos los archivos de servicio, debe indicar que el directorio contiene manage.py. Finalmente deberías configurar proxy_pass http://127.0.0.1:8000/blog/;en tunginxconfiguración.

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

Unit]
Description=Gunicorn socket for blog.example.comg

[Socket]
ListenStream=8000

[Install]
WantedBy=sockets.target

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

[Unit]
Description=gunicorn daemon for blog.example.com
Requires=blog.example.com.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 127.0.0.1:8000 \
          project.wsgi:application

[Install]
WantedBy=multi-user.target

/etc/nginx/sitios-disponibles/blog-ejemplo:

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://127.0.0.1:9000/blog/;
    }
} 

información relacionada