Nginx防盗链的配置能够和日志记录的相关配置结合起来,由于都用到了location进行匹配php
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
需求:访问/admin/目录的请求,只容许某几个IP访问html
修改虚拟主机配置文件linux
location /admin/ { allow 192.168.88.10; allow 127.0.0.1; deny all; }
Nginx的匹配机制与防火墙相似,从上到下依次匹配,匹配到相应规则即停止继续向下匹配。nginx
结果测试web
[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能够匹配正则,进而能够限制访问的文件类型dom
location ~ .*(upload|image)/.*\.php$ { deny all; }
根据user_agent限制curl
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; }
注:deny all和return 403的效果相同ide
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
问题缘由:post
一、查看Nginx的错误日志,看一下配置文件里面是否正确对应(尤为是各文件中监听的是端口仍是sock)
二、PHP-fpm服务进程资源耗尽
用户不能直接访问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服务器(猿课论坛)。