因为梦见你离开,我从哭泣中醒来,看夜风吹过窗台。。。
Ubuntu 20.04 安装、启动、配置 Nginx 详细教程
一、安装 Nginx(Ubuntu 20.04)
1. 更新软件源
sudo apt update -y
2. 安装 Nginx
sudo apt install -y nginx
二、查看 Nginx 状态 / 启动 / 停止
1. 查看是否正在运行
sudo systemctl status nginx
成功输出示例:
● nginx.service - A high-performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2026-04-06 10:00:00 CST; 1min 10s ago
Main PID: 12345 (nginx)
Tasks: 2 (limit: 2276)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─12345 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─12346 nginx: worker process
看到 active (running) 就是启动成功。
2. 启动 Nginx(如果没启动)
sudo systemctl start nginx
3. 停止 Nginx
sudo systemctl stop nginx
4. 重启 Nginx(修改配置后用)
sudo systemctl restart nginx
5. 平滑重载(不中断服务 生产推荐)
sudo systemctl reload nginx
6. 设置开机自启(必须开)
sudo systemctl enable nginx
输出:
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx
三、开放防火墙(必须执行,否则外网访问不了)
1. 防火墙设置
sudo ufw allow 'Nginx Full'
sudo ufw reload
输出:
Rules updated
Rules updated (v6)
Reloading firewall
2. 权限问题:403 Forbidden
sudo chown -R www-data:www-data /var/www/xxx
四、Nginx 核心目录(必须记住)
/etc/nginx/nginx.conf # 主配置文件(一般不动)
/etc/nginx/conf.d/ # 你的网站配置放这里
/var/www/html/ # 默认网页目录
/var/log/nginx/ # 日志(access.log 访问日志 / error.log 错误日志)
五、测试 Nginx 是否正常运行
curl localhost
输出:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and working.</p>
</body>
</html>
看到这个页面 = Nginx 安装 + 启动 100% 成功!
六、创建一个完整的前端网站配置(Vue/React/HTML)
1. 创建配置文件
sudo vim /etc/nginx/conf.d/mywebsite.conf
2. 写入带中文注解的配置
# 一个网站 = 一个 server
server {
# 监听 80 端口(HTTP 默认端口)
listen 80;
# 域名或服务器IP
server_name 192.168.1.100;
# 网站根目录(dist 或静态文件目录)
root /var/www/mywebsite/dist;
# 默认首页
index index.html;
# 路由配置(解决 Vue/React 刷新 404 问题)
location / {
# 先找文件 -> 找目录 -> 最后返回 index.html
try_files $uri $uri/ /index.html;
}
# 静态资源缓存(js/css/png 等缓存30天)
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control public;
}
# 开启 gzip 压缩(加速前端加载)
gzip on;
gzip_types text/css application/javascript application/json text/javascript;
gzip_min_length 1k;
}
3. 检查配置是否写错
sudo nginx -t
成功输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4. 重载配置生效
sudo systemctl reload nginx
5. HTTPS 配置(免费 SSL,一键搞定)
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx
按提示输入邮箱、域名,自动配置 HTTPS + 自动续期。
七、反向代理配置(把请求转发到后端 8080)
server {
listen 80;
server_name api.xxx.com;
location / {
# 转发到本地 8080 端口
proxy_pass http://127.0.0.1:8080;
# 传递真实IP给后端
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
八、查看 Nginx 日志(排查问题)
1. 访问日志
tail -f /var/log/nginx/access.log
输出示例:
192.168.1.10 - - [06/Apr/2026:10:05:00 +0800] "GET /index.html HTTP/1.1" 200 1024 "-" "Mozilla/5.0..."
2. 错误日志
tail -f /var/log/nginx/error.log
九、常用命令
sudo nginx -t # 检查配置
sudo systemctl reload nginx # 平滑重启
sudo systemctl status nginx # 查看状态
sudo systemctl start nginx # 启动
sudo systemctl stop nginx # 停止
十、总结
- apt install nginx → 安装
- systemctl start/enable nginx → 启动 + 开机自启
- conf.d/xxx.conf → 写网站配置
- nginx -t && reload → 生效
- 访问 IP 看到页面 → 成功