Skip to content

Commit dbd2430

Browse files
feat: writing chapter about nginx (#2278)
* feat: writing chapter about nginx * Update docs/books/web_services/022-web-servers-nginx.md Co-authored-by: Serge Croisé <SergeCroise@users.noreply.github.com> * Update docs/books/web_services/022-web-servers-nginx.md Co-authored-by: Serge Croisé <SergeCroise@users.noreply.github.com> --------- Co-authored-by: Serge Croisé <SergeCroise@users.noreply.github.com>
1 parent b989e6d commit dbd2430

File tree

1 file changed

+273
-4
lines changed

1 file changed

+273
-4
lines changed
Lines changed: 273 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,279 @@
11
---
2-
author:
3-
contributors:
2+
author: Antoine Le Morvan
3+
contributors: Steven Spencer, Ganna Zhyrnova
44
title: Part 2.2 Web Servers Nginx
55
---
66

7-
!!! info
7+
## Nginx web server
88

9-
This content is not written yet.
9+
In this chapter, you will learn about the web server Nginx.
1010

11+
****
12+
13+
**Objectives**: You will learn how to:
14+
15+
:heavy_check_mark: install and configure Nginx
16+
17+
:checkered_flag: **nginx**, **http**
18+
19+
**Knowledge**: :star: :star:
20+
**Complexity**: :star: :star:
21+
22+
**Reading time**: 15 minutes
23+
24+
****
25+
26+
### Generalities
27+
28+
**Nginx** is a **free HTTP web server under BSD license**. It was first developed in Russia in 2002 by Igor Sysoev. In addition to the standard features of a web server, Nginx provides a **reverse proxy** for the **HTTP** protocol, as well as a proxy for the **POP** and **IMAP** messaging protocols.
29+
30+
The development of the nginx server is a response to the **C10K** problem: supporting 10,000 concurrent connections (common on the modern web) is a real challenge for web servers.
31+
32+
Commercial support is available from Nginx Inc.
33+
34+
The server's internal architecture enables **very high performance** with **low memory consumption** compared to the Apache web server in particular.
35+
36+
Modules complementing the basic functions of the nginx kernel are compile-time bound: they cannot be activated/deactivated on the fly.
37+
38+
Server processes are controlled by a master process, making it possible to **modify configuration or update software without stopping service**.
39+
40+
Nginx has a significant market share of 28% on the busiest sites on the market, just behind Apache (41%).
41+
42+
#### Features
43+
44+
Nginx offers the following basic functions:
45+
46+
* Hosting of static web pages;
47+
* Automatic index page generation;
48+
* Accelerated reverse proxy with cache;
49+
* Load balancing;
50+
* Fault tolerance;
51+
* Cached support for FastCGI, uWSGI, SCGI and memcached cache server;
52+
* Various filters for gzip, xslt, ssi, image transformation, ...
53+
* Support for SSL/TLS and SNI;
54+
* HTTP/2 support.
55+
56+
Other features:
57+
58+
* Hosting by name or IP address;
59+
* Keepalive management of client connections;
60+
* Log management: syslog, rotation, buffer;
61+
* URI rewriting;
62+
* Access control: by IP, password, etc.
63+
* FLV and MP4 streaming.
64+
65+
### Installation
66+
67+
Nginx is available directly from the appstream repository, and more recent versions are available as a dnf module.
68+
69+
```bash
70+
sudo dnf install nginx
71+
sudo systemctl enable nginx --now
72+
```
73+
74+
### Configuration
75+
76+
Nginx configuration is located in `/etc/nginx`:
77+
78+
The `/etc/nginx/nginx.conf` file: global server configuration file. Settings affect the entire server.
79+
80+
!!! NOTE
81+
82+
The .htaccess file functionality known to Apache administrators does not exist in nginx!
83+
84+
The `nginx.conf` file, stripped of all comments, is provided below for your information:
85+
86+
```bash
87+
user nginx;
88+
worker_processes auto;
89+
error_log /var/log/nginx/error.log;
90+
pid /run/nginx.pid;
91+
include /usr/share/nginx/modules/*.conf;
92+
events {
93+
worker_connections 1024;
94+
}
95+
http {
96+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
97+
'$status $body_bytes_sent "$http_referer" '
98+
'"$http_user_agent" "$http_x_forwarded_for"';
99+
access_log /var/log/nginx/access.log main;
100+
sendfile on;
101+
tcp_nopush on;
102+
tcp_nodelay on;
103+
keepalive_timeout 65;
104+
types_hash_max_size 4096;
105+
include /etc/nginx/mime.types;
106+
default_type application/octet-stream;
107+
include /etc/nginx/conf.d/*.conf;
108+
server {
109+
listen 80;
110+
listen [::]:80;
111+
server_name _;
112+
root /usr/share/nginx/html;
113+
include /etc/nginx/default.d/*.conf;
114+
error_page 404 /404.html;
115+
location = /404.html {
116+
}
117+
error_page 500 502 503 504 /50x.html;
118+
location = /50x.html {
119+
}
120+
}
121+
}
122+
```
123+
124+
Default configuration guidelines:
125+
126+
| Directive | Description |
127+
|-----------------------------|-------------|
128+
| `user` | Defines the process owner `user` and `group`. If the group is not specified, the group with the same name as the user is used. |
129+
| `worker_processes` | Defines the number of processes. The optimum value depends on many factors, such as the number of CPU cores, hard disk specifications, etc. In case of doubt, the nginx documentation suggests a starting value equivalent to the number of CPU cores available (the auto value will try to determine this). |
130+
| `pid` | Defines a file to store the pid value. |
131+
| `worker_connections` | Sets the maximum number of simultaneous connections a worker process can open (to the client and to mandated servers). |
132+
| `tcp_nopush` | `tcp_nopush` is inseparable from the sendfile option. It is used to optimize the quantity of information sent at a single time. Packets are only sent when they have reached their maximum size. |
133+
| `tcp_nodelay` | Activating `tcp_nodelay` forces data contained in the socket to be sent immediately, regardless of packet size, which is the opposite of what `tcp_nopush` does. |
134+
| `sendfile` | Optimize the sending of static files (this option is not required for a proxy-inverse configuration). If sendfile is enabled, nginx ensures that all packets are completed before they are sent to the client (thanks to `tcp_nopush`). When the last packet arrives, nginx disables `tcp_nopush` and forces data to be sent using `tcp_nodelay`. |
135+
| `keepalive_timeout` | maximum time before closing an inactive connection. |
136+
| `types_hash_max_size` | Nginx maintains hash tables containing static information. Set the maximum size of the hash table. |
137+
| `include` | Include another file or files that match the template provided in the configuration. |
138+
| `default_type` | Default MIME type of a request. |
139+
| `ssl_protocols` | Accepted TLS protocol versions. |
140+
| `ssl_prefer_server_ciphers` | Prefer server cipher suite to client cipher suite. |
141+
| `access_log` | Configure access logs (see “log management” paragraph). |
142+
| `error_log` | Configure error logs (see “log management” paragraph). |
143+
| `gzip` | The ngx_http_gzip_module is a filter that compresses data transmitted in gzip format. |
144+
| `gzip_disable` | Disable gzip based on a regular expression. |
145+
146+
The nginx configuration is structured as follows:
147+
148+
```text
149+
# global directives
150+
151+
events {
152+
# worker configuration
153+
}
154+
155+
http {
156+
# http service configuration
157+
158+
# Configure the first server listening on port 80
159+
server {
160+
listen 80 default_server;
161+
listen [::]:80 default_server;
162+
root /var/www/html;
163+
index index.html index.htm;
164+
server_name _;
165+
location / {
166+
try_files $uri $uri/ =404;
167+
}
168+
}
169+
}
170+
171+
mail {
172+
# mail service configuration
173+
174+
# global mail service directives
175+
server {
176+
# A first server listening on the pop protocol
177+
listen localhost:110;
178+
protocol pop3;
179+
proxy on;
180+
}
181+
182+
183+
server {
184+
# A second server listening on the imap protocol
185+
listen localhost:143;
186+
protocol imap;
187+
proxy on;
188+
}
189+
}
190+
```
191+
192+
### https configuration
193+
194+
To configure an https service, you need to add a server block, or modify an existing server block (a server block can listen on both port 443 and port 80).
195+
196+
This block can, for example, be added to the new `/etc/nginx/conf.d/default_https.conf` file:
197+
198+
```bash
199+
server {
200+
listen 443 ssl default_server;
201+
ssl_protocols TLSv1.3 TLSv1.2 TLSv1.1
202+
ssl_certificate /path/to/cert.pem;
203+
ssl_certificate_key /path/to/key.key;
204+
root /var/www/html;
205+
index index.html index.htm;
206+
server_name _;
207+
location / {
208+
try_files $uri $uri/ =404;
209+
}
210+
}
211+
```
212+
213+
or the default server can be modified to support https:
214+
215+
```bash
216+
server {
217+
listen 80;
218+
listen 443 ssl;
219+
server_name _;
220+
ssl_protocols TLSv1.3 TLSv1.2 TLSv1.1
221+
ssl_certificate /path/to/cert.pem;
222+
ssl_certificate_key /path/to/key.key;
223+
...
224+
}
225+
```
226+
227+
### Log management
228+
229+
The `error_log` directive is used to configure error logs.
230+
231+
Syntax of the error_log directive:
232+
233+
```bash
234+
error_log file [level];
235+
```
236+
237+
The first parameter defines a file to receive error logs.
238+
239+
The second parameter determines the log level: debug, info, notice, warn, error, crit, alert or emerg (see syslog chapter of our admin guide).
240+
241+
Logs can be sent to syslog using the “syslog:” prefix.
242+
243+
```bash
244+
access_log syslog:server=192.168.1.100:5514,tag=nginx debug;
245+
```
246+
247+
### Nginx as a reverse proxy
248+
249+
Reverse proxy functionality is provided by the `ngx_http_upstream_module`. It lets you define groups of servers which are then called by the `proxy_pass` or `fastcgi_pass` directives, `memcached_pass`, etc.
250+
251+
Example of a basic configuration, which distributes the load 2/3 to the first server and 1/3 to the second application server:
252+
253+
```bash
254+
upstream frontservers {
255+
server front1.rockylinux.lan:8080 weight=2;
256+
server front2.rockylinux.lan:8080 weight=1;
257+
}
258+
259+
server {
260+
location / {
261+
proxy_pass http://docs.rockylinux.lan;
262+
}
263+
}
264+
```
265+
266+
Servers can be declared as backups:
267+
268+
```bash
269+
upstream frontservers {
270+
...
271+
server front3.rockylinux.lan:8080 backup;
272+
server front4.rockylinux.lan:8080 backup;
273+
}
274+
```
275+
276+
The server directive accepts a number of arguments:
277+
278+
* `max_fails=numberofattempts`: sets the number of connection attempts that must fail during the time period defined by the `fail_timeout` parameter for the server to be considered unavailable. Default value is 1, 0 disables functionality.
279+
* `fail_timeout=time`: sets the time during which a defined number of connections will cause the server to be unavailable, and sets the period of time during which the server will be considered unavailable. The default value is 10 seconds.

0 commit comments

Comments
 (0)