扩展:
502 问题汇总 :
http://ask.apelearn.com/question/9109
location 优先级 :
http://blog.lishiming.net/?p=100
12.13 Nginx 防盗链
设定目录访问受限:
1. 配置 test.com 网站目录的防盗链, 编辑虚拟主机配置文件 :
[root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
插入黄框内容 (注释掉红框行):
- 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;
- }
- access_log off;
- }
- location ~ .*\.(js|CSS)$
- {
2. 检测 nginx 配置文件是否有错 ?
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -t
3. 重新加载 nginx 配置文件 (非重启!) :
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -s reload
4. 查看 test.com 网站目录下的静态文件 :
[root@hao-01 ~]# ls /data/wwwroot/test.com/
5. curl 测试, 通过百度网站访问 test.com 网站下的静态文件, 403 受限 :
[root@hao-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
6. curl 测试, 通过 test.com 网站访问 test.com 网站下的静态文件, 200 通 :
[root@hao-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.jpg
12.14 Nginx 访问控制
1. 配置 testt.com 网站访问控制, 编辑虚拟主机配置文件 :
- [root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
- location /hao/
- {
- allow 127.0.0.1;
- allow 127.168.211.128;
- deny all;
- }
2. 检测 nginx 配置文件是否有错 ?
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -t
3. 重新加载 nginx 配置文件 (非重启!) :
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -s reload
4. 在 test.com 网站目录下, 创建一个目录 hao :
[root@hao-01 ~]# mkdir /data/wwwroot/test.com/hao/
5. /.../hao / 目录下创建 1.html 文件, 并追加内容 :
[root@hao-01 ~]# echo test,test>/data/wwwroot/test.com/hao/1.html
6. curl 通过白名单 127.0.0.1ip 访问 test.com 网站下 hao 目录下的 1.html 文件 :
[root@hao-01 ~]# curl -x127.0.0.1:80 test.com/hao/1.html -I
7. curl 通过白名单 192.168.211.128ip 访问 test.com 网站下 hao 目录下的 1.html 文件 :
[root@hao-01 ~]# curl -x192.168.211.128:80 test.com/hao/1.html -I
8. 添加一个虚拟网卡 :
9. 重新自动获取 ens37 新网卡 ip :
[root@hao-01 ~]# dhclient ens37
10. 查看 ens37 新网卡 ip :
[root@hao-01 ~]# ifconfig ens37
11. curl 通过非白名单 192.168.211.132ip 访问 test.com 网站下 hao 目录下的 1.html 文件 :
[root@hao-01 ~]# curl -x192.168.211.132:80 test.com/hao/1.html -I
设定指定目录下的 php 文件解析受限:
1. 配置 test.com 网站 php 文件的防盗链, 编辑虚拟主机配置文件:
[root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
增加内容:
- location ~ .*(upload|image)/.*\.php$
- {
- deny all;
- }
2. 在 test.com 网站目录下, 创建一个目录 upload/:
[root@hao-01 ~]# mkdir /data/wwwroot/test.com/upload/
3. /...hao / 目录下创建 1.php 文件, 并追加内容:
[root@hao-01 ~]# echo 111>/data/wwwroot/test.com/upload/1.php
4. 检测 nginx 配置文件是否有错 ?
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -t
5. 重新加载 nginx 配置文件 (非重启!) :
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -s reload
6. curl 访问 test.com 网站下 upload 目录下 1.php 文件 403 受限 :
[root@hao-01 ~]# curl -x192.168.211.132:80 test.com/upload/1.php -I
根据 user_agent 限制:
1. 编辑 test.com 虚拟主机配置文件 :
[root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
增加内容 (deny all 和 return 403 效果一样):
- if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
- {
- return 403;
- }
2. 检测 nginx 配置文件是否有错 ?
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -t
3. 重新加载 nginx 配置文件 (非重启!) :
[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -s reload
4. curl 指定 user_agent 为 tomatoalsdkflsd, 访问 test.com 网站 403 受限 :
[root@hao-01 ~]# curl -A "tomatoalsdkflsd" -x127.0.0.1:80 test.com -I
12.15 Nginx 解析 php 相关配置
1. 编辑 test.com 虚拟主机配置文件 :
- [root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
- (注意这行很重要, fcgi.sock 保证路径存在: astcgi_pass unix:/tmp/php-fcgi.sock; 路径不对, 访问错误会报 502)
增加内容 (解析 php 相关内容):
- 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;
- }
2. 在 test.com 网站目录下创建 1.php 文件, 并填写如下内容 :
[root@hao-01 ~]# vim /data/wwwroot/test.com/1.php
添加内容 (php 相关配置)
<?php
phpinfo();
3. curl 访问 test.com 网站下的 1.php 文件 :
[root@hao-01 ~]# curl -x127.0.0.1:80 test.com/1.php
12.16 Nginx 代理
1. 进入...vhost 目录下 :
[root@hao-01 ~]# cd /usr/local/nginx/conf/vhost
2. 创建 proxy.conf 文件, 并写入代理配置 :
- [root@hao-01 vhost]# vim /usr/local/nginx/conf/vhost/proxy.conf
- server
- {
- listen 80;
- server_name baidu.com;
- location /
- {
- proxy_pass http://111.13.101.208/;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- }
3. curl 访问远程 baidu.com/robots.txt :
[root@hao-01 vhost]# curl -x127.0.0.1:80 baidu.com/robots.txt
来源: http://www.bubuko.com/infodetail-2528279.html