简介
Nginx(发音为"engine-x")是一个高性能的开源 Web 服务器,也可以用作反向代理服务器、负载均衡器和 HTTP 缓存。
Nginx 官网文档:https://nginx.org/en/docs/
部署
包管理器
debian/ubuntu
1
2
|
sudo apt update
sudo apt install nginx
|
centos
1
2
3
|
sudo yum update
sudo yum install nginx
|
官方仓库
Debian
前提条件:
1
|
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
|
导入密钥,验证包的正确性:
1
2
|
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
|
验证下载的文件是否包含正确的密钥:
1
|
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
|
输出应包含完整指纹 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 如下:
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx signing key signing-key@nginx.com
如果指纹不同,删除文件。
二选一:
- 要为稳定的 nginx 包设置 apt 存储库,请运行以下命令:
1
2
3
|
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
|
- 如果希望使用 mainline nginx 包,请改为运行以下命令:
1
2
3
|
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
|
设置存储库固定,使用 Nginx 官方的软件包而不是发行版提供的软件包:
1
2
|
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
|
要安装 nginx,请运行以下命令:
1
2
|
sudo apt update
sudo apt install nginx
|
参考教程
https://nginx.org/en/linux_packages.html#Debian
Ubuntu
前提条件:
1
|
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
|
导入密钥,验证包的正确性:
1
2
|
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
|
验证下载的文件是否包含正确的密钥:
1
|
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
|
输出应包含完整指纹 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 如下:
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid
如果指纹不同,删除文件。
二选一:
- 要为稳定的 nginx 包设置 apt 存储库,请运行以下命令:
1
2
3
|
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
|
- 如果希望使用 mainline nginx 包,请改为运行以下命令:
1
2
3
|
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
|
设置存储库固定,使其更喜欢我们的软件包而不是发行版提供的软件包:
1
2
|
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
|
要安装 nginx,请运行以下命令:
1
2
|
sudo apt update
sudo apt install nginx
|
参考教程
https://nginx.org/en/linux_packages.html#Ubuntu
配置文件
通过包管理工具安装的 Nginx 一般在 /etc/nginx
下
其中有一个 nginx.conf
文件,它就是 Nginx 的配置文件,但是为了方便管理,一般不直接修改该文件,而是在 /etc/nginx/conf.d/
下新建文件。
在默认的 nginx.conf
中 http
模块下,有这样一条命令 include /etc/nginx/conf.d/*.conf;
它表示包含了 /etc/nginx/conf.d/
下所有文件,所以我们在/etc/nginx/conf.d/
直接添加新文件即可,如果没有该命令需要手动把文件的路径添加到 nginx.conf
中
配置⽂件结构
Nginx 的配置⽂件是由模块(⼀系列的指令)组成的,每个模块都是由⼀个或者多个指令+参数
组成的。
指令和参数之间使⽤空格来分隔,指令以分号 ; 结尾,参数可以使⽤单引号或者双引号来包裹。
配置⽂件分为以下⼏个模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# 全局块
worker_processes 1;
# events块
events {
}
# http块
http {
# server块
server {
# location块
location / {
}
}
}
|
全局块
全局块是配置⽂件的第⼀个块,也是配置⽂件的主体部分,主要⽤来设置⼀些影响 Nginx 服务器整体运⾏的配置指令,主要包括配置运⾏ Nginx 服务器的⽤户(组)、允许⽣成的 workerprocess 数、进程 PID 存放路径、⽇志存放路径和类型以及配置⽂件引⼊等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 指定运⾏Nginx服务器的⽤户,只能在全局块配置
# 将user指令注释掉,或者配置成nobody的话所有⽤户都可以运⾏
# user [user] [group]
# user nobody nobody;
user nginx;
# 指定⽣成的worker进程的数量,也可使⽤⾃动模式,只能在全局块配置
worker_processes 1;
# 错误⽇志存放路径和类型
error_log /var/log/nginx/error.log warn;
# 进程PID存放路径
pid /var/run/nginx.pid;
|
events 块
1
2
3
4
5
6
|
events {
# 指定使⽤哪种⽹络IO模型,只能在events块中进⾏配置
# use epoll
# 每个worker process允许的最⼤连接数
worker_connections 1024;
}
|
http 块
http 块是配置⽂件的主要部分,包括 http 全局块和其下的 server 块。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
http {
# nginx 可以使用include指令引入其他配置文件
include /etc/nginx/mime.types;
# 默认类型,如果请求的URL没有包含文件类型,会使用默认类型
default_type application/octet-stream; # 默认类型
# 开启高效文件传输模式
sendfile on;
# 连接超时时间
keepalive_timeout 65;
# access_log 日志存放路径和类型
# 格式为:access_log <path> [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log /var/log/nginx/access.log main;
# 定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
# 设置sendfile最大传输片段大小,默认为0,表示不限制
# sendfile_max_chunk 1m;
# 每个连接的请求次数
# keepalive_requests 100;
# keepalive超时时间
keepalive_timeout 65;
# 开启gzip压缩
# gzip on;
# 开启gzip压缩的最小文件大小
# gzip_min_length 1k;
# gzip压缩级别,1-9,级别越高压缩率越高,但是消耗CPU资源也越多
# gzip_comp_level 2;
# gzip压缩文件类型
# gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# upstream指令用于定义一组服务器,一般用来配置反向代理和负载均衡
upstream www.example.com {
# ip_hash指令用于设置负载均衡的方式,ip_hash表示使用客户端的IP进行hash,这样可以保证同一个客户端的请求每次都会分配到同一个服务器,解决了session共享的问题
ip_hash;
# weight 用于设置权重,权重越高被分配到的几率越大
server 192.168.50.11:80 weight=3;
server 192.168.50.12:80;
server 192.168.50.13:80;
}
server {
# 参考server块的配置
}
}
|
server 块
server 块是配置虚拟主机的,⼀个 http 块可以包含多个 server 块,每个 server 块就是⼀个虚拟主机。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
server {
# 监听IP和端口
# listen的格式为:
# listen [ip]:port [default_server] [ssl] [http2] [spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number];
# listen指令非常灵活,可以指定多个IP和端口,也可以使用通配符
# 下面是几个实际的例子:
# listen 127.0.0.1:80; # 监听来自127.0.0.1的80端口的请求
# listen 80; # 监听来自所有IP的80端口的请求
# listen *:80; # 监听来自所有IP的80端口的请求,同上
# listen 127.0.0.1; # 监听来自来自127.0.0.1的80端口,默认端口为80
listen 80;
# server_name 用来指定虚拟主机的域名,可以使用精确匹配、通配符匹配和正则匹配等方式
# server_name example.org www.example.org; # 精确匹配
# server_name *.example.org; # 通配符匹配
# server_name ~^www\d+\.example\.net$; # 正则匹配
server_name localhost;
# location 块
location / {
# ...
}
# error_page 用于指定错误页面,可以指定多个,按照优先级从高到低依次查找
error_page 500 502 503 504 /50x.html; # 错误页面
location = /50x.html {
root /usr/share/nginx/html;
}
}
|
location 块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
server {
#############
# server 块配置...
#############
# location块用来配置请求的路由,一个server块可以包含多个location块,每个location块就是一个请求路由
# location块的格式是:
# location [=|~|~*|^~] /uri/ { ... }
# = 表示精确匹配,只有完全匹配上才能生效
# ~ 表示区分大小写的正则匹配
# ~* 表示不区分大小写的正则匹配
# ^~ 表示普通字符匹配,如果匹配成功,则不再匹配其他location
# /uri/ 表示请求的URI,可以是字符串,也可以是正则表达式
# { ... } 表示location块的配置内容
location / {
# root指令用于指定请求的根目录,可以是绝对路径,也可以是相对路径
root /usr/share/nginx/html; # 根目录
# index指令用于指定默认文件,如果请求的是目录,则会在目录下查找默认文件
index index.html index.htm; # 默认文件
}
# 下面是一些location的示例:
location = / { # 精确匹配请求
root /usr/share/nginx/html;
index index.html index.htm;
}
location ^~ /images/ { # 匹配以/images/开头的请求
root /usr/share/nginx/html;
}
location ~* \.(gif|jpg|jpeg)$ { # 匹配以gif、jpg或者jpeg结尾的请求
root /usr/share/nginx/html;
}
location !~ \.(gif|jpg|jpeg)$ { # 不匹配以gif、jpg或者jpeg结尾的请求
root /usr/share/nginx/html;
}
location !~* \.(gif|jpg|jpeg)$ { # 不匹配以gif、jpg或者jpeg结尾的请求
root /usr/share/nginx/html;
}
}
|
SSL/TLS
在 server 块增加
1
2
3
4
5
6
7
8
9
10
|
# 证书文件路径
ssl_certificate /path/to/yourdomain/cert.pem;
# 私钥文件路径
ssl_certificate_key /path/to/yourdomain/key.pem;
# SSL 配置 (推荐的配置)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
|
一些常见的需求
设置客户端 body 最大值
client_max_body_size
这个指令定义了客户端请求主体的最大允许大小,在网盘项目中,需要设置。
默认值是 1m
可以在 http
, server
, location
中设置
设置方法
在 http
块中设置 (全局设置):
这将对 Nginx 处理的所有站点和所有请求生效。适用于你的所有网站都有相似上传需求的情况。
1
2
3
4
5
6
7
8
9
10
11
|
# /etc/nginx/nginx.conf (或你的主配置文件)
http {
client_max_body_size 100m; # 允许最大上传100MB的文件
# ... 其他 http 配置 ...
server {
# ...
}
}
|
在 server
块中设置 (针对特定网站): 如果你有多个网站,并且每个网站的上传需求不同,可以在每个 server
块中单独设置。
1
2
3
4
5
6
7
8
9
10
|
# /etc/nginx/sites-available/your_website.conf
server {
listen 80;
server_name your_domain.com;
client_max_body_size 500m; # 该网站允许最大上传500MB的文件
# ... 其他 server 配置 ...
}
|
在 location
块中设置 (针对特定 URL 路径): 这是最细粒度的控制,只对匹配特定 URL 路径的请求生效。例如,你可能只在 /upload
路径下允许大文件上传。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
server {
listen 80;
server_name your_domain.com;
location / {
# 默认的或较小的上传限制
client_max_body_size 10m;
# ...
}
location /upload {
# 仅此上传路径允许较大的文件
client_max_body_size 2g; # 允许最大上传2GB的文件
# ...
}
}
|
设置单位
size
可以使用以下单位:
k
或 K
(千字节)
m
或 M
(兆字节)
g
或 G
(吉字节)
例如:100k
, 10m
, 2g
。
设置为 “无限制” (不推荐)
将 client_max_body_size
设置为 0
会禁用对客户端请求主体大小的检查。这意味着 Nginx 会接受任意大小的请求主体。 但请注意: 设置为 0
意味着请求体根本不会被读取,实际上就是不允许上传文件。 如果你真的需要允许非常大的文件上传,应该设置一个非常大的具体值,而不是 0
。
例如,允许非常大的文件(但仍有上限):
1
|
client_max_body_size 10g; # 允许最大10GB的文件
|
常用命令
1
2
3
4
5
6
7
8
9
10
11
12
13
|
systemctl start nginx # 启动Nginx (默认已经启动)
systemctl stop nginx # 关闭Nginx
systemctl status nginx # 查看Nginx状态
sudo nginx -t # 检查Nginx配置文件语法是否正确
sudo nginx -s reload # 重新加载Nginx配置
sudo nginx # 启动Nginx
sudo nginx -c filename # 指定配置⽂件
nginx -V # 查看Nginx的版本和编译参数等信息
sudo nginx -s quit # 优雅停⽌Nginx
sudo nginx -s stop # 快速停⽌Nginx
sudo nginx -s reopen # 重新打开⽇志⽂件
|
案例
简单示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
server {
listen 80;
server_name seektao.cc;
return 301 https://www.seektao.cc$request_uri;
}
server {
listen 80;
server_name www.seektao.cc;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name www.seektao.cc;
}
|
这个 Nginx 配置文件包含了三个 server 块,每个块定义了不同的虚拟主机配置。
- 第一个 server 块:
listen 80;
: 监听 80 端口,表示这个 server 块用于处理 HTTP 请求。
server_name seektao.cc;
: 指定服务器的域名为seektao.cc
。
return 301 https://www.seektao.cc$request_uri;
: 当有 HTTP 请求访问seektao.cc
时,返回 301 重定向到https://www.seektao.cc$request_uri
,即将 HTTP 请求重定向到 HTTPS 协议的www.seektao.cc
。
- 第二个 server 块:
listen 80;
: 同样监听 80 端口,处理 HTTP 请求。
server_name www.seektao.cc;
: 指定服务器的域名为www.seektao.cc
。
return 301 https://$host$request_uri;
: 当有 HTTP 请求访问www.seektao.cc
时,返回 301 重定向到https://$host$request_uri
,即将 HTTP 请求重定向到 HTTPS 协议的www.seektao.cc
。
- 第三个 server 块:
listen 443 ssl http2;
: 监听 443 端口,启用 SSL 和 HTTP/2 协议。
server_name www.seektao.cc;
: 指定服务器的域名为www.seektao.cc
。
这个配置文件的作用是:
- 当有 HTTP 请求访问
seektao.cc
或www.seektao.cc
时,会将请求重定向到相应的 HTTPS 地址。
- 对于 HTTPS 请求,只有
www.seektao.cc
会被处理
这是一个简单的配置示例,实际环境中还需要根据具体需求进行进一步配置,例如配置 SSL 证书、代理、缓存等功能。
web 服务器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/nginx/ssl/yourdomain.com.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.com.key;
# SSL 配置 (推荐的配置)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# HSTS (HTTP Strict Transport Security) - 强制浏览器使用 HTTPS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
# Content Security Policy (CSP) - 进一步加强安全性 (根据你的需求配置)
# add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;";
root /var/www/yourdomain.com; # 替换为你的网站根目录
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
# PHP 配置 (如果你的网站使用 PHP)
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 替换为你的 PHP-FPM 套接字路径
}
location ~ /\.ht {
deny all;
}
}
# 可选: 将 HTTP 请求重定向到 HTTPS
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
|
反向代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
server {
if ($host = yourdomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name yourdomain.com;
# SSL 证书配置
ssl_certificate /etc/nginx/ssl/yourdomain.com/cert.pem;
ssl_certificate_key /etc/nginx/ssl/yourdomain.com/key.pem;
# 安全设置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 反向代理
location / {
proxy_pass http://127.0.0.1:8080/;
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 https;
}
# 上传的最大文件尺寸
client_max_body_size 200000m;
# 设置超时时间,单位为秒
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
# 日志配置
access_log /var/log/nginx/yourdomain.com.access.log;
error_log /var/log/nginx/yourdomain.com.error.log;
}
|