简体   繁体   中英

nginx server block + docker, how does back and front communicate

My application is divided into a backend and frontend docker container which are running in digital ocean server. I purchased a domain and inserted the routes provided from digital ocean into my namecheap DNS. I am using nginx server block to route my frontend to the server and would like it to communicate to my backend docker container. I am currently watching this tutorial from faraday.

My frontend container is running on localhost:3000 and my backend is running on localhost:5000 ; And i've set the ports to run when the location is server_name/. How will my nginx server block know whether it's loading the frontend or backend to the domain since both are expected to run proxy_pass at location /?

I want to display the front onto server_name provided but still able to access my backend

server{
    server_name newlife.life;
    access_log /var/log/nginx/st-access.log
    error_log /var/log/nginx/st-error.log debug
    
    location / {
        proxy_pass http://localhost:3000;
    }
    location / {
        proxy_pass http://localhost:5000;
    }

}

Your frontend can make the backend calls on a different subpath. These requests will arrive at nginx and then nginx can proxy them to backend by rewriting the URL using the http_rewrite module.

See https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Example:

location /backend {
    proxy_pass localhost:5000;
    rewrite  ^/backend/(.*)  /$1 break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM