前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx防盗链、访问控制、PHP解析、服务器代理

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

作者头像
阿dai学长
发布2019-04-03 11:18:48
9160
发布2019-04-03 11:18:48
举报
文章被收录于专栏:阿dai_linux阿dai_linux

12.13 Nginx防盗链

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

代码语言:javascript
复制
[root@adailinux ~]# 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@adailinux ~]# /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 ~]# /usr/local/nginx/sbin/nginx -s reload

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

检测

代码语言:javascript
复制
[root@adailinux ~]# 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进行访问,被拒绝!!!

12.14 Nginx访问控制

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

代码语言:javascript
复制
[root@adailinux ~]# 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@adailinux ~]# /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 ~]# /usr/local/nginx/sbin/nginx -s reload

创建上面指定的目录:

代码语言:javascript
复制
[root@adailinux ~]# mkdir /data/wwwroot/test.com/admin

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

测试

代码语言:javascript
复制
[root@adailinux ~]# curl -x127.0.0.1:80  test.com/admin/1.html
“test,test”

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

访问控制——正则匹配

代码语言:javascript
复制
location ~ .*(abc|image)/.*\.php$
{
        deny all;
}

访问控制——user_agent限制

代码语言:javascript
复制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403;
}

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

12.15 Nginx解析PHP相关配置

核心配置:

代码语言:javascript
复制
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服务器。

代码语言:javascript
复制
graph LR
用户-->代理服务器
代理服务器-->用户
代理服务器-->web服务器
web服务器-->代理服务器

进入虚拟主机目录:

代码语言:javascript
复制
[root@adailinux ~]# cd /usr/local/nginx/conf/vhost/

创建代理服务器

代码语言:javascript
复制
[root@adailinux 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;
    }
}

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

检测

设置代理前
代码语言:javascript
复制
[root@adailinux 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>
设置代理后
代码语言:javascript
复制
[root@adailinux 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@adailinux 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/

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/08/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 12.13 Nginx防盗链
    • 检测
    • 12.14 Nginx访问控制
      • 测试
        • 访问控制——正则匹配
          • 访问控制——user_agent限制
          • 12.15 Nginx解析PHP相关配置
          • 12.16 Nginx代理
            • 工作原理
              • 创建代理服务器
                • 检测
                  • 设置代理前
                  • 设置代理后
              相关产品与服务
              轻量应用服务器
              轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档