DEV Community

Cover image for Docker 201: Use NGINX as a Reverse Proxy for NodeJS Server in 2020! 📦 🙌 (practical guide)

Docker 201: Use NGINX as a Reverse Proxy for NodeJS Server in 2020! 📦 🙌 (practical guide)

Shahjada Talukdar on March 02, 2020

I will skip "What/Why Docker?" part to make it straight to the point! 🤝 Goal: We are gonna use NGINX as a Reverse Proxy for a NodeJS Server. ⧚ Fo...
Collapse
 
reggietheroman profile image
reggietheroman

This is really interesting. Could you eli5 why one would need to use a reverse proxy at all?

Collapse
 
maximization profile image
Maxim Orlov

One reason is, you have multiple apps/websites hosted on the same server. Say you have a landing/marketing page at myproduct.com and a dashboard for user accounts at app.myproduct.com. They can't both listen port 80 (default http port). You set up a reverse proxy that listens to 80 and forwards traffic to the respective app based on the domain. The apps can listen to ports 3000 and 3001 for example.

Does that make sense?

Collapse
 
reggietheroman profile image
reggietheroman

I think so. Just so im sure i understood your explanation, internally they use 3000 and 3001, and when people hit up myproduct.com and app.myproduct.com, that goes to nginx which then forwards the request to the correct ports?

Thread Thread
 
maximization profile image
Maxim Orlov

Correct!

Thread Thread
 
reggietheroman profile image
reggietheroman

awesome. thanks so much!

Collapse
 
destro_mas profile image
Shahjada Talukdar

This is a really nice article-
cloudflare.com/learning/cdn/glossa...

Collapse
 
tawsbob profile image
Dellean Santos

nice article, just to add more information --link flag is a legacy feature of Docker

Collapse
 
destro_mas profile image
Shahjada Talukdar

Thanks, I will update accordingly 👍

Collapse
 
tawsbob profile image
Dellean Santos

Great 😃

Collapse
 
mrnonz profile image
Nontawat Numor

I think it's will make people confuse between Proxy Vs Reverse Proxy.

If you agree with me please correct these.

Collapse
 
destro_mas profile image
Shahjada Talukdar

Actually, Reverse proxy is the specific term. I will update

Collapse
 
dancsee89 profile image
Daniel Molnar

In the next round, do this with https ;) :D

Collapse
 
destro_mas profile image
Shahjada Talukdar

Will try,
I have a lot of things in my queue 😀

Collapse
 
sani071 profile image
MD. Sani Mia

very helpful article .. Thanks for sharing
1

Collapse
 
destro_mas profile image
Shahjada Talukdar

Welcome!

Collapse
 
luiscarlosb3 profile image
Luis Carlos Galvão de Oliveira

nice article, have you any tips about static files? I've been some problems with css and js front end scripts locations

Collapse
 
rhernandog profile image
Rodrigo Hernando

What worked for me was to create a different folder with nginx and use docker compose in order to run and connect everything at the same time. It works fine in development. Here is the repo I have so far:
github.com/rhernandog/docker-expre...

Since I just started with docker a couple of weeks ago, I can't get it to work on production though. I can make it work with the static files but I can't find a way to start the express API server.

If someone could lend me a hand I'd appreciate it. Here is how my Dockerfile looks so far:

FROM nginx AS static
WORKDIR /app
COPY ./client /app

FROM node:12-alpine AS api
WORKDIR /app
COPY ./server/package.json /app
RUN npm install
COPY ./server /app

FROM nginx
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=static /app /usr/share/nginx/html
EXPOSE 80

If I remove the middle section (all the node part) it works fine, but as I said I can't find a way to start the express server.

If you user the code in the repo, running docker-compose up --build is going to work fine, as long as you provide your own mongo database of course.

Collapse
 
chiragshahklc profile image
Chirag Shah

If you haven't gave a chance, try Caprover. It's based on docker and very very simple to use.
It uses docker + nginx reverse proxy + let's encrypt.
caprover.com/