Nginx配置:防盗链、访问控制、解析PHP以及代理

一、Nginx防盗链

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

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

[[email protected] vhost]# vim linuxtest.conf

server
{
   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、测试

[[email protected] vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# echo "这是防盗链jpg测试!" > /data/wwwroot/linuxtest/test.jpg
[[email protected] vhost]# curl -x127.0.0.1:80 linuxtest.com/test.jpg -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 14:33:07 GMT
Content-Type: image/jpeg
Content-Length: 28
Last-Modified: Thu, 15 Mar 2018 14:32:45 GMT
Connection: keep-alive
ETag: "5aaa840d-1c"
Expires: Thu, 22 Mar 2018 14:33:07 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes

[[email protected] vhost]# curl -x127.0.0.1:80 -e "http://www.com" linuxtest.com/test.jpg -I     //-e选项自定义referer
HTTP/1.1 403 Forbidden
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 14:33:28 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

二、访问控制

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

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

[[email protected] vhost]# vim linuxtest.conf  //添加如下内容

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

2、测试

[[email protected] vhost]# mkdir /data/wwwroot/linuxtest/admin
[[email protected] vhost]#  echo “test,test”>/data/wwwroot/linuxtest/admin/1.html
[[email protected] vhost]# curl -x127.0.0.1:80  linuxtest.com/admin/1.html
“test,test”
[[email protected] 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

修改虚拟主机配置文件

[[email protected] 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服务器–>代理服务器

1、更改配置文件

[[email protected] vhost]# vim proxy.conf

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

[[email protected] vhost]# vim proxy.conf
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# curl -x127.0.0.1:80 ask.apelearn.com -I    //同通过代理
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 15:44:25 GMT
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.29
P3P: 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.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

[[email protected] vhost]# curl ask.apelearn.com -I     //直接连
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 15 Mar 2018 15:46:06 GMT
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.29
P3P: 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.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

原文地址:http://blog.51cto.com/3069201/2087397

时间: 2024-08-26 06:58:50

Nginx配置:防盗链、访问控制、解析PHP以及代理的相关文章

LNMP - Nginx配置防盗链

配置防盗链的目的:当别的网站盗用了我们网站的图片,视频等文件资源,放到他们自己的网站上去,一方面这属于无耻的盗窃行为,另一方面在并发量很大的情况下势必会无端增加我们网站的流量,增加服务器的负载. 解决办法:与Apache一样,Nginx也可以限制referer # cd /usr/local/nginx/conf/vhosts # vim test.conf server { listen 80; server_name www.test.com; index index.html index.

7.配置防盗链,访问控制

[toc] 11.25 配置防盗链 1.防盗链和referer概念 防盗链,通俗讲就是不让别人盗用你网站上的资源,这个资源指的是图片.视频.歌曲.文档等,在这之前需要理解一下referer的概念,如果你通过A网站的一个页面http://a.com/a.html里面的链接去访问B网站的一个页面http://b.com/b.html,那么这个B网站页面的referer就是http://a.com/a.html.也就是说,一个referer就是一个网址. 2.编辑虚拟主机配置文件httpd-vhost

LNMP搭建11:Nginx配置防盗链

我们网站上的资源如果没有设置防盗链,那么其他人可以通过资源链接盗用我们的资源,占用我们的带宽,影响我们网站对合法用户的服务质量.举个例子,假如我们网站上有一张图片,如下图所示: 复制图片地址,可以直接在浏览器中搜到我们的图片,也可以在其他地方通过该图片地址引用或下载该图片. 为了不让别人盗用我们的资源,我们可以在服务器上设置防盗链. 编辑虚拟主机配置文件 [[email protected] vhosts]# vim test.conf 添加指定类型文件的防盗链 没错误重新加载配置文件 [[em

nginx配置防盗链

#白名单valid_referers none blocked *.dd.com#取反,则不是白名单的地址禁止从第三方访问文件.if ($invalid_referer){ return 403;} #指定文件类型.location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 15d; access_log off; #白名单 valid_referers none blocked *.dd.com if ($invalid_referer) { ret

配置防盗链 访问控制Directory 访问控制FilesMatch

原文地址:https://www.cnblogs.com/xiaobo-Linux/p/8541412.html

配置防盗链、访问控制Directory针对目录、访问控制FilesMatch针对链接

配置防盗链 访问控制Directory针对目录 访问控制针对目录FilesMatch针对连接 原文地址:http://blog.51cto.com/13515599/2083516

2.2-nginx配置防盗链

nginx的防盗链配置比apache简单,同样用到了referers #不记录指定的文件类型日志 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$ { access_log off; expires 15d; valid_referers none blocked *.llzdwyp.com *.wyp.com *.llzd.com; if ($invalid_referer)     #无效referer 注意refer

Nginx的防盗链、Nginx的访问控制、Nginx解析php的配置、Nginx代理

Nginx的防盗链 Nginx的访问控制 禁止上传图片目录里php解析 Nginx解析php的配置 Nginx代理 原文地址:http://blog.51cto.com/13515599/2087315

配置防盗链、访问控制Directory、访问控制FilesMatch

配置防盗链 我的网站遇到最多的是两类盗链,一是图片盗链,二是文件盗链.曾经有一个访问量极大的网站盗链我网站的图片,一天竟然消耗了数G的流量.同时,我站放的不少几十兆的大型软件也常遭到文件盗链,大量消耗我站资源. 1.新增内容 [[email protected] local]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.

2018-3-6 10周5次课 配置防盗链、访问控制Directory、FilesMatch

11.25 配置防盗链 ·通过限制referer来实现防盗链的功能 带宽会异常升高,有可能是被倒链 配置如下内容: Order Allow,Deny ##顺序,先把白名单允许,把其他deny掉 Allow from env=local_ref (注意:"Allow,Deny"中间只有一个逗号,也只能有一个逗号,有空格都会出错:单词的大小写不限.上面设定的含义是先设定"先检查允许设定,没有允许的全部禁止",而第二句允许变量 local_ref 的 referer. &