day 50 Nginx访问控制与php解析

12.13 Nginx防盗链

  • 编辑nginx配置文件/usr/local/nginx/conf/nginx.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.14 Nginx访问控制

1、限定IP访问控制
  • 需求:访问/admin/目录的请求,只允许某几个IP访问,编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,配置如下:
location /admin/
{
    allow 192.168.133.1; #允许访问的IP
    allow 127.0.0.1;
    deny all;
}
  • mkdir /data/wwwroot/test.com/admin/
  • echo “test,test”>/data/wwwroot/test.com/admin/1.html
  • -t && -s reload                                         #语法检查并重加载
  • curl -x127.0.0.1:80 test.com/admin/1.html -I
  • curl -x192.168.133.130:80 test.com/admin/1.html -I
2、匹配正则
location ~ .*(abc|image)/.*\.php$
{
        deny all;
}

3、根据user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403; #效果与 deny all一样
}

12.15 Nginx解析php的配置

  • 编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,配置如下:
location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock; #fastcgi_pass 用来指定php-fpm监听的地址或者socket
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }

12.16 Nginx代理

  • cd /usr/local/nginx/conf/vhost
  • vim proxy.conf                            #编辑代理配置文件,加入如下内容
server
{
    listen 80;
    server_name ask.apelearn.com;
    location /
    {
        proxy_pass      http://121.201.9.155/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值