博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
92.Nginx配置:防盗链、访问控制、解析PHP以及代理
阅读量:7064 次
发布时间:2019-06-28

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

一、Nginx防盗链

防盗链是指一个网站的资源(图片或附件)未经允许在其它网站提供浏览和下载,尤其热门资源的盗链,对网站带宽的消耗非常大,设置防盗链以节省资源。

1、修改虚拟主机配置文件

[root@sdwaqw vhost]# vim linuxtest.confserver{   listen 80;   server_name linuxtest.com;   index index.html index.htm index.php;   root /data/wwwroot/linuxtest;   location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$   {    expires 7d;    valid_referers none blocked server_names  *.linuxtest.com ;#   定义referer白名单       if ($invalid_referer) {        return 403;#    if函数的意思是:如果不是白名单内的域名,返回值:403    }#   location /#     { #       auth_basic         "Auth";#       auth_basic_user_file /usr/local/nginx/conf/htpasswd;#     }   access_log /tmp/linuxtest.log combined_realip;}#使用access_log指定日志存储路径和使用的日志格式名字

2、测试

[root@sdwaqw 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@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload[root@sdwaqw vhost]# echo "这是防盗链jpg测试!" > /data/wwwroot/linuxtest/test.jpg[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/test.jpg -IHTTP/1.1 200 OKServer: nginx/1.12.2Date: Thu, 15 Mar 2018 14:33:07 GMTContent-Type: image/jpegContent-Length: 28Last-Modified: Thu, 15 Mar 2018 14:32:45 GMTConnection: keep-aliveETag: "5aaa840d-1c"Expires: Thu, 22 Mar 2018 14:33:07 GMTCache-Control: max-age=604800Accept-Ranges: bytes[root@sdwaqw vhost]# curl -x127.0.0.1:80 -e "http://www.com" linuxtest.com/test.jpg -I     //-e选项自定义refererHTTP/1.1 403 ForbiddenServer: nginx/1.12.2Date: Thu, 15 Mar 2018 14:33:28 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive

二、访问控制

访问控制即限制指定的IP才能访问指定的目录

1、修改虚拟主机配置文件

[root@sdwaqw vhost]# vim linuxtest.conf //添加如下内容

location /admin/

{
allow 192.168.242.128;
allow 127.0.0.1;
deny all;# 设置IP白名单
}

2、测试

[root@sdwaqw vhost]# mkdir /data/wwwroot/linuxtest/admin

[root@sdwaqw vhost]# echo “test,test”>/data/wwwroot/linuxtest/admin/1.html
[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/admin/1.html
“test,test”
[root@sdwaqw vhost]# curl -x192.168.242.128:80 linuxtest.com/admin/1.html
“test,test”

3、访问控制-正则

location ~ .(abc|image)/..php$

{
deny all;
}

4、访问控制-代理

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')

{
return 403;
}

三、Nginx解析PHP

修改虚拟主机配置文件

[root@sdwaqw vhost]# vim linuxtest.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;
}

四、Nginx代理

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

graph LR
用户–>代理服务器
代理服务器–>用户
代理服务器–>web服务器
web服务器–>代理服务器
92.Nginx配置:防盗链、访问控制、解析PHP以及代理

1、更改配置文件

[root@sdwaqw vhost]# vim proxy.conf

server

{
listen 80;
server_name ask.apelearn.com;# 定义域名(一般和被代理ip的域名保持一致)
location /
{
proxy_pass ;# 指定被代理(被访问)的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;
}
}

2、测试

[root@sdwaqw vhost]# vim proxy.conf

[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@sdwaqw vhost]# curl -x127.0.0.1:80 ask.apelearn.com -I //同通过代理
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Thu, 15 Mar 2018 15:44:25 GMTContent-Type: text/htmlConnection: keep-aliveVary: Accept-EncodingX-Powered-By: PHP/5.3.29P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"Set-Cookie: ape__Session=k44g3eklsert1fgbjhl061l4f4; path=/; domain=.apelearn.comExpires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cache

[root@sdwaqw vhost]# curl ask.apelearn.com -I //直接连

HTTP/1.1 200 OKServer: nginx/1.8.0Date: Thu, 15 Mar 2018 15:46:06 GMTContent-Type: text/htmlConnection: keep-aliveVary: Accept-EncodingX-Powered-By: PHP/5.3.29P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"Set-Cookie: ape__Session=ium8s3hsrjh4ulf6qbrjpdcme2; path=/; domain=.apelearn.comExpires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cache

转载于:https://blog.51cto.com/sdwaqw/2088617

你可能感兴趣的文章
关于Thread类中三个interrupt方法的研究与学习(转)
查看>>
mysql 加入列,改动列,删除列。
查看>>
UML的学习
查看>>
x265探索与研究(六):main()函数
查看>>
UITableView分页
查看>>
跟我一起数据挖掘(13)——矩阵分解
查看>>
CAShapeLayer(持续更新)
查看>>
JAVA UUID 生成唯一标识
查看>>
spring学习笔记(4)依赖注入详解
查看>>
菜鸟学自动化测试(五)-----selenium命令之定位页面元素
查看>>
【SICP练习】64 练习2.35
查看>>
PSK星座对象(constellation.cc)
查看>>
Linux链接脚本学习--lds
查看>>
Android将list数据通过LitePal保存到本地(集合保存到本地)
查看>>
hdu 1285 确定比赛名次
查看>>
Eureka微服务实战-服务提供者
查看>>
简单的原生ajax
查看>>
h5开发坑点小总结
查看>>
几分钟内提升技能的8个 JavaScript 方法!
查看>>
mac显示隐藏文件
查看>>