Nginx配置referer报错大坑:unknown directive “valid_referers“

错误:

nginx: [emerg] unknown directive "if($invalid_referer)" in 
/usr/local/nginx/conf/nginx.conf:27

代码:

设置有效的refer值

  • none:检测地址没有refer,则有效
  • server_name:检测主机地址,refer显示是从这个地址来的,则有效(server_name必须是完整的http://xxxx
valid_referers  http:192.168.174/134;
if ($invalid_referer){
     return 403;
}

注意(重要的事情说一遍,很可能忽视):if ($invalid_referer)中if后有个空格,不写就会报错

参考文档:

1、Nginx之防盗链及高可用解读

2、ngx_http_referer_module模块

3、ngx_http_referer_module模块

worker_processes auto; # 根据CPU核心数自动设置工作进程数 # events块 events { worker_connections 1024; # 每个工作进程的最大连接数 } # http块 http { include mime.types; # 包含MIME类型定义文件 default_type application/octet-stream; # 默认的MIME类型 # 日志格式定义 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; # 开启高效文件传输模式 tcp_nopush on; # 减少网络报文段的数量 tcp_nodelay on; # 防止Nagle算法,提高网络应用响应时间 keepalive_timeout 65; # 保持连接的时间 # server块 server { listen 8881; server_name localhost; # 禁用SSL(强制HTTP) if ($http_x_forwarded_proto = "https") { return 301 http://$host$request_uri; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://127.0.0.1:9980; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; client_max_body_size 4096m; } # static files location ^~ /browser { proxy_pass http://127.0.0.1:9980; proxy_set_header Host $http_host; } # WOPI discovery URL location ^~ /hosting/discovery { proxy_pass http://127.0.0.1:9980; proxy_set_header Host $http_host; } # Capabilities location ^~ /hosting/capabilities { proxy_pass http://127.0.0.1:9980; proxy_set_header Host $http_host; } # main websocket location ~ ^/cool/(.*)/ws$ { proxy_pass http://127.0.0.1:9980; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $http_host; proxy_read_timeout 36000s; } # download, presentation and image upload location ~ ^/(c|l)ool { proxy_pass http://127.0.0.1:9980; proxy_set_header Host $http_host; } # Admin Console websocket location ^~ /cool/adminws { proxy_pass http://127.0.0.1:9980; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $http_host; proxy_read_timeout 36000s; } } server { listen 80; # 监听端口80 server_name localhost; # 服务器名称 # location块 location / { root /usr/share/nginx/html; # 网站根目录 index index.html index.htm; # 默认页面文件 } # 错误页面配置 error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } } 这是我的nginx文件 需要怎么改
11-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值