Home > Software engineering >  (Bad Gateway) ERROR 502 Nginx, Gunicorn, Django DRF, Vue.js
(Bad Gateway) ERROR 502 Nginx, Gunicorn, Django DRF, Vue.js

Time:09-30

I have deployed ecommerce made with Django and Vue.js on a VPS. Everything is working fine, but when I try to use POST method, Error 502 appears in console and nothing happens. The API is able to show products from Django Admin, but I can't post anything from the frontend. It always gives me this Error 502 problem. I have been trying to solve this for a few day already and still don't get what is wrong. Hope you can help. Thank you!

upstream perulab_app_server {
    server unix:/webapps/perulab/venv/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 8000;
    listen [::]:8000;
    server_name 172.16.7.52;

    client_max_body_size 40M;

    location / {
        root /webapps/perulab/web-frontend/dist;
        try_files $uri /index.html;
        index index.html index.htm;
    }

    location /static/ {
        root /webapps/perulab/web-backend;
    }

    location /media/ {
        root /webapps/perulab/web-backend;
    }
        
    location /api/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://perulab_app_server/api/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    location /admin/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://perulab_app_server/admin/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
}

When I try to run gunicorn --bind 172.16.7.52:8000 core.wsgi this is what appears:

[2021-09-27 14:55:59 -0500] [230558] [INFO] Starting gunicorn 20.1.0
[2021-09-27 14:55:59 -0500] [230558] [ERROR] Connection in use: ('172.16.7.52', 8000)
[2021-09-27 14:55:59 -0500] [230558] [ERROR] Retrying in 1 second.
[2021-09-27 14:56:00 -0500] [230558] [ERROR] Connection in use: ('172.16.7.52', 8000)
[2021-09-27 14:56:00 -0500] [230558] [ERROR] Retrying in 1 second.
[2021-09-27 14:56:01 -0500] [230558] [ERROR] Connection in use: ('172.16.7.52', 8000)
[2021-09-27 14:56:01 -0500] [230558] [ERROR] Retrying in 1 second.
[2021-09-27 14:56:02 -0500] [230558] [ERROR] Connection in use: ('172.16.7.52', 8000)
[2021-09-27 14:56:02 -0500] [230558] [ERROR] Retrying in 1 second.
[2021-09-27 14:56:03 -0500] [230558] [ERROR] Connection in use: ('172.16.7.52', 8000)
[2021-09-27 14:56:03 -0500] [230558] [ERROR] Retrying in 1 second.
[2021-09-27 14:56:04 -0500] [230558] [ERROR] Can't connect to ('172.16.7.52', 8000)

It seems like something is occupying the space, so in order for this command to work I first need to kill all of the processes and then run this command. After that the whole web app goes down.

I tried to run everything on the same port. Maybe that is the problem, I don't know how to set up Nginx properly for this.

UPDATE: After testing everything, it looks like API is working only with Views Classes that don't send emails. Any view that has send_mail() generates this Error 502 problem. I still don't know how to solve it.

CodePudding user response:

I was able to solve that problem. In this case, the setup above was fine. All APIs were working except the ones that sent emails. So, the problem was a mail server, where I wasn't able to send emails from DNS. Once that was solved, everything started working fine.

  • Related