API connections from react(Axios) to Nginx (Docker/Django) doesn't work. ERR_CONNECTION_REFUSED

Viewed 25

I have a problem with connecting my fronted(react/Axios) to backend(Django) data hosted on VPS using Nginx and docker. The problem is weird because I can connect to API by Postman. The issue appears when I try to get data from my frontend(localhost:3000) or from netlify app.

There is Nginx code:

    upstream 127.0.0.1 {
    server django_gunicorn:8000;
}




server {
    listen 80;

    location / {
        proxy_pass http://127.0.0.1;
    }

    location /ws {
        proxy_pass http://127.0.0.1;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }


    location /static/ {
        alias /static/;
    }

    location /media/ {
        alias /code/media/;
    }

}

EDIT: I changed my server name to django_api and i added three more lines in location /, afterwards everything works.

upstream django_api {
        server django_gunicorn:8000;
}




server {
        listen 80;

        location / {
                proxy_pass http://django_api;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_redirect off;

        }

        location /ws {
                proxy_pass http://django_api;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }
0 Answers
Related