nginx防盗链、访问控制、PHP解析、服务器代理

12.13 Nginx防盗链

由于该配置也使用location板块,因此本节可结合日志管理一块儿配置:php

[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 -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
[root@1 ~]# /usr/local/nginx/sbin/nginx -s reload

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

检测

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

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

12.14 Nginx访问控制

需求:访问/admin/目录的请求,只容许几个指定IP经过,配置以下:nginx

[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 -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
[root@1 ~]# /usr/local/nginx/sbin/nginx -s reload

建立上面指定的目录:web

[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效果同样vim

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上请求链接的客户端,此时代理服务器对外就表现为一个服务器。app

工做原理

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

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

进入虚拟主机目录:

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

建立代理服务器

[root@1 vhost]# vim proxy.conf
server
{
    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.txt
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>

设置代理后

[root@1 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
[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/
相关文章
相关标签/搜索