首页 > 系统相关 >Nginx防盗链&Nginx访问控制&Nginx解析php相关配置&Nginx代理

Nginx防盗链&Nginx访问控制&Nginx解析php相关配置&Nginx代理

时间:2022-10-13 21:04:08浏览次数:42  
标签:Nginx 403 2018 test 80 php com 防盗链

12.13 Nginx防盗链

Nginx防盗链的配置可以和日志记录的相关配置结合起来,因为都用到了location进行匹配



修改虚拟主机配置文件

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 ; //定义白名单
if ($invalid_referer) { //如果不是白名单的域名
return 403; //返回403
}
access_log off;
}



效果验证

[root@linux-10 ~]# curl -e "http://www.baidu.com" -x 127.0.0.1:80 test.com/123.png -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Mon, 11 Jun 2018 13:16:31 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@linux-10 ~]# curl -e "http://test.com" -x 127.0.0.1:80 test.com/123.png -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Mon, 11 Jun 2018 13:16:42 GMT
Content-Type: image/png
Content-Length: 19
Last-Modified: Sat, 09 Jun 2018 03:40:35 GMT
Connection: keep-alive
ETag: "5b1b4c33-13"
Expires: Mon, 18 Jun 2018 13:16:42 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes



12.14 Nginx访问控制

需求:访问/admin/目录的请求,只允许某几个IP访问

修改虚拟主机配置文件

location /admin/
{
allow 192.168.88.10;
allow 127.0.0.1;
deny all;
}

Nginx的匹配机制与防火墙类似,从上到下依次匹配,匹配到相应规则即中止继续向下匹配。

结果测试

[root@linux-10 ~]# curl -x127.0.0.1:80 test.com/admin/1.txt -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Mon, 11 Jun 2018 17:24:11 GMT
Content-Type: text/plain
Content-Length: 5
Last-Modified: Mon, 11 Jun 2018 16:35:39 GMT
Connection: keep-alive
ETag: "5b1ea4db-5"
Accept-Ranges: bytes

[root@linux-10 ~]# curl -x192.168.47.128:80 test.com/admin/1.txt -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Mon, 11 Jun 2018 17:24:15 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

注:直接访问/admin/目录本身会产生403,因为虚拟主机配置文件中定义了index 所以,如果只写目录,它会去找index.php index.html  index.htm  如果没有这些名字的文件,就会返回403,这个和deny配置没有关系。



根据访问文件类型进行限制

location可以匹配正则,进而可以限制访问的文件类型

location ~ .*(upload|image)/.*\.php$
{
deny all;
}

根据user_agent限制

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}

注:deny all和return 403的效果相同



12.15 Nginx解析php相关配置



修改虚拟主机配置文件

location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; //要与其他服务中的配置文件的配置保持一致
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}



测试结果

[root@linux-10 ~]# curl -x127.0.0.1:80 test.com/test.php
fsdfsdfsdfsd



502问题

问题原因:

1、查看Nginx的错误日志,看一下配置文件里面是否正确对应(尤其是各文件中监听的是端口还是sock)

2、PHP-fpm服务进程资源耗尽



12.16 Nginx代理



Nginx的代理原理

Nginx防盗链&Nginx访问控制&Nginx解析php相关配置&Nginx代理_php

用户不能直接访问WEB服务器,而代理服务器既可以与用户互通,也可以和WEB服务器互通,因此用户通过访问代理服务器的方式来访问WEB服务器。



修改虚拟主机配置文件

server
{
listen 80;
server_name ask.apelearn.com;

location /
{
proxy_pass http://223.94.95.10/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

注:代理服务器本身起代理作用,因此server_name要填写被代理的WEB服务器的域名,代理服务器本身不存在域名。



测试效果

[root@linux-10 ~]# curl -x127.0.0.1:80 ask.apelearn.com/robot.txt 
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

[root@linux-10 ~]# curl -x127.0.0.1:80 ask.apelearn.com/robot.txt -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 12 Jun 2018 02:12:51 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.3
P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Set-Cookie: ape__Session=g4j6kgen2rjm59fhdni6ohv0d4; path=/; domain=.apelearn.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
myheader: web1

从测试结果可以看出,配置完成Nginx代理后,可通过访问本机回环地址127.0.0.1:80,直接访问到猿课论坛,本次实验中,用户是代理服务器本身,用户访问代理服务器,最终访问到WEB服务器(猿课论坛)。

标签:Nginx,403,2018,test,80,php,com,防盗链
From: https://blog.51cto.com/u_13622854/5754600

相关文章