A
A
Artem Karachev2018-05-19 13:55:39
Django
Artem Karachev, 2018-05-19 13:55:39

How to setup nginx for django backend and nuxt.js frontend?

Welcome all! Things are like this, nuxt is raised via pm2 on port 5006 in server-side rendering mode. And there is django on the same server, on port 5007 with the help of gunicorn, which serves only for api requests and for accessing the admin panel.
My task is to proxy all requests to the frontend through nginx, except for requests to /api/, /admin/ and, apparently, /static/. There is only one domain, a certificate from letsencrypt.
I tried to do this by stupidly writing different locations:

server {
    server_name test.com www.test.com;
    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;
    include /etc/nginx/snippets/letsencrypt.conf;

    location / {
        include proxy_params;
        proxy_pass http://localhost:5006;
    }

    location /api/ {
        include proxy_params;
        proxy_pass http://unix:/home/path/to/core.sock;
    }
    location /admin/ {
        include proxy_params;
        proxy_pass http://unix:/home/path/to/core.sock;
    }
    location /static/ {
        root /home/path/to/staticfolder;
    }
    return 301 https://$host$request_uri;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2 ipv6only=on;
        server_name test.com;
        include /etc/nginx/snippets/letsencrypt.conf;
        location / {
            include proxy_params;
            proxy_pass http://localhost:5006;
        }
        location /api/ {
            include proxy_params;
            proxy_pass http://unix:/home/path/to/core.sock;
        }
  location /admin/ {
            include proxy_params;
            proxy_pass http://unix:/home/path/to/core.sock;
        }
        location /static/ {
            root /home/path/to/staticfolder;
        }
        ssl_certificate /etc/path/to/fullchain.pem;
        ssl_certificate_key /etc/path/to/privkey.pem;
        ssl_trusted_certificate /etc/letsencrypt/live/test/fullchain.pem;
        include /etc/nginx/snippets/ssl.conf;
}

This configuration distributes nuxt, but when I request /api/ I get 502 Bad gateway from nginx. I want to use this bundle always and everywhere, so I would like to figure out how to configure it through nginx. Help, comrades, please)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question