为什么选择Nginx
Nginx是目前最流行的Web服务器之一,占据了超过30%的市场份额。相比Apache,Nginx更擅长处理高并发连接,内存占用更少,配置也更直观。
我个人所有的项目都用Nginx,从简单的静态站到反向代理都能轻松搞定。
安装Nginx
Ubuntu/Debian
1
2
3
4
5
6
7
8
9
10
11
12
| $ sudo apt update
$ sudo apt install -y nginx
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove
Setting up nginx-core (1.18.0-6ubuntu14.4) ...
Setting up nginx (1.18.0-6ubuntu14.4) ...
$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
|
CentOS/RHEL
1
2
3
4
| $ sudo yum install -y epel-release
$ sudo yum install -y nginx
$ nginx -v
nginx version: nginx/1.20.1
|
安装完成后,启动并设置开机自启:
1
2
3
4
5
6
7
8
| $ sudo systemctl start nginx
$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service
$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: active (running) since Mon 2026-01-07 10:00:00 UTC
|
打开浏览器访问服务器IP,看到"Welcome to nginx!“页面就说明安装成功了。
配置文件结构
Nginx的配置文件组织得很清晰:
| 路径 | 作用 |
|---|
/etc/nginx/nginx.conf | 主配置文件 |
/etc/nginx/sites-available/ | 可用站点配置 |
/etc/nginx/sites-enabled/ | 已启用站点(软链接) |
/etc/nginx/conf.d/ | 额外配置片段 |
/var/log/nginx/ | 日志目录 |
/var/www/html/ | 默认网站根目录 |
主配置文件解析
1
| $ cat /etc/nginx/nginx.conf
|
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
| user www-data;
worker_processes auto; # 自动匹配CPU核心数
pid /run/nginx.pid;
events {
worker_connections 1024; # 每个worker最大连接数
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志配置
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;
# 加载站点配置
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
|
配置第一个站点
创建站点目录
1
2
3
4
5
6
7
8
9
10
11
| $ sudo mkdir -p /var/www/mysite
$ sudo chown -R www-data:www-data /var/www/mysite
# 创建一个测试页面
$ sudo tee /var/www/mysite/index.html > /dev/null <<'EOF'
<!DOCTYPE html>
<html>
<head><title>我的网站</title></head>
<body><h1>Nginx配置成功!</h1></body>
</html>
EOF
|
创建站点配置
1
| $ sudo vim /etc/nginx/sites-available/mysite
|
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
| server {
listen 80;
server_name example.com www.example.com;
root /var/www/mysite;
index index.html index.htm;
# 访问日志
access_log /var/log/nginx/mysite-access.log;
error_log /var/log/nginx/mysite-error.log;
location / {
try_files $uri $uri/ =404;
}
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
}
|
启用站点
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 创建软链接到sites-enabled
$ sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
# 删除默认站点(可选)
$ sudo rm /etc/nginx/sites-enabled/default
# 测试配置语法
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 重载配置
$ sudo systemctl reload nginx
|
注意:修改配置后一定要先nginx -t测试,确认没有语法错误再reload,否则可能导致Nginx挂掉。
常用配置场景
多站点配置
一台服务器跑多个网站,用server_name区分:
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 站点A
server {
listen 80;
server_name siteA.com;
root /var/www/siteA;
}
# 站点B
server {
listen 80;
server_name siteB.com;
root /var/www/siteB;
}
|
自定义错误页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| server {
# ...其他配置
error_page 404 /404.html;
location = /404.html {
root /var/www/mysite/errors;
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mysite/errors;
internal;
}
}
|
常用Nginx命令
| 命令 | 作用 |
|---|
nginx -t | 测试配置语法 |
nginx -T | 测试并打印完整配置 |
systemctl reload nginx | 平滑重载配置 |
systemctl restart nginx | 重启Nginx |
systemctl stop nginx | 停止Nginx |
tail -f /var/log/nginx/error.log | 实时查看错误日志 |
常见问题
端口被占用
1
2
3
4
5
6
| $ sudo ss -tlnp | grep :80
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("apache2",pid=1234,fd=4))
# Apache占了80端口,先停掉
$ sudo systemctl stop apache2
$ sudo systemctl disable apache2
|
403 Forbidden
1
2
3
4
5
| # 通常是权限问题
$ ls -la /var/www/mysite/
# 确保www-data用户有读取权限
$ sudo chown -R www-data:www-data /var/www/mysite/
$ sudo chmod -R 755 /var/www/mysite/
|
下一步
Nginx安装配置好之后,你可能还需要:
这两个是实际生产环境中几乎必配的功能,建议接着看。