时间:2021-04-20 08:57:39 | 栏目:Nginx | 点击:次
跨域访问控制
跨域访问

为什么浏览器禁止跨域访问
不安全,容易出现CSRF攻击!

如果黑客控制的网站B在响应头里添加了让客户端去访问网站A的恶意信息,就会出现CSRF攻击
Nginx如何配置跨域访问
add_header语法
语法解释:
location ~ .*\.(htm|html)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
root /opt/app/code;
}
防盗链
防盗链目的
基于http_referer防盗链配置模块
ngx_http_referer_module模块用于阻止对“Referer”头字段中具有无效值的请求访问站点。
举例
valid_referers none blocked server_names
*.example.com example.* www.example.org/galleries/
~\.google\.;
if ($invalid_referer) {
return 403;
}
referer_hash_bucket_size语法
语法解释:
referer_hash_bucket_size size;表示设置有效引用散列表的存储区大小。
referer_hash_max_size 语法
语法解释:
referer_hash_max_size size;表示设置有效引用者哈希表的最大大小。
valid_referers语法
语法解释:
防盗链小案例
touch test_referer.html (在 /op/app/code 目录下)
<html> <head> <meta charset="utf-8"> <title>imooc1</title> </head> <body style="background-color:red;"><br data-filtered="filtered"> <h1>张彪</h1> <img src="http://192.168.1.112/wei.png"/> </body> </html>

配置防盗链如果不是从 www.zhangbiao.com 域名转来的就会报错
location ~ .*\.(jpg|gif|png)$ {
valid_referers none blocked www.zhangbiao.com;
if ($invalid_referer) {
return 403;
}
root /opt/app/code/images;
}
location ~ /test_refer.html {
root /opt/app/code;
}
访问
http://192.168.1.112/test_refer.html

访问
http://www.zhangbiao.com/test_refer.html

允许其他网站访问自己网站资源配置
