Full NGINX Tutorial - Demo Project with Node.js, Docker
3 min read
1 month ago
Published on Jun 09, 2025
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
This tutorial will guide you through setting up NGINX as a reverse proxy for a Node.js application using Docker. You'll learn core concepts of NGINX, configure it for load balancing, and secure your connection with HTTPS. This hands-on approach will add valuable skills to your engineering toolkit.
Step 1: Understand NGINX and Its Uses
- NGINX is a high-performance web server often used as a reverse proxy, load balancer, and HTTP cache.
- It can manage multiple client requests and efficiently distribute traffic to backend servers.
Step 2: Set Up Your Development Environment
- Ensure you have Docker installed on your machine.
- Create a simple Node.js web application that will serve as the backend.
- Clone the demo project from the Git repository:
git clone https://gitlab.com/twn-youtube/nginx-crash-course cd nginx-crash-course
Step 3: Add Backend Web Server
- Create a backend web server using Node.js.
- Ensure that your server listens on a specific port (e.g., 3000).
Step 4: Dockerize the Node.js Application
- Create a
Dockerfile
in your Node.js application directory with the following content:FROM node:14 WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "app.js"]
- Build the Docker image:
docker build -t my-node-app .
Step 5: Install NGINX
- Use Docker to run NGINX:
docker run --name nginx -p 80:80 -d nginx
Step 6: Configure NGINX as a Reverse Proxy
- Create a configuration file for NGINX (e.g.,
nginx.conf
) with the following content:server { listen 80; location / { proxy_pass http://localhost:3000; 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-Proto $scheme; } }
- Copy this file into the NGINX container:
docker cp nginx.conf nginx:/etc/nginx/conf.d/default.conf
Step 7: Start NGINX and Test Load Balancing
- Restart the NGINX container to apply the new configuration:
docker restart nginx
- Access your application through your browser at
http://localhost
.
Step 8: Configure HTTPS for Secure Connections
- Generate a self-signed TLS certificate:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
- Update your
nginx.conf
to include the SSL configuration:server { listen 443 ssl; ssl_certificate /etc/nginx/conf.d/server.crt; ssl_certificate_key /etc/nginx/conf.d/server.key; location / { proxy_pass http://localhost:3000; ... } }
- Copy the certificate files into the NGINX container:
docker cp server.crt nginx:/etc/nginx/conf.d/ docker cp server.key nginx:/etc/nginx/conf.d/
Step 9: Redirect HTTP to HTTPS
- Modify your
nginx.conf
to redirect HTTP traffic to HTTPS:server { listen 80; server_name your_domain.com; return 301 https://$host$request_uri; }
Conclusion
In this tutorial, you learned how to set up NGINX as a reverse proxy for a Node.js application running in Docker. You created a secure HTTPS connection and configured HTTP to HTTPS redirection. As next steps, consider learning more about advanced NGINX features or exploring container orchestration with Kubernetes.