NGINX Proxy with SSL and Docker Setup
Overview
This guide covers setting up an NGINX reverse proxy with SSL termination in a Docker container to manage traffic between a React app and backend services. It also defines key concepts like NGINX, proxy forwarding, and reverse proxy.
Commands Breakdown
OpenSSL Command
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/app.local.key -out /etc/nginx/ssl/app.local.crt
Generates a self-signed SSL certificate and private key for HTTPS.
Docker Command
docker run -d --name nginx-proxy -p 80:80 -p 443:443 \
-v /path/to/app.local.crt:/etc/nginx/ssl/app.local.crt:ro \
-v /path/to/app.local.key:/etc/nginx/ssl/app.local.key:ro \
-v /path/to/nginx.conf:/etc/nginx/conf.d/default.conf:ro \
nginx
Runs NGINX in a Docker container with SSL certificates and custom configuration.
NGINX Configuration(nginx.conf)
server {
listen 80;
server_name app.local;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name app.local;
ssl_certificate /etc/nginx/ssl/app.local.crt;
ssl_certificate_key /etc/nginx/ssl/app.local.key;
location /api {
proxy_pass http://backend:1337;
}
location / {
proxy_pass http://frontend:3000;
}
}
Benefits
Advantages
Disadvantages
Conclusion
This setup provides a secure, scalable, and flexible way to manage traffic between services. For production, using a CA-issued certificate is recommended.