Nginx防盗链
1.编辑虚拟主机文件/usr/local/nginx/conf/vhost/test.com.conf添加如下代码
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
valid_referers none blocked server_names *.test.com ; #此处server_names配置可以省略
if ($invalid_referer) {
return 403;
}
access_log off;
}
2.检查配置文件并且重新加载配置文件
/usr/local/nginx/sbin/nginx -t #检查配置文件是否有误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
3.测试防盗链
curl命令refer测试
curl -e "http://www.baidu.com/123.txt" -x127.0.0.1:80 -I test.com/test.jpg
查看引用结果:提示403即无权限
[root@yolks2 ~]# curl -e "http://www.baidu.com/123.txt" -x127.0.0.1:80 -I test.com/test.jpg
HTTP/1.1 403 Forbidden
Server: nginx/1.6.3
Date: Thu, 16 Aug 2018 12:42:14 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
Nginx访问控制
需求:访问/admin/目录的请求,只允许某几个IP访问,防止一句话木马
1.编辑虚拟主机文件/usr/local/nginx/conf/vhost/test.com.conf添加如下代码
location /admin/
{
allow 192.168.248.129; #主机ip
allow 127.0.0.1; #本地主机,即localhost
deny all;
}
规则就按照此处写的顺序执行,先allow再deny.
2.检查配置文件并且重新加载配置文件
/usr/local/nginx/sbin/nginx -t #检查配置文件是否有误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
3.进行测试
windows机器配置hosts然后Linux机器开启80端口防火墙规则:
添加规则
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
使用Windows浏览器访问效果:
Linux上的Nginx查看日志:cat /tmp/test.com.log,因为此时访问的ip不在白名单中,所以被deny掉
4.添加特定目录(如upload)禁止php的解析,关键代码如下:
1)操作文件:/usr/local/nginx/conf/vhost/test.com.conf
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
2)检查配置文件并且重新加载配置文件
/usr/local/nginx/sbin/nginx -t #检查配置文件是否有误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
- 在/data/wwwroot/test.com/ 目录下创建/upload/并且创建测试php文件
[root@yolks2 ~]# mkdir /data/wwwroot/test.com/upload #创建对应目录
[root@yolks2 ~]# echo "test upload php" >> /data/wwwroot/test.com/upload/testphp.php #创建测试文件
4)测试
此处注释掉配置的话也不会解析php,浏览器访问的话但会下载php文件到本地,原因是我们还没有配置好解析php的相关配置。具体参照Nginx解析php相关配置内容部分。
创建txt文件再次测试:
echo "test123" >> /data/wwwroot/test.com/upload/test123.txt
5.根据user_agent限制
1)添加如下配置
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
- 检查配置文件并且重新加载配置文件
/usr/local/nginx/sbin/nginx -t #检查配置文件是否有误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
3)测试
[root@yolks2 ~]# curl -x127.0.0.1:80 test.com/upload/test123.txt -I
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Thu, 16 Aug 2018 14:37:31 GMT
Content-Type: text/plain
Content-Length: 8
Last-Modified: Thu, 16 Aug 2018 14:29:50 GMT
Connection: keep-alive
ETag: "5b758a5e-8"
Accept-Ranges: bytes
[root@yolks2 ~]# curl -A "Tomatoshksjskskj" -x127.0.0.1:80 test.com/upload/test123.txt -I
HTTP/1.1 403 Forbidden
Server: nginx/1.6.3
Date: Thu, 16 Aug 2018 14:38:05 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
严格区分的话则需要稍微修改下配置
if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato') #此处添加*通配符匹配
{
return 403;
}
Nginx解析php相关配置
此时因为我们还未添加相关解析配置,所以php文件并不会解析,在/data/wwwroot/test.com/ 目录下创建文件进行测试:
创建test0816.php文件内容如下:
<?php
echo "this is test php page!";
?>
1.编辑虚拟主机文件/usr/local/nginx/conf/vhost/test.com.conf添加如下代码
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; #此处不写或有误则会报503错误
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
上面配置中的**/tmp/php-fcgi.sock**参数来源于php-fpm配置文件:
/data/wwwroot/test.com$fastcgi_script_name指的是如下所示:
2.检查配置文件并且重新加载配置文件
/usr/local/nginx/sbin/nginx -t #检查配置文件是否有误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
3.测试502
[root@yolks2 ~]# curl -x127.0.0.1:80 test.com/test0816.php
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
查看日志
解决方法:修改成ip访问
使用Windows访问
Nginx代理
进入/usr/local/nginx/conf/vhost目录,编辑新文件proxy.conf
cd /usr/local/nginx/conf/vhost/ #进目录
vim proxy.conf #编辑文件
1.如下配置内容:
server
{
listen 80;
server_name ask.apelearn.com; #访问的server_name地址,即要访问的域名
location /
{
proxy_pass http://223.94.95.10/; #最新的论坛对应地址,可通过ping域名获取最新ip
proxy_set_header Host $host; #$host指的是上面的server_name
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2.检查配置文件并且重新加载配置文件
/usr/local/nginx/sbin/nginx -t #检查配置文件是否有误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
3.进行测试
[root@yolks2 vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#
User-agent: *
Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
Disallow: /*/ajax/
拓展
502问题汇总 http://ask.apelearn.com/question/9109
location优先级 http://blog.lishiming.net/?p=100