Skip to content

Nginx(科普 + 命令)

一、是什么与核心概念

Nginx 是高性能 Web 服务器 + 反向代理,以高并发、低内存著称,也能做负载均衡、HTTP 缓存、SSL 终结。一台 Nginx 可同时服务几十个站点,轻松扛上万并发。

概念解释
正向代理代理客户端访问外网(公司代理、VPN)
反向代理代理服务端接收请求,转发给内部服务(本文核心)
负载均衡把请求分发给多个后端服务器,分摊压力
虚拟主机(Server Block)基于域名区分,一台服务器跑多个网站
locationURL 路径匹配规则,决定请求怎么处理
upstream定义后端服务器集群

反向代理原理:浏览器只知道 Nginx 地址,Nginx 转发给真正的服务。好处:安全(隐藏内部地址)、灵活(改后端不用改前端)、统一入口(多服务共用一个端口)。

浏览器 ──→ Nginx (公网IP:80) ──→ 内部服务1 (127.0.0.1:3000)
                                 └→ 内部服务2 (127.0.0.1:8080)

二、前端路由兼容(history 模式)

Vue/React 等 SPA 用 history 模式时,刷新子路由会 404(服务器上不存在 /user/profile 这个文件)。Nginx 用 try_files 把所有请求回退到 index.html

nginx
location / {
  try_files $uri $uri/ /index.html;
}

三个必须对齐的配置:Nginx 的 location 路径、vue-routerbasevue.configpublicPath。三者前缀不一致会导致资源 404 或白屏。

三、安装与检测

bash
which nginx                       # 检查是否已安装
curl -I http://localhost          # 测试是否运行
yum install -y nginx              # CentOS 安装

四、服务管理

bash
sudo systemctl start nginx        # 启动
sudo systemctl stop nginx         # 停止
sudo systemctl restart nginx      # 重启
sudo systemctl reload nginx       # 热重载(不中断服务,推荐)
sudo systemctl status nginx       # 查看状态
sudo systemctl enable nginx       # 开机自启

# 或直接用 nginx 命令
nginx -t                          # 验证配置语法(改配置后必跑)
nginx -s reload                   # 热重载
nginx -s stop                     # 快速停止
nginx -s quit                     # 优雅停止

五、关键目录与文件

bash
cd /etc/nginx                    # 配置目录
cat /etc/nginx/nginx.conf        # 看主配置
cd /usr/share/nginx/html         # 网站部署目录
tail -f /var/log/nginx/access.log  # 访问日志
tail -f /var/log/nginx/error.log   # 错误日志(排错用)
位置说明
/etc/nginx配置目录
/etc/nginx/nginx.conf主配置文件
/usr/share/nginx/html默认网站根目录
/var/log/nginx日志目录

六、生产路由配置模板

用法:复制下面内容到 /etc/nginx/nginx.conf(或 conf.d/*.conf),改完执行 nginx -t && nginx -s reload。场景:前端 SPA 部署在 /admin/(history 模式),后端 API 代理到 127.0.0.1:8080,端口 9001(80 被占用时)。

nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {
        listen 9001;
        server_name _;
        root /usr/share/nginx/html;

        # SPA 根路径在 /admin/(与 router base / publicPath 一致)
        location /admin/ {
            try_files $uri $uri/ /admin/index.html;
        }

        # 默认站点(非 /admin 应用)
        location / {
            try_files $uri $uri/ /index.html;
        }

        # 后端 API 反向代理
        location /api/ {
            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 $scheme;
            proxy_redirect off;
        }

        # 根路径重定向到 /admin/
        location = / {
            return 301 /admin/;
        }

        index index.html;
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
    }
}

七、容器内操作(如 MaxKB)

bash
docker exec -it maxkb sh                          # 进入容器
find / -type f -name "index.html" 2>/dev/null     # 搜索部署目录

八、排错清单

现象排查
刷新 404检查 try_files 是否回退到 index.html
资源 404 / 白屏检查 location / router.base / publicPath 三者一致
访问不了systemctl status nginx + tail -f /var/log/nginx/error.log
改配置不生效nginx -t 校验,再 nginx -s reload
加载慢静态资源别被代理转发(应直接返回)

© 2026 开发速查