Nginx 入门:安装、静态托管与反向代理
MrSmy Lv5

前端项目做完要上线,第一件事就是把打包产物交给一台服务器,再用 Nginx 挂出去。这篇是当时的入门笔记:安装、静态资源托管、反向代理,以及那几个让新手最头疼的 location 匹配规则。

安装

CentOS 7 下用官方源安装,比 yum install nginx 靠谱,版本新很多:

1
2
3
4
5
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
nginx -v
systemctl enable nginx
systemctl start nginx

Ubuntu 则是:

1
2
3
apt install -y nginx
nginx -t # 检查配置语法
systemctl reload nginx

配置文件结构:

1
2
/etc/nginx/nginx.conf      # 主配置
/etc/nginx/conf.d/*.conf # 站点配置,被 include 进来

一般不动主配置,每个站点在 conf.d 下建一个 .conf,好维护也好删。

托管一个静态站点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name blog.example.com;

root /var/www/blog;
index index.html;

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

location ~* \.(js|css|png|jpg|jpeg|gif|svg|woff2?)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
access_log off;
}
}

这里有两个关键点:

  • try_files $uri $uri/ /index.html:单页应用的所有路由都由前端处理,服务器找不到文件时必须回退到 index.html,否则刷新页面会 404。
  • index.html 本身不能长期缓存。上面那条正则只匹配静态资源扩展名,index.html 落在 location / 里走默认行为,这样发版后用户才能拿到新文件的 hash 引用。

反向代理

前后端分离后,前端只负责静态文件,接口交给后端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name api.example.com;

location /api/ {
proxy_pass http://127.0.0.1:7001/;
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_connect_timeout 10s;
proxy_read_timeout 60s;
}
}

proxy_pass 结尾带不带 / 是两回事:

配置 请求 /api/user/1 实际转发到
proxy_pass http://127.0.0.1:7001; http://127.0.0.1:7001/api/user/1
proxy_pass http://127.0.0.1:7001/; http://127.0.0.1:7001/user/1

带斜杠表示把 location 匹配到的前缀替换掉。这个细节踩过一次,接口全 404。

location 匹配优先级

这是最容易写错的部分:

  1. = 精确匹配,命中即停
  2. ^~ 前缀匹配,命中后不再检查正则
  3. ~ / ~* 正则匹配,按配置顺序,第一个命中的生效
  4. 普通前缀匹配,取最长的那条
  5. / 兜底
1
2
3
4
5
location = /health { return 200 'ok'; }        # 只匹配 /health
location ^~ /static/ { ... } # 命中后不再看正则
location ~* \.(js|css)$ { ... } # 不区分大小写正则
location /api/ { ... } # 普通前缀
location / { ... } # 兜底

注意一个反直觉的点:普通前缀匹配如果命中但不满足 ^~,Nginx 还会继续检查正则,正则一旦命中就覆盖前缀。所以 /api/v1/xxx.png 这种路径,可能被图片正则抢走。

gzip 压缩

1
2
3
4
5
6
gzip              on;
gzip_vary on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/json image/svg+xml;
gzip_disable "msie6";

gzip_comp_level 不要拉到 9,CPU 消耗提升明显而体积收益很小,5 是常用折中。图片、字体本身已压缩,不要放进 gzip_types。

日志与排错

1
2
3
4
5
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" rt=$request_time urt=$upstream_response_time';

access_log /var/log/nginx/access.log main;

$request_time 是 Nginx 看到的整条耗时,$upstream_response_time 是后端耗时。两个一比就知道慢在前端链路还是后端接口,排慢请求时非常有用。

常用命令

1
2
3
4
nginx -t          # 检查语法,改配置后必跑
nginx -s reload # 平滑重载
nginx -s stop # 停止
tail -f /var/log/nginx/error.log

改完配置先 nginx -t,这是唯一能避免”误删分号导致线上全挂”的习惯。真正的坑不在配置本身,而在改动之前没有备份、没有检查。

  • 本文标题:Nginx 入门:安装、静态托管与反向代理
  • 本文作者:MrSmy
  • 创建时间:2020-07-08 21:12:00
  • 本文链接:https://fengyang.live/2020/07/08/Nginx入门:安装、静态托管与反向代理/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!