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
效果同样证实代理设置成功。