博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx防盗链、访问控制、PHP解析、服务器代理
阅读量:6905 次
发布时间:2019-06-27

本文共 3804 字,大约阅读时间需要 12 分钟。

hot3.png

12.13 Nginx防盗链

因为该配置也使用location板块,所以本节可结合日志管理一起配置:

[root@1 ~]# 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 ;    #定义referer白名单    if ($invalid_referer) {        return 403;    #if函数的意思是:如果不是白名单内的域名,返回值:403    }    access_log off;}……[root@1 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@1 ~]# /usr/local/nginx/sbin/nginx -s reload

说明: “location ~* ^.+”在此0“ * ”的作用是后面匹配的内容不区分大小写。

检测

[root@1 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.pngHTTP/1.1 403 ForbiddenServer: nginx/1.12.1Date: Mon, 14 Aug 2017 06:22:36 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive

即,使用非白名单内的referer进行访问,被拒绝!!!

12.14 Nginx访问控制

需求:访问/admin/目录的请求,只允许几个指定IP通过,配置如下:

[root@1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf ……location /admin/    {    allow 192.168.8.132;    allow 127.0.0.1;    deny all;    #设置IP白名单    }……[root@1 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@1 ~]# /usr/local/nginx/sbin/nginx -s reload

创建上面指定的目录:

[root@1 ~]# mkdir /data/wwwroot/test.com/admin[root@1 ~]#  echo “test,test”>/data/wwwroot/test.com/admin/1.html

测试

[root@1 ~]# curl -x127.0.0.1:80  test.com/admin/1.html“test,test”[root@1 ~]# curl -x192.168.8.132:80  test.com/admin/1.html“test,test”

访问控制——正则匹配

location ~ .*(abc|image)/.*\.php${        deny all;}

访问控制——user_agent限制

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato'){      return 403;}

说明: deny all和return 403效果一样

12.15 Nginx解析PHP相关配置

核心配置:

vim /usr/local/nginx/conf/vhost/test.com.conf……location ~ \.php$    {        include fastcgi_params;        #fastcgi_pass 127.0.0.1:9000        fastcgi_pass unix:/tmp/php-fcgi.sock;        ##fastcgi_pass两种监听格式,但是要保证Nginx和php-fpm中格式一致        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;    }……

注: 在此注意两点,fastcgi_pass有两种格式,但是无论使用哪种格式都有保证Nginx和php-fpm中格式一致,否则会报错502;fastcgi _param SCRIPT _FILENAME所在行的路径要和root路径一致!

12.16 Nginx代理

Nginx代理是一种反向代理。反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

工作原理

Nginx代理是在一台代理服务器中自定义一个域名,该域名指向一个IP,然后将用户的请求通过这台代理服务器访问指定的IP所对应的web服务器。

graph LR用户-->代理服务器代理服务器-->用户代理服务器-->web服务器web服务器-->代理服务器

进入虚拟主机目录:

[root@1 ~]# cd /usr/local/nginx/conf/vhost/

创建代理服务器

[root@1 vhost]# vim proxy.confserver{    listen 80;    server_name ask.apelearn.com;    #定义域名(一般和被代理ip的域名保持一致)    location /    {        proxy_pass      http://121.201.9.155/;        #指定被代理(被访问)的IP(web服务器IP)        proxy_set_header Host   $host;        #$host指的是代理服务器的servername(也是被代理IP的域名)        proxy_set_header X-Real-IP      $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }}

说明: 因为该虚拟主机只用作代理服务器,不需要访问本地文件,所以不需要设置根目录。

检测

设置代理前

[root@1 vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt404 Not Found

404 Not Found


nginx/1.12.1

设置代理后

[root@1 vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@adailinux vhost]# /usr/local/nginx/sbin/nginx -s reload[root@1 vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt## robots.txt for MiWen#User-agent: *Disallow: /?/admin/Disallow: /?/people/Disallow: /?/question/Disallow: /account/Disallow: /app/Disallow: /cache/

转载于:https://my.oschina.net/u/3706800/blog/1801689

你可能感兴趣的文章
数据库分页
查看>>
ehcache的介绍和使用
查看>>
Android利用Gson解析嵌套多层的Json
查看>>
log4j.properties配置详解与实例
查看>>
Spring学习笔记:Spring概述,第一个IoC依赖注入案例
查看>>
let和var的区别
查看>>
《面向模式的软件体系结构1--模式系统》读书笔记(9)--- 模式系统
查看>>
2012年11月8日学习研究报告
查看>>
《黑客与画家》读后感
查看>>
jq中html(),text(),val()以及js中innerHTML,innerText和value
查看>>
**RESTful API版本控制策略
查看>>
win7更改配色方案介绍~
查看>>
CImageList用法介绍
查看>>
[LeetCode] 40. Combination Sum II
查看>>
1065. 单身狗(25)
查看>>
《程序员代码面试指南》第八章 数组和矩阵问题 未排序数组中累加和小于或等于给定值的最长子数组长度...
查看>>
23种设计模式
查看>>
ES6_入门(3)_顶层对象属性
查看>>
使用VNC(ubuntu 与 windows 互连 )
查看>>
Map和Stack的STL方法简介
查看>>