Skip to content

nginx 与流量分流 ​

线上只有一台 nginx 1.28(Ubuntu),配置文件按站点拆分:

文件内容
/etc/nginx/conf.d/00-log-format.conf统一 log_format main
/etc/nginx/conf.d/emphant.conf主站 emphant.com、/m/、/logs/
/etc/nginx/conf.d/emphant-subdomains.conf文档站与随笔站两个 vhost
/etc/nginx/conf.d/emphant-logs.incGoAccess 面板的 location 片段

所有配置文件都由仓库模板生成(scripts/deploy.py),手工改动会在下次发布时被覆盖,改配置请改模板再执行 ./deploy.sh nginx / ./deploy.sh sites。

主站配置要点 ​

80 端口与默认 server ​

nginx
server {
    listen      80 default_server;
    server_name emphant.com www.emphant.com m.emphant.com _;
    ...
    if ($host != emphant.com) { return 301 https://emphant.com$request_uri; }
}

default_server 意味着任何未匹配的 Host 都会落到这里,然后被重定向到主域。新增子域时必须放在这个 server 之前匹配的前面(靠 server_name 精确匹配,nginx 的自然行为),否则子域会被主域吞掉。

手机 UA 判定 ​

nginx
map $http_user_agent $is_mobile_ua {
    default 0;
    "~*(googlebot|bingbot|baiduspider|...)" 0;   # 爬虫不跳转
    "~*(ipad|tablet|kindle)" 0;                  # 平板看电脑版
    "~*android(?!.*mobile)" 0;                   # Android 平板
    "~*(mobile|iphone|ipod|harmonyos|...)" 1;
}

map 必须写在 http 作用域。nginx 的 conf.d/*.conf 是被 http {} include 的,所以写在文件顶层即可。

分流矩阵:

访问者//{分类}/{工具}/桌面专属工具
手机302 → /m/302 → /m/#/t/{分类}/{工具}留在电脑版
平板200 电脑版200 电脑版200 电脑版
桌面200 电脑版200 电脑版200 电脑版
搜索引擎爬虫200 电脑版200 电脑版200 电脑版

电脑版 / 手机版切换 ​

nginx
location = / {
    if ($arg_view = pc) { rewrite ^ /__view-pc last; }
    if ($arg_view = mobile) { rewrite ^ /__view-mobile last; }
    if ($is_mobile_ua) { set $to_mobile 1; }
    if ($cookie_view_pref = pc) { set $to_mobile 0; }
    if ($to_mobile) { return 302 /m/; }
    try_files /index.html =404;
}

location = /__view-pc {
    add_header Set-Cookie "view_pref=pc; Path=/; Max-Age=2592000; SameSite=Lax" always;
    add_header Cache-Control "no-cache" always;
    rewrite ^ /index.html break;
}

为什么要中间 location:add_header 在 if 块里不生效,只能交给真实 location 去发 Set-Cookie。

缓存策略 ​

路径策略原因
/assets/*(带 hash)expires 1y; immutable文件名变了内容才变
/m/assets/*expires 1y同上
index.html、/m/index.htmlno-cache发版立即生效
/data/lottery.json默认(协商缓存)构建期更新,不需要强缓存

手机版 location ​

nginx
location = /m { return 301 /m/; }

location /m/ {
    index index.html;
    try_files $uri $uri/ /m/index.html;
    add_header Cache-Control "no-cache" always;
    add_header X-Robots-Tag "noindex" always;
}

/m/ 整段 noindex,避免与电脑版重复收录。

访问统计面板 ​

nginx
location /logs/ {
    alias /var/www/emphant-logs/;
    auth_basic           "Emphant Logs";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

GoAccess 生成的静态 HTML 放在 /var/www/emphant-logs/,由主站 server 提供,路径 /logs/,basic auth 保护。账号文件 /etc/nginx/.htpasswd,加人用 htpasswd -b /etc/nginx/.htpasswd 用户名 密码。

子域 vhost ​

文档站与随笔站各一个 server,共用同一张证书(emphant.com 证书已扩展到 5 个域名):

nginx
server {
    listen 80;
    server_name docs.emphant.com;
    return 301 https://docs.emphant.com$request_uri;
}

server {
    listen 443 ssl;
    http2 on;
    server_name docs.emphant.com;

    ssl_certificate     /etc/letsencrypt/live/emphant.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/emphant.com/privkey.pem;

    root /var/www/emphant-docs;
    index index.html;

    location ~* ^/assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    location = /404.html { internal; }

    location / {
        # cleanUrls:/guide/quick-start 实际文件是 guide/quick-start.html
        try_files $uri $uri.html $uri/ =404;
    }

    error_page 404 /404.html;

    add_header X-Content-Type-Options nosniff always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;
    add_header X-Frame-Options SAMEORIGIN always;

    access_log /var/log/nginx/emphant-docs.access.log main;
}

关键细节:

  • try_files $uri $uri.html $uri/ =404; 是 VitePress cleanUrls 的必需配置,缺了会全线 404;
  • 文档页可以设置 no-cache,但 /assets/ 必须长缓存,否则每次打开都重新下载 CSS;
  • 随笔站额外为 /rss.xml 指定 MIME 与短缓存:
nginx
location = /rss.xml {
    default_type application/rss+xml;
    add_header Cache-Control "public, max-age=3600" always;
}

改配置的正确流程 ​

bash
# 改 scripts/deploy.py 里的模板
./deploy.sh nginx --force     # 主站
./deploy.sh sites             # 子站(含证书检查)

# 线上手工验证
nginx -t && systemctl reload nginx

脚本会:备份原配置 → 上传新配置 → nginx -t → 通过才 reload → 失败自动还原并退出。

文档随仓库一起更新,站点内容全部在浏览器本地运行。