Nginx防盜鏈、訪問控制、解析PHP相關配置、Nginx代理

​11月28日任務javascript

12.13 Nginx防盜鏈php

12.14 Nginx訪問控制html

12.15 Nginx解析php相關配置java

12.16 Nginx代理linux

 

 

12.13 Nginx防盜鏈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;
}
 vim

#操做過程app

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

[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 reloadsocket

[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 
#容許這兩個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 

[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

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

[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

 

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

 

#操做過程

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

相關文章
相關標籤/搜索