S
S
s75002021-06-26 22:27:44
Nginx
s7500, 2021-06-26 22:27:44

How to set up nginx to interact with docker?

I put a backing on a remote server. I use nginx for the server. Beck is running in a docker container, which is lifted with docker-compose. What is the problem: I raise nginx, it pulls up the front (everything is ok here). But the problem appears when you need to use the
Nginx backend as if it does not see the urls that the backend has and sends 404. The request does not reach the docker, but there is a request in the nginx logs.
I suspect that the problem is somewhere in the ports, but I can’t figure out where
the part of the nginx config is:

upstream backend {
        server 127.0.0.1:8000;
}
server {
        server_name mysite.com;
 
        access_log /var/log/nginx/mysite.com.access.log;
        error_log /var/log/nginx/mysite.com.error.log;

        root /srv/www/mysite-frontend/build/;

        location / {
                try_files $uri $uri/ /;
                index index.html;
        }

        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Pragma,Expires,x-csrftoken' always;
        add_header "Access-Control-Allow-Origin" $http_origin always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT' always;
        add_header X-Frame-Options "SAMEORIGIN";

        location /api/ {

                proxy_pass http://127.0.0.1:8000/;
                proxy_set_header Host $server_name;
                proxy_set_header X-Forwarded-Proto $scheme;
                keepalive_timeout 650;
        }
        location /django-static/ {
                alias /srv/www/mysite-backend/static/;
        }


    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

}
server {
    listen 80;
}


in docker, the internal port is forwarded to 8000:443

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2021-06-26
@ky0

Are you sure that's how you need it?

location /api/ {
proxy_pass http://127.0.0.1:8000/;

In this configuration, the request fails inside not /api, but simply /. Otherwise, remove the slash at the end of the directive proxy_pass.
PS - since you have upstream defined, it's strange not to use it in proxy_pass:
proxy_pass http://backend/;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question