vi /etc/nginx/conf.d/bbs.aaa.com.conf
location ~ \.(png|gif|jpeg|bmp|mp3|mp4|flv)$ { valid_referers none blocked server_names *.aaa.com; if ($invalid_referer) { return 403; } }
[root@localhost blog.abc.com]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost blog.abc.com]# nginx -s reload
[root@localhost blog.abc.com]# curl -e "http://wwww.baidu.com" -x127.0.0.1:80 blog.abc.com/1.jpeg -I HTTP/1.1 403 Forbidden Server: nginx/1.14.2 Date: Sun, 17 Feb 2019 12:43:02 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@localhost blog.abc.com]# curl -e "http://bbs.aaa.com" -x127.0.0.1:80 blog.abc.com -I HTTP/1.1 200 OK Server: nginx/1.14.2 Date: Sun, 17 Feb 2019 12:48:58 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/7.3.2 Link: <http://blog.abc.com/index.php?rest_route=/>; rel="https://api.w.org/"
当咱们的网站中有某一站点只是针对公司内部使用,禁止外网使用的时候能够使用访问控制来实现php
编辑虚拟主机配置文件html
# vim /usr/local/nginx/conf/vhost/test.com.conf
添加以下内容nginx
allow 127.0.0.1; //现实生产中,该白名单地址应设置为公司外网地址。 deny all;
使用curl命令测试,能够看到,使用指定白名单ip能够正常访问,使用没指定过的ip访问该站点就会受到限制。vim
# curl -x127.0.0.1:80 test.com/admin/1.jpg fangwen kongzhi ceshi ` # curl -x192.168.254.131:80 test.com/admin/1.jpg <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.15.3</center> </body> </html>
编辑虚拟主机配置文件api
# vim /usr/local/nginx/conf/vhost/test.com.conf
添加内容网络
location ~ .*(upload|image)/.*\.php$ { deny all; }
在test.com目录下建立一个upload目录,并写一个PHP文件1.phpcurl
测试配置文件是否有问题,并从新加载ide
# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root[@localhost](https://my.oschina.net/u/570656) ~]# /usr/local/nginx/sbin/nginx -s reload
使用curl测试限制解析是否成功,能够看到返回的代码是403,表示限制解析成功测试
[root[@localhost](https://my.oschina.net/u/570656) ~]# curl -x127.0.0.1:80 test.com/upload/1.php <html> <head><title>403 Forbidden</title></head>
好比我想让谁访问个人网站,我就告诉他域名,若是不告诉别人域名,就说明我不想让他知道个人站点,这须要禁止搜索引擎在网络上爬取站点内容。能够经过user_agent来限制。网站
编辑虚拟主机文件
[root[@localhost](https://my.oschina.net/u/570656) ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
添加以下内容
if ($http_user_agent ~* 'Spider/3.0|baidu|YoudaoBot|Tomato') { return 403; }
测试并从新加载配置文件 ..-t ...-s reload
使用curl测试,curl -A 能够模拟user_agent,发现返回的代码是403,表示实验成功。
[root[@localhost](https://my.oschina.net/u/570656) ~]# curl -A "www.baidu.com" -x127.0.0.1:80 test.com -I HTTP/1.1 403 Forbidden Server: nginx/1.15.3 Date: Tue, 04 Sep 2018 17:57:37 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive