细心的你可能发现了一个问题: 答案是:选择 这就引出了 Nginx 中一个核心概念:location 匹配优先级。我们正好借此机会梳理一下它的规则。有个口诀可以帮助记忆:
解释一下:
说实话,这个口诀我也记不住。现在更高效的方式是,遇到复杂的配置需求,直接让 AI 助手帮你生成或检查 下面我们通过一个模拟电商网站的完整 Nginx 配置案例,来直观感受一下不同优先级规则的实战应用:
为了验证上述配置的匹配顺序,可以参考下表:
问题二:
|
| 功能 | Nginx 原生命令 | systemctl 命令 |
|---|---|---|
| 平滑重载配置 | nginx -s reload |
systemctl reload nginx |
| 停止服务 | nginx -s stop 或 nginx -s quit |
systemctl stop nginx |
| 启动服务 | nginx (需带-c指定配置) |
systemctl start nginx |
| 测试配置 | nginx -t |
无直接对应,仍需调用 nginx -t |
对于使用 Systemd 的现代 Linux 服务器,推荐优先使用 systemctl,因为它是系统管理服务的标准,集成度更高(如日志管理、开机自启等)。一个优雅的操作流程通常是:
# 1. 首先测试配置文件语法是否正确
nginx -t
# 2. 如果测试通过,使用 systemctl 平滑重载配置
systemctl reload nginx
# 3. 检查服务状态
systemctl status nginx
# 如果 systemctl 不可用,再回退到原生命令
sudo nginx -s reload
养成先测试 (nginx -t) 后重载的习惯,能有效避免因配置语法错误导致服务异常。
我们通常在能联网的服务器上通过包管理器安装 Nginx,例如在 Ubuntu/Debian 上:
sudo apt update
sudo apt install nginx
但如果遇到一台无法连接外网的服务器,该如何安装呢?这里提供两种方案。
这是最推荐的方式。你需要先在能上网的机器上,根据目标服务器的架构和系统版本,从 Nginx 官方仓库下载对应的安装包。
1. 确定服务器架构和系统版本
# 查看系统架构
uname -m
# 输出 x86_64(Intel/AMD 64位)、aarch64(ARM 64位)等
# 查看系统版本信息
cat /etc/os-release
2. 下载对应的安装包
根据上一步的信息,到 Nginx 官方下载页面 或对应的包仓库找到合适版本的 RPM(CentOS/RHEL)或 DEB(Ubuntu/Debian)包。
3. 传输并安装
将下载好的包上传到目标服务器,然后执行安装:
# 对于 RPM 包 (CentOS/RHEL)
sudo rpm -ivh nginx-*.rpm
# 对于 DEB 包 (Ubuntu/Debian)
sudo dpkg -i nginx-*.deb
4. 启动并验证
sudo systemctl start nginx
sudo systemctl enable nginx
nginx -v
curl http://localhost
这种方式更为复杂,通常只在有特殊定制需求(如添加第三方模块、指定安装路径)时使用。主要步骤包括:下载源码、安装编译依赖(如 gcc、PCRE、zlib、OpenSSL)、执行 ./configure、make、make install。对于大多数运维场景,方案一已足够。
当你使用 Unity 发布 WebGL(最终输出为 WASM 文件)项目并用 Nginx 部署时,可能会遇到类似下面的错误:
Failed to load module script: The server responded with a non-JavaScript MIME type of "application/wasm".
这通常是因为 Nginx 没有正确配置 WASM 文件的 MIME 类型。部署 WASM 应用需要一些特殊配置。
找到 Nginx 配置文件目录下的 mime.types 文件(通常在 /etc/nginx/mime.types 或 /usr/local/nginx/conf/mime.types),确保其中包含以下行:
application/wasm wasm;
如果不存在,请手动添加。
以下是一个针对 Unity WebGL 应用的完整 Nginx 配置示例,重点关注 MIME 类型、缓存和压缩设置:
server {
listen 80;
server_name your-domain.com; # 请修改为你的域名或IP
root /var/www/unity-webgl; # 修改为你的构建文件存放目录
index index.html;
# WASM 文件(必须正确设置 MIME 类型)
location ~ \.wasm$ {
# 明确指定 MIME 类型
types {
application/wasm wasm;
}
# 长期缓存,WASM 文件通常不会变
add_header Cache-Control "public, max-age=31536000, immutable";
# 如果需要跨域则添加,同域可省略
add_header Access-Control-Allow-Origin *;
}
# 处理压缩的 WASM 文件(如 .wasm.gz)
location ~ \.wasm\.gz$ {
add_header Content-Encoding gzip; # 告诉浏览器这是gzip压缩的
add_header Content-Type application/wasm; # 设置正确的类型
add_header Cache-Control "public, max-age=31536000, immutable";
}
# Data 文件(Unity 资源数据)
location ~ \.data$ {
types {
application/octet-stream data;
}
add_header Cache-Control "public, max-age=31536000, immutable";
}
location ~ \.data\.gz$ {
add_header Content-Encoding gzip;
add_header Content-Type application/octet-stream;
add_header Cache-Control "public, max-age=31536000, immutable";
}
# JavaScript 文件
location ~ \.js$ {
types {
application/javascript js;
}
add_header Cache-Control "public, max-age=31536000, immutable";
}
location ~ \.js\.gz$ {
add_header Content-Encoding gzip;
add_header Content-Type application/javascript;
add_header Cache-Control "public, max-age=31536000, immutable";
}
# Unity 特定的目录和资源
location /StreamingAssets/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /Build/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /TemplateData/ {
add_header Cache-Control "public, max-age=86400";
}
# HTML 文件不应长时间缓存,以确保更新及时生效
location ~ \.html$ {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
}
# 通用处理规则
location / {
try_files $uri $uri/ /index.html;
}
# 开启 Gzip 压缩以提升性能
gzip on;
gzip_vary on;
gzip_types
text/plain
text/css
application/javascript
application/json
application/wasm # 记得加入 wasm
application/octet-stream;
}
mime.types 文件中添加 application/wasm wasm;,这是浏览器正确识别和执行 WASM 文件的前提。.gz 等压缩文件,需通过 add_header Content-Encoding 正确响应,否则浏览器无法解压。immutable)。而 index.html 是入口,应禁用缓存以确保用户总能获取到最新版本。X-Content-Type-Options: nosniff。希望以上关于 Nginx 配置细节、运维命令和特殊应用部署的分享,能帮助你更深入地理解这个强大的 Web 服务器。如果你在配置过程中遇到其他问题,欢迎到云栈社区与其他开发者一起交流探讨。