Nginx防盗链、访问控制、解析PHP相关配置、Nginx代理

​11月28日任务

12.13 Nginx防盗链

12.14 Nginx访问控制

12.15 Nginx解析php相关配置

12.16 Nginx代理

 

 

12.13 Nginx防盗链

  • 配置如下,可以和上面的配置结合起来

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;
}
 

#操作过程

[root@zgxlinux-01 conf]# vim /usr/local/nginx/conf/vhost/test.com.conf 

10eedd5394a7346b25889f58ce2ebca9b4a.jpg

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

[root@zgxlinux-01 conf]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 11:00:28 GMT
Content-Type: application/javascript
Content-Length: 32
Last-Modified: Sat, 01 Dec 2018 04:04:55 GMT
Connection: keep-alive
ETag: "5c020867-20"
Accept-Ranges: bytes

[root@zgxlinux-01 conf]# curl -x127.0.0.1:80 -I test.com/2.jif
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 11:00:49 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@zgxlinux-01 conf]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/2.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 11:05:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
 

 

 

12.14 Nginx访问控制

  • 需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:

location /admin/
{
    allow 192.168.133.1;
    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

 

#操作过程

[root@zgxlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 
0e64185162197635391645ac33861fb62f3.jpg#允许这两个ip访问 ,其他拒绝

[root@zgxlinux-01 ~]# /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@zgxlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 00:43:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@zgxlinux-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 00:45:07 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Sat, 01 Dec 2018 02:46:36 GMT
Connection: keep-alive
ETag: "5c01f60c-12"
Accept-Ranges: bytes
[root@zgxlinux-01 ~]# curl -x192.168.56.128:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 00:46:35 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Sat, 01 Dec 2018 02:46:36 GMT
Connection: keep-alive
ETag: "5c01f60c-12"
Accept-Ranges: bytes
[root@zgxlinux-01 ~]# cat /tmp/test.com.log 
127.0.0.1 - [01/Dec/2018:12:07:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [01/Dec/2018:12:08:29 +0800] test.com "/2.jsdsfdss" 404 "-" "curl/7.29.0"
127.0.0.1 - [01/Dec/2018:19:00:49 +0800] test.com "/2.jif" 404 "-" "curl/7.29.0"
127.0.0.1 - [02/Dec/2018:08:44:19 +0800] test.com "/admin" 301 "http://www.baidu.com/1.txt" "curl/7.29.0"
127.0.0.1 - [02/Dec/2018:08:45:07 +0800] test.com "/admin/" 200 "http://www.baidu.com/1.txt" "curl/7.29.0"
192.168.56.128 - [02/Dec/2018:08:46:35 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
[root@zgxlinux-01 ~]# dhclient ens37       #给ens37自动获取ip
[root@zgxlinux-01 ~]# ifconfig
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.128

  • 可以匹配正则

location ~ .*(abc|image)/.*\.php$
{
        deny all;
}

  • 根据user_agent限制

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403;
}

  •  deny all和return 403效果一样

[root@zgxlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 
6532e5969aab5e6cccb45749d0576e7a5a8.jpg

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

[root@zgxlinux-01 ~]# curl -x192.168.32.128:80 -I test.com/upload/
1.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 01:10:40 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@zgxlinux-01 ~]# mkdir /data/wwwroot/test.com/upload
[root@zgxlinux-01 ~]# echo "111111" > /data/wwwroot/test.com/upload/1.txt
[root@zgxlinux-01 ~]# curl -x192.168.32.128:80 -I test.com/upload/1.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 01:13:42 GMT
Content-Type: text/plain
Content-Length: 7
Last-Modified: Sun, 02 Dec 2018 01:13:31 GMT
Connection: keep-alive
ETag: "5c0331bb-7"
Accept-Ranges: bytes
 

 

 

12.15 Nginx解析php相关配置

  • 配置如下:

location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }

  •  fastcgi_pass 用来指定php-fpm监听的地址或者socket

 

#操作过程

[root@zgxlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf      #配置不执行,我们先做一个实验,看看能否解析php
c9f145dab6393ddb523c727bbde0c83f6c0.jpg

[root@zgxlinux-01 ~]# vim /data/wwwroot/test.com/3.php
49c8ffaf713f857ab3bc839b48e2a05e862.jpg

[root@zgxlinux-01 ~]# curl -x127.0.0.1:80 test.com/3.php       #只能显示出源码
<?php
phpinfo();

[root@zgxlinux-01 ~]# /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@zgxlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 ~]# curl -x127.0.0.1:80 test.com/3.php      #此时我们就可以看到解析php的结果了。
[root@zgxlinux-01 ~]# vi /usr/local/php-fpm/etc/php-fpm.conf
e1f90bf1441c39cf054e314b9ca2212a6f8.jpg

 

12.16 Nginx代理

 

67c45aa9d22a1ff6b0845faf409b676211b.jpg

  • 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;
    }
}

 

#操作过程

[root@zgxlinux-01 ~]# cd /usr/local/nginx/conf/vhost/
[root@zgxlinux-01 vhost]# vim proxy.conf
fff727023280b88907be965ba0e22cb7669.jpg

转载于:https://my.oschina.net/u/3959708/blog/2965303

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值