Skip to content

Commit b6191b5

Browse files
nginx blog
1 parent f2353e2 commit b6191b5

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

_posts/2024-11-07-nginx.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
layout: post
3+
title: The Fastest Web Server and Reverse Proxy
4+
date: 2024-11-07 22:07:44 +08000
5+
categories: blog
6+
tags: [NGINX,Web Server,Reverse Proxy]
7+
---
8+
9+
## What is NGINX?
10+
NGINX is an open-source web server and reverse proxy used to serve both static and dynamic content. In addition, NGINX can function as a load balancer, an SMTP server for sending emails, and as a tool for optimizing performance with caching and SSL/TLS termination. It was developed in 2004 by Russian programmer Igor Sysoev, with the goal of addressing performance issues in handling a large number of simultaneous connections.
11+
12+
## How Does NGINX Work?
13+
NGINX is particularly well-known for its asynchronous architecture, which means it can handle a large amount of traffic without using excessive system resources. Unlike traditional process-based servers (like Apache), NGINX uses a single or a few worker processes to efficiently handle multiple requests at the same time.
14+
15+
### Key Features:
16+
- Asynchronous Architecture: NGINX can handle a large number of HTTP requests concurrently using far fewer resources compared to process-based servers.
17+
- Speed and Efficiency: NGINX is optimized for high performance and can serve a large number of simultaneous connections without slowing down.
18+
- Low Resource Usage: Due to its architecture, NGINX uses far less CPU and memory, making it ideal for high-traffic systems.
19+
20+
# Roles of NGINX
21+
22+
1. Web Server
23+
Most commonly, NGINX is used as a web server that serves static files such as HTML, CSS, JavaScript, images, and videos. Its efficiency in handling a large number of simultaneous connections allows websites to use NGINX to serve content quickly and securely.
24+
25+
2. Reverse Proxy
26+
As a reverse proxy, NGINX takes HTTP requests from clients and forwards them to appropriate backend servers (such as Node.js, PHP, Python, Ruby applications). This helps with scalability and security, as the backend servers are hidden behind NGINX, and traffic is optimized.
27+
28+
A reverse proxy is also commonly used for load balancing, which helps distribute traffic across multiple backend servers.
29+
30+
3. Load Balancer
31+
NGINX is frequently used for load balancing between multiple servers, distributing incoming traffic across different resources. This improves performance and ensures high availability for applications. NGINX supports various load balancing methods, including round-robin, least connections, and IP-hash.
32+
33+
4. SSL/TLS Termination
34+
NGINX is used for SSL/TLS termination, meaning it takes care of encrypting and decrypting HTTPS traffic. This reduces the load on backend servers and ensures secure communication with clients.
35+
36+
5. Caching
37+
NGINX enables caching at the HTTP response level, which means it can store frequently requested resources (like images and HTML pages) in memory and serve them quickly to users. This reduces load on servers and speeds up page loading times.
38+
39+
## Advantages of NGINX
40+
- High Performance: NGINX is designed to handle tens of thousands of simultaneous connections, even on machines with minimal resources. This makes it perfect for websites with large numbers of visitors and dynamic content.
41+
42+
- Low Resource Usage: Unlike traditional web servers, NGINX is highly efficient in using memory and CPU. This allows servers to handle more traffic without needing additional resources.
43+
44+
- High Scalability: Due to its architecture and ability to balance traffic across multiple servers, NGINX is perfect for scalable systems. It allows you to easily increase your website’s capacity by adding more backend servers.
45+
46+
- Security: By using NGINX as a reverse proxy, you can enhance security by hiding your applications and backend servers from direct exposure to external users. NGINX also allows you to implement SSL/TLS encryption, ensuring secure communication with clients.
47+
48+
- Easy Configuration: NGINX’s configuration files are simple and text-based. Basic configurations can be set up in a few minutes, and the flexibility and extensibility of NGINX make it possible to set up more complex configurations with just a few lines of code.
49+
50+
# How to Set Up NGINX on Your Server?
51+
52+
To set up NGINX on your server, follow these steps (using an Ubuntu server as an example):
53+
54+
1. Install NGINX:
55+
56+
---
57+
sudo apt update
58+
sudo apt install nginx
59+
---
60+
61+
2. Set Up Basic Configuration:
62+
63+
- Open the configuration file:
64+
---
65+
sudo nano /etc/nginx/sites-available/default
66+
---
67+
68+
- Enter the following basic configuration:
69+
70+
---
71+
server {
72+
listen 80;
73+
server_name example.com;
74+
75+
location / {
76+
root /var/www/html;
77+
index index.html;
78+
}
79+
}
80+
---
81+
82+
3. Start NGINX:
83+
84+
- After making your changes, start NGINX:
85+
---
86+
sudo systemctl restart nginx
87+
---
88+
89+
4. Test:
90+
91+
Visit http://your-server-ip/ in your browser and check if the site is live.
92+
93+
---
94+
NGINX is an incredibly powerful, fast, and efficient tool for managing web traffic. Its advantages, such as high scalability, low resource usage, and flexibility, make it an excellent choice for any web developer or system administrator. Whether you're using it as a web server, reverse proxy, load balancer, or for SSL/TLS termination, NGINX can significantly improve the performance and security of your applications.
95+
96+
# Some more important commands
97+
- Stopping NGINX
98+
---
99+
sudo systemctl stop nginx
100+
---
101+
102+
- Restarting NGINX
103+
---
104+
sudo systemctl restart nginx
105+
---
106+
107+
- Reloading NGINX Configuration
108+
---
109+
sudo systemctl reload nginx
110+
---
111+
112+
- Checking the Status of NGINX
113+
---
114+
sudo systemctl status nginx
115+
---
116+
117+
- Enabling NGINX on Startup
118+
---
119+
sudo systemctl enable nginx
120+
---
121+
122+
- Disabling NGINX on Startup
123+
---
124+
sudo systemctl disable nginx
125+
---
126+
127+
- Testing the Configuration for Errors
128+
---
129+
sudo nginx -t
130+
---
131+
132+
- Running NGINX as a Background Process
133+
---
134+
sudo nginx
135+
---
136+
137+
- Stopping NGINX without systemd
138+
---
139+
sudo nginx -s stop
140+
---
141+
142+
- Reloading Configuration without Stopping NGINX
143+
---
144+
sudo nginx -s reload
145+
---
146+
147+
- Checking Logs
148+
149+
Access error or access logs to diagnose issues. On Ubuntu, logs are typically located at /var/log/nginx/.
150+
- Error log:
151+
---
152+
tail -f /var/log/nginx/error.log
153+
---
154+
155+
- Access log:
156+
---
157+
tail -f /var/log/nginx/access.log
158+
---
159+
160+
- Running in Foreground (without systemd)
161+
---
162+
sudo nginx -g "daemon off;"
163+
---
164+
165+
- Gracefully Shutting Down NGINX
166+
- Shuts down NGINX from the command line without systemctl.
167+
168+
---
169+
sudo nginx -s quit
170+
---
171+
172+
- Reopening Log Files
173+
---
174+
sudo nginx -s reopen
175+
---
176+
These commands cover the basics for managing NGINX on a Linux server, including routine maintenance, restarting, testing configurations, and checking the server’s status.

0 commit comments

Comments
 (0)