飞跃高山与大洋的鱼飞跃高山与大洋的鱼
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
  • NGINX

    • 快速开启 Nginx
    • 为 Nginx 配置 systemd 服务
    • 处理 Nginx ERR
    • 使用 goaccess 可视化查看日志
    • Nginx 的限制模块
    • Nginx 的 location 规则
    • 主从 Nginx
    • Nginx 反向代理与负载均衡
    • Nginx 的 proxy_pass 规则
    • Nginx 防盗链
      • 配置
      • 测试
        • 1. *.example.com
        • 2. example.*
        • 3. nginx.example.top/foo/
    • Nginx 的 rewrite 规则
    • root 与 alias 区别
    • Nginx 至 HTTPS
    • websocket 反向代理

Nginx 防盗链

referer (引用页),常用于防盗链,当使用 CDN、OSS 时经常会碰到类似的提示。

referer 正确英文为 referrer,但由于早期 HTTP 规范的拼写错误,为了保持向后兼容也就将错就错了。

配置

Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location

# Example
location / {
  valid_referers none blocked *.example.com example.* nginx.example.top/foo/ ~\.google\.;

  if ($invalid_referer) {
      return 403;
  }
  return 200 'valid_referers\n';
}

测试

1. *.example.com

curl -e 'http://dev.example.com' nginx.example.com
# 等价于
curl -H 'referer: http://dev.example.com' nginx.example.com
# HTTP/1.1 200 OK
# valid_referers

curl -ie 'http://dev.aexample.com' nginx.example.com
# HTTP/1.1 403 Forbidden

2. example.*

curl -ie 'http://example.org' nginx.example.com
# HTTP/1.1 200 OK
# valid_referers

curl -ie 'http://dev.example.org' nginx.example.com
# HTTP/1.1 403 Forbidden

curl -ie 'http://example.org/aaa' nginx.example.com
# HTTP/1.1 200 OK
# valid_referers

3. nginx.example.top/foo/

curl -ie 'http://nginx.example.top' nginx.example.com
# HTTP/1.1 403 Forbidden

curl -ie 'http://nginx.example.top/aaa' nginx.example.com
# HTTP/1.1 403 Forbidden

curl -ie 'http://nginx.example.top/foo' nginx.example.com
# HTTP/1.1 403 Forbidden
curl -ie 'http://nginx.example.top/foo/' nginx.example.com
# HTTP/1.1 200 OK
# valid_referers

curl -ie 'http://nginx.example.top/foo/aa' nginx.example.com
# HTTP/1.1 200 OK
# valid_referers
编辑文档!
上次更新:
贡献者: shanyuhai123
Prev
Nginx 的 proxy_pass 规则
Next
Nginx 的 rewrite 规则