nginx訪問控制、rewrite應用、代理設置

           nginx訪問控制、rewrite應用、代理設置

一、訪問控制

在這裏依然還是以default2.conf虛擬主機爲例,配置文件位置default2.conf

1、允許某個ip訪問 ,需要在default2.conf配置配文件中添加,具體如下圖:

wKioL1WPdeyBrf72AALWY0xYijA774.jpg

規則如下:   

allow 127.0.0.1;

allow 192.168.21.97;

deny all;

只允許127.0.0.1192.168.21.97來訪問,其他的全部拒絕

 

退出保存

1)檢查配置文件

2)重置配置文件

3)測試

     允許ip測試

[root@mysql ~]# curl -x192.168.21.97:80 http://www.guhantai.cn/1.jpg-I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 22 Jun
2015 15:21:05 GMT
Content-Type:
image/jpeg
Content-Length: 0
Last-Modified: Mon,
22 Jun 2015 01:16:22 GMT
Connection:
keep-alive
ETag:
"558761e6-0"
Expires: Thu, 02 Jul
2015 15:21:05 GMT
Cache-Control:
max-age=864000
Accept-Ranges: bytes

 

其他ip測試

wKioL1WPdjaT9ZbkAAGcQYE6x-8054.jpg

 2 禁止某個IP訪問

      禁止本地IP地址訪問

wKioL1WPdmLzA4ObAAMMLey8w7k332.jpg

規則如下:

  deny 127.0.0.1;

 allow all;

 

1)保存退出

2)檢查配置文件

 /usr/local/nginx/sbin/nginx -t

3)重置配置文件

[root@mysql ~]# /usr/local/nginx/sbin/nginx -s reload

4)測試

使用127.0.0.1來個訪問guhantai.cn

[root@mysql ~]# curl -x127.0.0.1:80 http://www.guhantai.cn/1.jpg
<html>
<head><title>403 Forbidden</title></head>  #提示被禁止
<body
bgcolor="white">
<center><h1>403
Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

 

使用192.168.21.97測試

[root@mysql ~]# curl -x192.168.21.97:80 http://www.guhantai.cn/1.jpg-I
HTTP/1.1 200 OK   #訪問是OK的
Server: nginx/1.6.2
Date: Mon, 22 Jun
2015 14:52:58 GMT
Content-Type:
image/jpeg
Content-Length: 0
Last-Modified: Mon,
22 Jun 2015 01:16:22 GMT
Connection:
keep-alive
ETag:
"558761e6-0"
Expires: Thu, 02 Jul
2015 14:52:58 GMT
Cache-Control:
max-age=864000
Accept-Ranges: bytes

 

3、某個目錄下限制IP訪問,這個的作用主要就是是針對一些特定的目錄來限制訪問,依然以default2.conf虛擬主機配置爲例

      只允許192.168.61.0/24這個網段對w目錄訪問,其他的全部拒絕,配置文件位置:/usr/local/nginx/conf/vhosts/default2.conf

如圖:

wKioL1WPdpHQdUCDAAKBibaagBA134.jpg

規則寫法如下:    

location /w/ {

        allow 192.168.61.0/24;   #這裏的IP段就是允許的IP

        deny all;

        location ~ \.php$ {

            include fastcgi_params;

            fastcgi_pass unix:/tmp/php-fcgi.sock;

            fastcgi_index index.php;

            fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

        }

    }

一定要加上location php,不然是不會去解析php

 

退出保存

檢查配置文件

[root@mysql w]# /usr/local/nginx/sbin/nginx -t
 

重啓nginx

/etc/init.d/nginx restart

 

測試,我自己的win7 電腦ip21網段的,所以無法訪問

wKiom1WPdYKziEH8AAF1AlVko6s890.jpg

4在nginx的配置文件nginx.conf中加入

include deny.ip; 

 

重啓一下nginx的服務:/usr/local/nginx/sbin/nginx  reload就可以生效了。 

 

deny.ip 的格式中也可以用deny all; 

如果你想實現這樣的應用,除了幾個IP外,其他全部拒絕,

那需要你在deny.ip 中這樣寫

allow 1.1.1.1; 

allow 1.1.1.2;

deny all;

 

5有時候會根據目錄來限制php解析,這裏依然還是以/usr/local/nginx/conf/vhosts/default2.conf 虛擬主機爲例的


location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.php$   #括號裏面的表示或者的意思,只要是括號裏面的,不管哪一個都禁止

{

        deny all;

}

 

這個的意思就是說訪問這些目錄後的php全部不解析

 

添加在這個位置,如下圖:

wKiom1WPdaygaVFrAAMFFeL8c_U945.jpg

退出保存

 

檢查配置文件

/usr/local/nginx/sbin/nginx -t

 

重啓nginx

 /etc/init.d/nginx restart

 

測試訪問www.guhantai.cn 後面帶image目錄下後綴爲.php的文件時,提示拒絕訪問

[root@mysql ~]# curl -x192.168.21.97:80 http://www.guhantai.cn/wer/image/23.php

<html>

<head><title>403 Forbidden</title></head>

<body bgcolor="white">

<center><h1>403 Forbidden</h1></center>

<hr><center>nginx/1.6.2</center>

</body>

</html>

 

 

6使用 user_agent 控制客戶端訪問 ,就是可以限制某些瀏覽器來訪問,只需要將瀏覽器的標示寫入到規則裏面就可以,都是在nginx虛擬主機配置文件中設置的

代碼如下:

 if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){

            return 403;

    }

 

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }

 

 

二、nginx的rewrite應用

      1rewrite作用

           重寫URL,或者修改字符串。需要注意的是重寫URL只對相對路徑有效,如果想要對主機名,要使用if語句

     2僞靜態rewrite規則,一下這規則可以作爲一個標準的模板,需要的時候直接複製粘貼就可以,當然這個也是寫在虛nginx虛擬主機的配置文件中的

         

僞靜態rewrite規則

    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;

    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;

    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;

    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;


參考文檔    :               

Rewrite設置及示例 http://www.lishiming.net/thread-239-1-1.html

nginx $document_uri 參數使用 http://www.lishiming.net/thread-993-1-1.html

nginx的301與302如何配置 http://www.lishiming.net/thread-4840-1-1.html

nginx rewrite不支持if 嵌套也不支持邏輯或和邏輯並  http://www.lishiming.net/thread-4842-1-1.html

 

三、 nginx 代理

       一般在真實生產環境中,主要是代理自己的網站,而不是別人的網站。

1、在/usr/local/nginx/conf/vhosts目錄下寫一個proxy.conf文件

      寫入以下代碼:

server {

            listen 80;

            server_name www.baidu.com; #這裏的域名就是所要代理的域名

 

            location / {

                proxy_pass      http://115.239.210.27/; #這裏的IP地址一定要是代理域名的真實IP地址纔可以的

                proxy_set_header Host   $host;

                proxy_set_header X-Real-IP      $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            }

#            access_log  /home/logs/aaa_access.log combined;

        }

退出保存

檢查配置文件

重置nginx服務

 

測試

沒有配置之前:

[root@mysql ~]# curl -x 192.168.21.97:80 www.baidu.com
<!DOCTYPE
html>
<html>
<head>
<title>Welcome
to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial,
sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to
nginx!</h1>
<p>If you see
this page, the nginx web server is successfully installed and
working. Further
configuration is required.</p>
 
<p>For online
documentation and support please refer to
<a
href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support
is available at
<a
href="http://nginx.com/">nginx.com</a>.</p>
 
<p><em>Thank
you for using nginx.</em></p>
</body>
</html>

 

配置代理之後:

[root@mysql vhosts]#curl -x 192.168.21.97:80 www.baidu.com
<!DOCTYPE
html><!--STATUS OK--><html><head><meta
http-equiv="content-type"
content="text/html;charset=utf-8"><meta
http-equiv="X-UA-Compatible" content="IE=Edge"><meta
content="always" name="referrer"><meta
name="theme-color" content="#2932e1"><link
rel="shortcut icon" href="/favicon.ico"
type="image/x-icon" /><link rel="icon" sizes="any"
mask href="//www.baidu.com/img/baidu.svg"><link
rel="dns-prefetch" href="//s1.bdstatic.com"/><link
rel="dns-prefetch" href="//t1.baidu.com"/><link
rel="dns-prefetch" href="//t2.baidu.com"/><link
rel="dns-prefetch" href="//t3.baidu.com"/><link
rel="dns-prefetch" href="//t10.baidu.com"/><link
rel="dns-prefetch" href="//t11.baidu.com"/><link
rel="dns-prefetch" href="//t12.baidu.com"/><link
rel="dns-prefetch"
href="//b1.bdstatic.com"/><title>百度一下,你就知道</title>

………...內容太多了,就不寫了

 

 

 

2如果後端有多臺機器,使用如下代碼來寫。

代碼如下:

upstream bbb #這裏是設置這兩個ip的別名

{

            server  1.2.3.1:80;

            server  1.2.3.4:80;

}

 

server {

        listen 80;

        server_name bbb.com;

 

        location / {

                proxy_pass      http://bbb/; #這裏的bb是別名

                proxy_set_header Host   $host;

                proxy_set_header X-Real-IP      $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

#            access_log  /home/logs/bb_access.log combined;

}

 

3代理一個服務器上所有域名 

首先在vhosts目錄下需要建立兩個文件,一個是servername 列表文件,一個是虛擬主機配置文件

兩個文件內容分別爲

(1) servername

server_name www.123.net.cn  www.alsdjfl.com   www.asdfa1.com;  //就這麼簡單一行,當然這個server_name 還可以繼續添加的

 

(2) 虛擬主機配置文件

server {

            listen 80;

            include vhosts/servername; // 這裏的文件就是上邊那個servername列表文件

            location / {

                proxy_pass     http://1.2.1.2/;  //這裏就是需要做代理的服務器ip地址了

                proxy_set_header Host   $host;

                proxy_set_header X-Real-IP      $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            }

            access_log  /dev/null; 

        }


參考文檔: 

根據訪問的目錄來區分後端的web http://www.lishiming.net/thread-920-1-1.html

針對請求的uri來代理 http://www.lishiming.net/thread-1049-1-1.html


實驗配置代碼參考:

server
{
    listen 80;
    server_name www.1.com www.a.com www.b.com;

#域名跳轉   
   if ($host != 'www.a.com' ) {
        rewrite  ^/(.*)$  http://www.a.com/$1  permanent;
    }
    index index.html index.htm index.php;
    root /data/www;

#    location  /uc_server/ {
#          auth_basic              "Auth";
#          auth_basic_user_file   /usr/local/nginx/conf/.htpasswd;
#    }

#黑名單
#    deny 127.0.0.1;
#    allow all;
#白名單
#    allow 127.0.0.1;
#    allow 192.168.31.141;
#    deny all;


#某個目錄下限制ip
    location /uc_server/ {
        allow 192.168.31.0/24;
        deny all;
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
        }
    }

#針對目錄限制php解析
    location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.php$
    {
        deny all;
    }

#根據user_agent控制
    if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){
            return 403;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }

#緩存時間
#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#    {
#          expires      30d;
#          access_log off;
#    }

    location ~ .*\.(js|css)?$
    {
          expires      12h;
          access_log off;
    }

#防盜鏈
    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
         expires 10d;
         valid_referers none blocked server_names *.1.com *.a.com *.b.com *.baidu.com\
         *.google.com *.google.cn *.soso.com ;
         if ($invalid_referer) {
              return 403;
              #rewrite ^/ http://www.example.com/nophoto.gif;
         }
         access_log off;
    }

# 僞靜態rewrite規則
    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;

#docment_uri
#    if ($document_uri !~ 'abc')
#    {
#           rewrite ^/(.*)$ /abc/$1 redirect;
#    }


    access_log /home/logs/discuz.log combined_realip;
}

                筆記有錯誤的地方還請大神指正,小白會繼續修改



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章