0
votes

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;
    }

}
1
you can't expose on same location. What is your frontend technology? Is it single page app?Shashank V
Its a multipage app which uses authprovider to check for jwt tokens. Reason why i want the backend to run immediately is my frontend does a token check immediately when application starts.DustBoy
your frontend can make the backend calls on a different subpath and nginx can do URL rewriting when proxying those requests to the backend. nginx.org/en/docs/http/ngx_http_rewrite_module.htmlShashank V
ooh okay thats great to know thanks!DustBoy

1 Answers

1
votes

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;
}