且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

更新时间:2022-09-26 16:07:01

扩展:

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;

}12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理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.gif12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

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.jpg12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理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;

   }

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理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 -I12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

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

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

8. 添加一个虚拟网卡12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理9. 重新自动获取ens37新网卡ip

[root@hao-01 ~]# dhclient ens37

10. 查看ens37新网卡ip

[root@hao-01 ~]# ifconfig ens3711. curl通过白名单192.168.211.132ip访问test.com网站下hao目录下的1.html文件:[root@hao-01 ~]# curl -x192.168.211.132:80 test.com/hao/1.html -I12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理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;

}

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

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;

}

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

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_agenttomatoalsdkflsd,访问test.com网站 403受限

[root@hao-01 ~]# curl -A "tomatoalsdkflsd" -x127.0.0.1:80 test.com -I12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理12.15 Nginx解析php相关配置

1. 编辑test.com虚拟主机配置文件:

[root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

增加内容(解析php相关内容):

(注意这行很重要,fcgi.sock保证路径存在:astcgi_pass unix:/tmp/php-fcgi.sock;

路径不对,访问错误会报502)

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;

}

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

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

502问题汇总  http://ask.apelearn.com/question/9109

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;

   }

}

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理

3. curl访问远程baidu.com/robots.txt

[root@hao-01 vhost]# curl -x127.0.0.1:80 baidu.com/robots.txt

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配置;12.16 Nginx代理










本文转自 主内安详 51CTO博客,原文链接:http://blog.51cto.com/zhuneianxiang/1956200,如需转载请自行联系原作者