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