This directory contains a Docker Compose configuration for running Nginx as a containerized web server and reverse proxy.
This setup creates an Nginx service that:
- Serves static files and acts as a reverse proxy
- Includes health checks to ensure the service is running properly
- Supports custom configuration files
- Exposes a configurable port for web access
- Uses the lightweight Alpine Linux image
- Docker and Docker Compose installed
Before starting the service, configure the following in nginx-compose.yml:
- HTTP Port: Replace
xxxin the ports section with your desired host port (e.g.,80:80) - HTTPS Port: (Optional) Uncomment and configure the HTTPS port if you need SSL support
If you need custom Nginx configuration:
-
Custom nginx.conf:
- Create a
nginx.conffile in this directory - Uncomment the
nginx.confvolume mount in the compose file
- Create a
-
Server configurations:
- Create a
conf.ddirectory - Add your server configuration files (e.g.,
default.conf) - Uncomment the
conf.dvolume mount in the compose file
- Create a
-
Static files:
- Create an
htmldirectory - Add your static files (HTML, CSS, JS, etc.)
- Uncomment the
htmlvolume mount in the compose file
- Create an
Start the Nginx service using Docker Compose:
docker compose -f nginx-compose.yml up -dThe -d flag runs the container in detached mode (in the background).
Check that the container is running:
docker ps | grep nginxYou can also test the service by opening a browser and visiting:
http://localhost:<your-configured-port>
You should see the default Nginx welcome page (if using default configuration).
Create an html directory and add your static files:
mkdir html
echo "<h1>Hello from Nginx!</h1>" > html/index.htmlThen uncomment the html volume mount in nginx-compose.yml.
Create a conf.d/default.conf file:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://your-backend-service: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;
}
}Then uncomment the conf.d volume mount in nginx-compose.yml.
Create multiple configuration files in conf.d/:
# conf.d/app1.conf
server {
listen 80;
server_name app1.localhost;
location / {
proxy_pass http://app1-service:3000;
}
}
# conf.d/app2.conf
server {
listen 80;
server_name app2.localhost;
location / {
proxy_pass http://app2-service:4000;
}
}Nginx can proxy requests to other Docker services. To connect Nginx to other services:
-
Create a shared network:
networks: app-network: driver: bridge
-
Add the network to both Nginx and your services:
services: nginx: networks: - app-network your-service: networks: - app-network
-
Configure Nginx to proxy to the service:
location / { proxy_pass http://your-service:port; }
Serve static HTML, CSS, and JavaScript files directly from Nginx.
Route requests to backend services (Node.js, Python, etc.) based on path or hostname.
Distribute traffic across multiple backend instances.
Handle HTTPS connections and forward to HTTP backends.
Route API requests to different microservices based on paths.
- Port Configuration: Configure the host port in
nginx-compose.ymlby replacingxxxwith your desired port number - Default Configuration: If no custom configuration is mounted, Nginx uses its default configuration
- Static Files: The default document root is
/usr/share/nginx/htmlinside the container - Configuration Reload: After changing configuration files, reload Nginx with:
docker exec nginx nginx -s reload - Logs: View Nginx logs with:
docker compose -f nginx-compose.yml logs -f - Image: Uses the official
nginx:alpineimage for a smaller footprint - Production Use: For production, configure proper SSL certificates, security headers, and access controls
Test your Nginx configuration before reloading:
docker exec nginx nginx -tIf the test passes, reload Nginx:
docker exec nginx nginx -s reloadView access logs:
docker exec nginx tail -f /var/log/nginx/access.logView error logs:
docker exec nginx tail -f /var/log/nginx/error.logOr use Docker Compose:
docker compose -f nginx-compose.yml logs -fFor more information, visit the Nginx documentation.