Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/korngsamnang/master-nginx
The core concepts of nginx.
https://github.com/korngsamnang/master-nginx
Last synced: 6 days ago
JSON representation
The core concepts of nginx.
- Host: GitHub
- URL: https://github.com/korngsamnang/master-nginx
- Owner: korngsamnang
- Created: 2024-12-12T13:06:06.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2024-12-13T02:11:21.000Z (about 2 months ago)
- Last Synced: 2024-12-13T03:19:16.235Z (about 2 months ago)
- Language: HTML
- Size: 1.55 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Nginx Load Balancing with Docker Compose
This project demonstrates a setup where Nginx acts as a load balancer for three Node.js applications running in Docker containers. The configuration includes HTTPS support with self-signed SSL certificates.
> Process flow diagram
> ![](diagram.png)---
## Features
- **Load Balancing**: Distributes requests among three Node.js apps using the `least_conn` method.
- **HTTPS Support**: Secures communication with SSL certificates.
- **Automatic Redirection**: Redirects HTTP traffic to HTTPS.
- **Dockerized Setup**: Easy deployment with Docker Compose.---
## Prerequisites
- **Docker** and **Docker Compose** installed on your system.
- Basic knowledge of Docker and Nginx configuration.---
## Project Structure
```
βββ πmaster-nginx
βββ πcerts
βββ nginx-selfsigned.crt
βββ nginx-selfsigned.key
βββ πimages
βββ career-quiz.png
βββ devops.png
βββ devsecops.png
βββ it-beginners.png
βββ docker-compose.yaml
βββ Dockerfile
βββ index.html
βββ nginx.conf
βββ package-lock.json
βββ package.json
βββ README.md
βββ server.js
```## Setup Instructions
### 1. Clone the Repository
```bash
git clone https://github.com/korngsamnang/master-nginx
cd master-nginx
```### 2. Generate SSL Certificates
To enable HTTPS, you need SSL certificates. Use the following openssl command to generate self-signed certificates:
```bash
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx-selfsigned.key -out nginx-selfsigned.crt
```Explanation:
- req: Initiates a certificate request.
- x509: Creates a self-signed certificate instead of a certificate signing request (CSR).
- nodes: Skips the option to encrypt the private key (no passphrase).
- days 365: Validity period of the certificate (1 year).
- newkey rsa:2048: Generates a new RSA key pair with 2048 bits.
- keyout: Specifies the filename for the private key.
- out: Specifies the filename for the certificate.Place the generated nginx-selfsigned.crt and nginx-selfsigned.key files in the certs/ directory.
### 3. Build and Run the Containers
Run the following command to build and start the services:
```bash
docker-compose up --build -d
```### 4. Access the Application
- HTTPS: https://localhost
- HTTP requests will be automatically redirected to HTTPS.## Nginx Configuration
The nginx.conf file contains the following:
- Upstream Block: Defines the load-balancing strategy (least_conn) and targets the three Node.js services (app1, app2, app3).
- SSL Configuration: Uses self-signed certificates located in the certs/ directory.
- HTTP to HTTPS Redirect: Ensures secure connections by redirecting HTTP traffic.## Docker Compose Services
- app1, app2, app3:
- Node.js applications running on the internal network.
- Not exposed to the host but accessible via the Nginx proxy.- nginx:
- Acts as a reverse proxy and load balancer.
- Listens on ports 80 (HTTP) and 443 (HTTPS).## Troubleshooting
- Check Logs: Use docker-compose logs to view logs for all services.
- Rebuild Containers: If changes are made, rebuild with docker-compose up --build.
- Certificate Errors: For development, bypass browser warnings for self-signed certificates.