1.編輯虛擬主機配置文件:php
vim /usr/local/nginx/conf/vhost/test.com.conf #在server{}裏添加如下配置:html
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ #以()裏字符結尾的 { expires 7d; #過時時間7天 valid_referers none blocked server_names *.test.com ; #定義referer白名單 if ($invalid_referer) { #判斷若不是白名單裏的域名 return 403; #返回403 } access_log off; #不記錄日誌 }
2.檢查並從新加載配置:nginx
/usr/local/nginx/sbin/nginx -t;/usr/local/nginx/sbin/nginx -s reloadweb
3.測試:使用curl -eapache
# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.png HTTP/1.1 403 Forbidden Server: nginx/1.12.1 Date: Mon, 14 Aug 2017 06:22:36 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive
curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.png #返回值爲200,表示正常。vim
需求:訪問/admin/目錄的請求,只容許幾個指定IP經過bash
1.編輯虛擬主機配置文件:服務器
vim /usr/local/nginx/conf/vhost/test.com.conf #在server{}裏添加如下配置:curl
location /admin/ { allow 192.168.8.132; #匹配後則跳出規則,與apache不一樣。 allow 127.0.0.1; deny all; }
2.檢查並從新加載配置:ide
/usr/local/nginx/sbin/nginx -t;/usr/local/nginx/sbin/nginx -s reload
3.建立測試目錄和文件:
mkdir /data/wwwroot/test.com/admin;echo 「test,test」>/data/wwwroot/test.com/admin/1.html
4.測試:
curl -x127.0.0.1:80 test.com/admin #白名單內ip的機器執行返回200
curl -x192.168.8.100:80 test.com/admin #白名單外ip的機器執行返回403
location ~ .*(abc|image)/.*\.php$ #訪問是abc或者image目錄,以.php結尾的 { deny all; #所有拒絕 }
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') #~後添加「*」 可表示忽略大小寫匹配 { return 403; #deny all和return 403效果同樣 }
1.編輯虛擬主機配置文件:
vim /usr/local/nginx/conf/vhost/test.com.conf #在server{}裏添加如下配置:
location ~ \.php$ { include fastcgi_params; #fastcgi_pass 127.0.0.1:9000 fastcgi_pass unix:/tmp/php-fcgi.sock; #fastcgi_pass兩種監聽格式,Nginx和php-fpm中格式要一致,不然報502 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; #此行的路徑要和root路徑一致 }
Nginx代理是在一臺代理服務器中自定義一個域名,該域名指向一個IP,而後將用戶的請求經過這臺代理服務器訪問指定的IP所對應的web服務器。
1.建立代理配置文件:
vim /usr/local/nginx/conf/vhost/proxy.conf #添加如下配置:
server { listen 80; server_name ask.apelearn.com; #定義域名,和被代理ip的域名保持一致 location / { proxy_pass http://121.201.9.155/; #指定被代理(被訪問)的IP proxy_set_header Host $host; #$host指的是代理服務器的server_name(被代理IP的域名) 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.測試:
curl ask.apelearn.com/robots.txt
curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
效果同樣證實代理設置成功。