@@ -31,6 +31,12 @@ If you need to use a specific SSH key for your Git operations, you can configure
|
|||||||
git config --add --local core.sshCommand 'ssh -i <PATH_TO_SSH_KEY>'
|
git config --add --local core.sshCommand 'ssh -i <PATH_TO_SSH_KEY>'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For Clone With Custom SSH Key Use:
|
||||||
|
```bash
|
||||||
|
git -c core.sshCommand="ssh -i <key-path>" clone host:repo
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
*Replace `<PATH_TO_SSH_KEY>` with the actual path to your SSH key file.*
|
*Replace `<PATH_TO_SSH_KEY>` with the actual path to your SSH key file.*
|
||||||
|
|
||||||
## Creating and Managing a Local Git Repository
|
## Creating and Managing a Local Git Repository
|
||||||
|
|||||||
@@ -268,3 +268,18 @@ Docker networks allow communication between containers.
|
|||||||
```bash
|
```bash
|
||||||
docker network disconnect <network-name> <container-name>
|
docker network disconnect <network-name> <container-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## **6. System Commands**
|
||||||
|
|
||||||
|
- **Show Docker Disk usage:**
|
||||||
|
```bash
|
||||||
|
docker system df
|
||||||
|
```
|
||||||
|
- **Remove Unuse Cache,Container And More**
|
||||||
|
- ```bash
|
||||||
|
- docker system prune
|
||||||
|
- ```
|
||||||
|
-
|
||||||
|
-
|
||||||
|
|||||||
164
Monitoring & Logging/Tools/stress-ng.md
Normal file
164
Monitoring & Logging/Tools/stress-ng.md
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
# Stress-ng: Hardware Stress Testing Tool
|
||||||
|
|
||||||
|
`stress-ng` is a powerful tool for performing various stress tests on your hardware components, including CPU, memory, and I/O. This utility helps in assessing hardware stability under heavy loads, making it useful for benchmarking or diagnosing hardware issues.
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [CPU Stress Testing](#cpu-stress-testing)
|
||||||
|
- [Memory Stress Testing](#memory-stress-testing)
|
||||||
|
- [I/O Stress Testing](#io-stress-testing)
|
||||||
|
- [Full System Stress Testing](#full-system-stress-testing)
|
||||||
|
|
||||||
|
### Installation
|
||||||
|
|
||||||
|
To install `stress-ng` on Ubuntu or other Debian-based systems, run:
|
||||||
|
```bash
|
||||||
|
sudo apt install stress-ng
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CPU Stress Testing
|
||||||
|
|
||||||
|
Use `stress-ng` to test CPU performance under different configurations:
|
||||||
|
|
||||||
|
### 1. Run a CPU Test with a Specified Number of Threads
|
||||||
|
|
||||||
|
You can specify the number of threads to use during a CPU stress test. Using `0` as the thread number utilizes all available CPU cores, maximizing CPU usage.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --cpu <thread-number>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --cpu 4
|
||||||
|
```
|
||||||
|
This command uses 4 CPU threads to run the stress test.
|
||||||
|
|
||||||
|
### 2. Run a CPU Test for a Specified Duration
|
||||||
|
|
||||||
|
Specify both the number of CPU threads and a time limit for the test.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --cpu <thread-number> --timeout <seconds>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --cpu 2 --timeout 60s
|
||||||
|
```
|
||||||
|
This command uses 2 CPU threads and runs the test for 60 seconds.
|
||||||
|
|
||||||
|
### 3. Run a CPU Load Test at a Specific Percentage
|
||||||
|
|
||||||
|
You can control the CPU load by specifying a percentage, which is helpful for testing different levels of CPU usage.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --cpu-load <percentage>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --cpu-load 50
|
||||||
|
```
|
||||||
|
This command keeps the CPU load at approximately 50%.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Memory Stress Testing
|
||||||
|
|
||||||
|
Stress test the system's memory by allocating and releasing blocks of memory. This can help evaluate memory stability and performance.
|
||||||
|
|
||||||
|
### 1. Basic Memory Stress Test
|
||||||
|
|
||||||
|
Run a memory test with a specified number of workers (processes) that continuously allocate and deallocate memory.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --vm <workers>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --vm 2
|
||||||
|
```
|
||||||
|
This command uses 2 workers to perform memory stress testing.
|
||||||
|
|
||||||
|
### 2. Run a Timed Memory Stress Test
|
||||||
|
|
||||||
|
Add a timeout option to run a memory test for a specific duration.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --vm <workers> --timeout <seconds>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --vm 2 --timeout 60s
|
||||||
|
```
|
||||||
|
This command uses 2 memory workers and runs the test for 60 seconds.
|
||||||
|
|
||||||
|
### 3. Allocate a Specific Amount of Memory
|
||||||
|
|
||||||
|
Specify the amount of memory to allocate per worker.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --vm <workers> --vm-bytes <size>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --vm 1 --vm-bytes 512M
|
||||||
|
```
|
||||||
|
This command allocates 512 MB of memory for one worker.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## I/O Stress Testing
|
||||||
|
|
||||||
|
I/O testing evaluates the performance of your system’s storage by reading and writing files repeatedly. This is useful for identifying storage bottlenecks and stress-testing the I/O subsystem.
|
||||||
|
|
||||||
|
### 1. Basic I/O Stress Test
|
||||||
|
|
||||||
|
Run an I/O test with a specified number of workers performing file operations.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --io <workers>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --io 4
|
||||||
|
```
|
||||||
|
This command runs 4 I/O workers, continuously reading and writing data to test disk performance.
|
||||||
|
|
||||||
|
### 2. Timed I/O Stress Test
|
||||||
|
|
||||||
|
Specify a timeout to limit the duration of the I/O stress test.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --io <workers> --timeout <seconds>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --io 4 --timeout 60s
|
||||||
|
```
|
||||||
|
This command runs the I/O stress test for 60 seconds with 4 workers.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Full System Stress Testing
|
||||||
|
|
||||||
|
For a comprehensive stress test, `stress-ng` allows you to stress multiple components at once, such as CPU, memory, and I/O. This puts a combined load on the system to simulate heavy usage conditions.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
stress-ng --all --timeout <seconds>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```bash
|
||||||
|
stress-ng --all --timeout 10s
|
||||||
|
```
|
||||||
|
This command runs a 10-second full system stress test, targeting all components that `stress-ng` supports.
|
||||||
|
|
||||||
63
Web Servers & Reverse Proxies/Nginx/1-Information.md
Normal file
63
Web Servers & Reverse Proxies/Nginx/1-Information.md
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# Nginx Documentation
|
||||||
|
|
||||||
|
## What Is Nginx?
|
||||||
|
|
||||||
|
**Nginx** (pronounced "engine-x") is a popular open-source web server and reverse proxy software. Known for its high performance, stability, rich feature set, simple configuration, and low resource consumption, Nginx has become one of the most widely used server applications worldwide.
|
||||||
|
|
||||||
|
Originally developed by Igor Sysoev, Nginx was designed to address the **C10k problem**—the challenge of handling 10,000 concurrent client connections. Nginx overcomes this limitation through an **event-driven, asynchronous architecture** that enables it to manage a vast number of simultaneous connections efficiently and with minimal resource usage.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Key Features of Nginx
|
||||||
|
|
||||||
|
1. **High Performance**: Nginx is optimized to handle high-traffic websites and can serve static content faster than many other web servers.
|
||||||
|
|
||||||
|
2. **Stability**: Its design enables stable operation under heavy load, making it reliable for production environments.
|
||||||
|
|
||||||
|
3. **Low Resource Consumption**: The asynchronous architecture minimizes memory and CPU usage, making it suitable for high-concurrency environments.
|
||||||
|
|
||||||
|
4. **Flexibility**: Nginx can be easily configured to function as a web server, reverse proxy, load balancer, and more.
|
||||||
|
|
||||||
|
5. **Security**: Nginx supports SSL/TLS and can be configured for secure HTTPS connections, with built-in features to prevent DoS and DDoS attacks.
|
||||||
|
|
||||||
|
6. **Extensibility**: Through a range of modules, Nginx can be extended to support various functions such as caching, load balancing, access control, and more.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Common Uses of Nginx
|
||||||
|
|
||||||
|
Nginx’s versatility makes it a powerful tool for a wide range of applications. Below are some of its most common uses:
|
||||||
|
|
||||||
|
### 1. Web Server
|
||||||
|
Nginx can serve as a **standalone web server** to deliver static content like HTML files, images, videos, and more. Due to its efficiency, it’s commonly used to serve content directly or in front of other server applications for added performance and caching benefits.
|
||||||
|
|
||||||
|
### 2. Reverse Proxy
|
||||||
|
Acting as a **reverse proxy**, Nginx can forward client requests to another server, often used to route traffic to applications hosted on multiple servers. This approach helps manage and distribute incoming traffic, improving performance and security by hiding the backend server details from clients.
|
||||||
|
|
||||||
|
### 3. Load Balancer
|
||||||
|
Nginx’s **load balancing** capabilities help distribute traffic across multiple servers. Load balancing not only increases fault tolerance by rerouting traffic in case of server failure but also enhances performance by preventing any single server from becoming overloaded.
|
||||||
|
|
||||||
|
Common load balancing methods in Nginx:
|
||||||
|
- **Round Robin**: Distributes requests sequentially across servers.
|
||||||
|
- **Least Connections**: Routes traffic to the server with the fewest active connections.
|
||||||
|
- **IP Hash**: Ensures clients are consistently routed to the same server based on their IP address.
|
||||||
|
|
||||||
|
### 4. Caching
|
||||||
|
Nginx can act as a **caching server** to store copies of frequently requested content. By serving cached content, Nginx can significantly reduce load times for users and lessen the workload on backend servers. This is especially beneficial for high-traffic websites with dynamic content.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Additional Nginx Features
|
||||||
|
|
||||||
|
- **SSL/TLS Termination**: Nginx can terminate SSL/TLS connections, handling the encryption and decryption process to reduce the burden on backend servers.
|
||||||
|
|
||||||
|
- **URL Rewriting and Redirection**: With URL rewriting rules, Nginx can redirect requests to different URLs, enabling efficient handling of routing and user-friendly URLs.
|
||||||
|
|
||||||
|
- **Access Control**: Provides robust tools for managing access controls, including IP-based access restrictions, user authentication, and authorization.
|
||||||
|
|
||||||
|
- **HTTP/2 and HTTP/3 Support**: Supports newer HTTP protocols for faster and more secure connections.
|
||||||
|
|
||||||
|
- **Customizable Modules**: Nginx’s modular architecture allows for custom modules, enabling functionality for a wide range of applications and configurations.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
114
Web Servers & Reverse Proxies/Nginx/2-Installtion.md
Normal file
114
Web Servers & Reverse Proxies/Nginx/2-Installtion.md
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
# Installing Nginx
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
Before installing Nginx, ensure that you have root or sudo privileges on your system to carry out installation and configuration commands.
|
||||||
|
|
||||||
|
## Step-by-Step Installation
|
||||||
|
|
||||||
|
### For Debian-Based Systems (e.g., Ubuntu)
|
||||||
|
|
||||||
|
1. **Update Package Repositories**
|
||||||
|
It’s a good practice to update your package repositories before installing new software to ensure you’re downloading the latest version available.
|
||||||
|
```bash
|
||||||
|
sudo apt update
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Install Nginx**
|
||||||
|
Install Nginx from the package repository.
|
||||||
|
```bash
|
||||||
|
sudo apt install nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Start Nginx Service**
|
||||||
|
Once installed, start the Nginx service.
|
||||||
|
```bash
|
||||||
|
sudo systemctl start nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Enable Nginx to Start on Boot**
|
||||||
|
This command configures Nginx to start automatically whenever the server reboots.
|
||||||
|
```bash
|
||||||
|
sudo systemctl enable nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
5. **Check Status (Optional)**
|
||||||
|
Verify that Nginx is running correctly.
|
||||||
|
```bash
|
||||||
|
sudo systemctl status nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
### For Red Hat-Based Systems (e.g., CentOS, Fedora)
|
||||||
|
|
||||||
|
1. **Update Package Repositories**
|
||||||
|
As with Debian-based systems, it’s recommended to update repositories first.
|
||||||
|
```bash
|
||||||
|
sudo yum update
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Install Nginx**
|
||||||
|
On Red Hat-based systems, install Nginx with `yum`.
|
||||||
|
```bash
|
||||||
|
sudo yum install nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Start Nginx Service**
|
||||||
|
Start Nginx after installation.
|
||||||
|
```bash
|
||||||
|
sudo systemctl start nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Enable Nginx to Start on Boot**
|
||||||
|
Configure Nginx to launch automatically on system startup.
|
||||||
|
```bash
|
||||||
|
sudo systemctl enable nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
5. **Check Status (Optional)**
|
||||||
|
Confirm that Nginx is running and functioning properly.
|
||||||
|
```bash
|
||||||
|
sudo systemctl status nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Post-Installation Steps
|
||||||
|
|
||||||
|
1. **Allow Nginx Through the Firewall**
|
||||||
|
If your server has a firewall enabled, you may need to allow HTTP (port 80) and HTTPS (port 443) traffic.
|
||||||
|
|
||||||
|
### Debian-Based Systems:
|
||||||
|
```bash
|
||||||
|
sudo ufw allow 'Nginx Full'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Red Hat-Based Systems:
|
||||||
|
```bash
|
||||||
|
sudo firewall-cmd --permanent --add-service=http
|
||||||
|
sudo firewall-cmd --permanent --add-service=https
|
||||||
|
sudo firewall-cmd --reload
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Verify Installation**
|
||||||
|
Open a web browser and navigate to your server’s IP address or domain name:
|
||||||
|
```
|
||||||
|
http://<your-server-ip>
|
||||||
|
```
|
||||||
|
You should see the default Nginx welcome page, which confirms that the installation is successful.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting Common Installation Issues
|
||||||
|
|
||||||
|
- **Error: Package Not Found**
|
||||||
|
If you encounter an error stating that the Nginx package was not found, you may need to install the **EPEL repository** (Extra Packages for Enterprise Linux) on Red Hat-based systems:
|
||||||
|
```bash
|
||||||
|
sudo yum install epel-release
|
||||||
|
sudo yum install nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Permission Denied Errors**
|
||||||
|
Ensure you’re using `sudo` to run commands that require root privileges.
|
||||||
|
|
||||||
|
- **Firewall Blocking Access**
|
||||||
|
If you can’t access Nginx via a browser, ensure that firewall rules are configured to allow HTTP/HTTPS traffic.
|
||||||
|
|
||||||
155
Web Servers & Reverse Proxies/Nginx/3-Setup-Web-Server.md
Normal file
155
Web Servers & Reverse Proxies/Nginx/3-Setup-Web-Server.md
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
# Setting Up a Web Server on Nginx
|
||||||
|
|
||||||
|
This guide covers the steps to configure Nginx as a basic web server to serve static HTML files and handle HTTP requests. We'll set up a sample web server on a Debian-based system, but the steps are similar for other Linux distributions.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- A server with **Nginx installed**. If you haven’t installed Nginx yet, follow the installation instructions in the [Nginx Installation Guide](2-Installtion.md) (or use the provided installation commands).
|
||||||
|
- **Root or sudo privileges** to edit configuration files and restart Nginx services.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 1: Set Up the Web Directory
|
||||||
|
|
||||||
|
1. **Create a Directory for Your Website**
|
||||||
|
Nginx typically serves content from `/var/www/`. Create a new directory for your website content.
|
||||||
|
```bash
|
||||||
|
sudo mkdir -p /var/www/example.com/html
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Set Permissions**
|
||||||
|
Ensure that the Nginx user (usually `www-data`) has permission to read files in this directory.
|
||||||
|
```bash
|
||||||
|
sudo chown -R $USER:$USER /var/www/example.com/html
|
||||||
|
sudo chmod -R 755 /var/www/example.com
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Add a Sample HTML File**
|
||||||
|
Create a simple HTML file to confirm the setup.
|
||||||
|
```bash
|
||||||
|
echo "<html>
|
||||||
|
<head>
|
||||||
|
<title>Welcome to Example.com!</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Success! Nginx is serving your website.</h1>
|
||||||
|
</body>
|
||||||
|
</html>" | sudo tee /var/www/example.com/html/index.html
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 2: Configure Nginx to Serve the Website
|
||||||
|
|
||||||
|
1. **Create a Server Block Configuration File**
|
||||||
|
Nginx server blocks (similar to Apache virtual hosts) allow you to host multiple sites on the same server. Create a new configuration file for your site.
|
||||||
|
```bash
|
||||||
|
sudo vim /etc/nginx/sites-available/example.com
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Add Server Block Configuration**
|
||||||
|
Paste the following configuration into the file, replacing `example.com` with your domain or IP address:
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name example.com www.example.com;
|
||||||
|
|
||||||
|
root /var/www/example.com/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ =404;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Enable the Server Block**
|
||||||
|
Link the configuration file to `sites-enabled` to enable it in Nginx:
|
||||||
|
```bash
|
||||||
|
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Test the Nginx Configuration**
|
||||||
|
Run the following command to check for any syntax errors in the configuration:
|
||||||
|
```bash
|
||||||
|
sudo nginx -t
|
||||||
|
```
|
||||||
|
|
||||||
|
5. **Reload Nginx to Apply Changes**
|
||||||
|
If the syntax test passes, reload Nginx to apply the new configuration.
|
||||||
|
```bash
|
||||||
|
sudo systemctl reload nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 3: Configure DNS (Optional)
|
||||||
|
|
||||||
|
If you have a domain name, point it to your server’s IP address in your DNS provider’s settings. Create an A record for `example.com` and, if desired, `www.example.com` to direct traffic to your server’s IP address.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 4: Access Your Website
|
||||||
|
|
||||||
|
In a web browser, navigate to `http://example.com` (replace `example.com` with your domain or IP address). You should see the sample HTML page you created, confirming that Nginx is serving your web content.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Optional: Enabling HTTPS with SSL/TLS
|
||||||
|
|
||||||
|
For added security, you can configure HTTPS on your Nginx web server. One free and easy way to do this is by using **Let’s Encrypt**.
|
||||||
|
|
||||||
|
1. **Install Certbot and the Nginx Plugin**
|
||||||
|
```bash
|
||||||
|
sudo apt install certbot python3-certbot-nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Obtain and Install a Certificate**
|
||||||
|
Run the following Certbot command to automatically obtain and configure an SSL certificate for your website:
|
||||||
|
```bash
|
||||||
|
sudo certbot --nginx -d example.com -d www.example.com
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Verify Renewal Process**
|
||||||
|
Certificates from Let’s Encrypt expire every 90 days. To automatically renew the certificates, add a cron job or use Certbot’s built-in renewal service:
|
||||||
|
```bash
|
||||||
|
sudo certbot renew --dry-run
|
||||||
|
```
|
||||||
|
|
||||||
|
Now your website will be accessible securely at `https://example.com`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Nginx Configuration Summary
|
||||||
|
|
||||||
|
Here's a quick reference for the key commands and file paths:
|
||||||
|
|
||||||
|
- **Site root directory**: `/var/www/example.com/html`
|
||||||
|
- **Nginx configuration files**:
|
||||||
|
- Site-specific: `/etc/nginx/sites-available/example.com`
|
||||||
|
- Enabled sites: `/etc/nginx/sites-enabled/`
|
||||||
|
- **Commands**:
|
||||||
|
- Check configuration syntax: `sudo nginx -t`
|
||||||
|
- Reload Nginx: `sudo systemctl reload nginx`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting Common Issues
|
||||||
|
|
||||||
|
1. **Error: 403 Forbidden**
|
||||||
|
- Check that Nginx has the necessary permissions to access files in the root directory (`/var/www/example.com/html`). Use `chmod 755` and `chown` commands as shown above.
|
||||||
|
|
||||||
|
2. **Error: 404 Not Found**
|
||||||
|
- Ensure the `index.html` file exists in the specified directory and that `try_files` directive is correctly pointing to it.
|
||||||
|
|
||||||
|
3. **Configuration Errors**
|
||||||
|
- Always run `sudo nginx -t` to check configuration changes before reloading Nginx.
|
||||||
|
|
||||||
|
4. **SSL Issues**
|
||||||
|
- If HTTPS fails, make sure Certbot successfully installed the certificate and that the DNS settings correctly point to your server’s IP address.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
108
Web Servers & Reverse Proxies/Nginx/4-Reverse-Proxy.md
Normal file
108
Web Servers & Reverse Proxies/Nginx/4-Reverse-Proxy.md
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
# Setting Up a Reverse Proxy with Nginx
|
||||||
|
|
||||||
|
A reverse proxy can forward client requests to multiple backend servers, helping manage traffic, load balance, and secure the backend infrastructure. This guide provides a step-by-step approach to setting up a basic reverse proxy configuration in Nginx.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- **Nginx Installed**: Ensure that Nginx is installed and running on your server.
|
||||||
|
- **Root or sudo privileges** to edit configuration files and restart Nginx.
|
||||||
|
- **Backend Servers**: At least two backend services or applications you want to proxy, such as `http://web1.com` and `http://web2.com`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 1: Create the Reverse Proxy Configuration File
|
||||||
|
|
||||||
|
1. **Open a new configuration file** for your reverse proxy in Nginx's `sites-available` directory:
|
||||||
|
```bash
|
||||||
|
sudo vim /etc/nginx/sites-available/reverse-proxy.conf
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Define the Reverse Proxy Configuration**
|
||||||
|
Copy the following configuration into the file. Adjust the backend server names (`web1.com` and `web2.com`) to match your actual server addresses.
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _; # Use "_" to accept any hostname, or specify a domain name
|
||||||
|
|
||||||
|
# Proxy for the first backend application
|
||||||
|
location /web1 {
|
||||||
|
proxy_pass http://web1.com;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Proxy for the second backend application
|
||||||
|
location /web2 {
|
||||||
|
proxy_pass http://web2.com;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Log settings
|
||||||
|
access_log /var/log/nginx/reverse-proxy-access.log;
|
||||||
|
error_log /var/log/nginx/reverse-proxy-error.log;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Explanation of Key Directives
|
||||||
|
- **listen**: Specifies the port Nginx will listen on (80 for HTTP).
|
||||||
|
- **server_name**: The domain name or IP address for this reverse proxy. Using `_` allows it to accept any hostname.
|
||||||
|
- **location**: Defines the URL path (`/web1`, `/web2`) to route to different backend servers.
|
||||||
|
- **proxy_pass**: Specifies the backend server URL to which traffic should be forwarded.
|
||||||
|
- **proxy_set_header**: Sets headers that pass client information to the backend, preserving the original IP and protocol.
|
||||||
|
- **access_log**: Logs access requests.
|
||||||
|
- **error_log**: Logs error messages for easier troubleshooting.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 2: Enable the Reverse Proxy Configuration
|
||||||
|
|
||||||
|
1. **Create a symbolic link** from `sites-available` to `sites-enabled` to enable the reverse proxy configuration in Nginx:
|
||||||
|
```bash
|
||||||
|
sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Verify Nginx Configuration**
|
||||||
|
Run a configuration test to ensure there are no syntax errors:
|
||||||
|
```bash
|
||||||
|
sudo nginx -t
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Reload Nginx** to apply the changes:
|
||||||
|
```bash
|
||||||
|
nginx -s reload
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 3: Access Your Reverse Proxy
|
||||||
|
|
||||||
|
With the reverse proxy set up, you can now access your backend services using the following URLs:
|
||||||
|
|
||||||
|
- **http://your-server-ip/web1**: For requests proxied to `http://web1.com`
|
||||||
|
- **http://your-server-ip/web2**: For requests proxied to `http://web2.com`
|
||||||
|
|
||||||
|
Replace `your-server-ip` with the actual IP address or domain name of your Nginx server.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting Common Issues
|
||||||
|
|
||||||
|
- **Error: 502 Bad Gateway**
|
||||||
|
- This error usually occurs if the backend server is down or unreachable. Verify that the backend server addresses (`http://web1.com`, `http://web2.com`) are correct and accessible.
|
||||||
|
|
||||||
|
- **Permission Denied for Log Files**
|
||||||
|
- Make sure the log file paths are writable by Nginx. Use `sudo chown www-data:www-data /var/log/nginx/reverse-proxy-access.log` if necessary.
|
||||||
|
|
||||||
|
- **Configuration Errors**
|
||||||
|
- Always test configuration changes using `sudo nginx -t` before reloading or restarting Nginx.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
128
Web Servers & Reverse Proxies/Nginx/5-Load-Balancer.md
Normal file
128
Web Servers & Reverse Proxies/Nginx/5-Load-Balancer.md
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
# Setting Up Load Balancing with Nginx
|
||||||
|
|
||||||
|
Load balancing with Nginx helps distribute incoming traffic across multiple backend servers, improving the performance, reliability, and availability of your applications. This guide provides a step-by-step process to configure a basic round-robin load balancer using Nginx.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- **Nginx Installed**: Ensure Nginx is installed on your server.
|
||||||
|
- **Root or sudo privileges** to edit configuration files and restart Nginx.
|
||||||
|
- **Multiple Backend Servers**: Two or more backend servers with applications running. In this example, we use `10.10.10.1` and `10.10.10.2`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 1: Create the Load Balancer Configuration File
|
||||||
|
|
||||||
|
1. **Open a new configuration file** for the load balancer in Nginx’s `sites-available` directory:
|
||||||
|
```bash
|
||||||
|
sudo vim /etc/nginx/sites-available/load_balancer.conf
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Define the Load Balancer Configuration**
|
||||||
|
Copy the following configuration into the file. Replace the IP addresses (`10.10.10.1` and `10.10.10.2`) with the actual IP addresses of your backend servers.
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
# Define the upstream group of backend servers
|
||||||
|
upstream backend_servers {
|
||||||
|
server 10.10.10.1;
|
||||||
|
server 10.10.10.2;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _; # Accept any hostname or specify a domain name if needed
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://backend_servers; # Forward requests to the backend servers group
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Log files
|
||||||
|
access_log /var/log/nginx/load_balancer_access.log;
|
||||||
|
error_log /var/log/nginx/load_balancer_error.log;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Explanation of Key Directives
|
||||||
|
- **upstream**: Defines a pool of backend servers to which Nginx will forward traffic. By default, Nginx uses a round-robin algorithm, sending requests to each server in turn.
|
||||||
|
- **server_name**: Accepts any hostname (`_`) or a specific domain name.
|
||||||
|
- **proxy_pass**: Specifies the backend server group defined by `upstream`.
|
||||||
|
- **proxy_set_header**: Passes client information such as the original IP and protocol to the backend servers.
|
||||||
|
- **access_log** and **error_log**: Directs logs to specified files for easier monitoring and troubleshooting.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 2: Enable the Load Balancer Configuration
|
||||||
|
|
||||||
|
1. **Create a symbolic link** to `sites-enabled` to activate the load balancer configuration in Nginx:
|
||||||
|
```bash
|
||||||
|
sudo ln -s /etc/nginx/sites-available/load_balancer.conf /etc/nginx/sites-enabled/load_balancer.conf
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Verify Nginx Configuration**
|
||||||
|
Test the Nginx configuration for syntax errors:
|
||||||
|
```bash
|
||||||
|
sudo nginx -t
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Reload Nginx** to apply the new configuration:
|
||||||
|
```bash
|
||||||
|
sudo nginx -s reload
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step 3: Test the Load Balancer
|
||||||
|
|
||||||
|
To ensure the load balancer is distributing traffic correctly, you can access the Nginx server’s IP address or domain name in your web browser:
|
||||||
|
```
|
||||||
|
http://your-server-ip/
|
||||||
|
```
|
||||||
|
|
||||||
|
You should see responses from the backend servers. Testing multiple times should show responses alternating between `10.10.10.1` and `10.10.10.2`, as Nginx forwards requests in a round-robin fashion.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Optional: Configure Additional Load Balancing Methods
|
||||||
|
|
||||||
|
Nginx supports multiple load balancing algorithms, which you can specify within the `upstream` block:
|
||||||
|
|
||||||
|
- **Round Robin (default)**: Distributes requests evenly across all servers.
|
||||||
|
- **Least Connections**: Directs traffic to the server with the fewest active connections.
|
||||||
|
```nginx
|
||||||
|
upstream backend_servers {
|
||||||
|
least_conn;
|
||||||
|
server 10.10.10.1;
|
||||||
|
server 10.10.10.2;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- **IP Hash**: Directs requests from the same client IP to the same backend server, which can help with session persistence.
|
||||||
|
```nginx
|
||||||
|
upstream backend_servers {
|
||||||
|
ip_hash;
|
||||||
|
server 10.10.10.1;
|
||||||
|
server 10.10.10.2;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting Common Issues
|
||||||
|
|
||||||
|
- **Error: 502 Bad Gateway**
|
||||||
|
- This error often means that the backend server is unreachable or down. Verify the IP addresses and ensure each backend server is running and accessible.
|
||||||
|
|
||||||
|
- **Permission Denied for Log Files**
|
||||||
|
- Ensure the log file paths are writable by Nginx. Adjust permissions as needed:
|
||||||
|
```bash
|
||||||
|
sudo chown www-data:www-data /var/log/nginx/load_balancer_access.log
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Configuration Errors**
|
||||||
|
- Always test configuration changes with `sudo nginx -t` before reloading or restarting Nginx.
|
||||||
|
|
||||||
Reference in New Issue
Block a user