天天看點

nginx配置通路控制、rewrite應用、nginx代理

一、通路控制

限制隻讓某個ip通路:

    allow 192.168.1.100;

    deny all;

限制隻有本地位址可以通路,白名單;

    allow 127.0.0.1;

拒絕本地通路,黑名單:

    deny 127.0.0.1;

    allow all;

deny all 直接拒絕所有,下面的allow就不生效了。

1

2

3

4

5

6

7

8

9

10

<code>[root@localhost vhosts]</code><code># vi default.conf</code>

<code>server</code>

<code>{</code>

<code>    </code><code>listen 80 default_server;</code>

<code>    </code><code>server_name localhost;</code>

<code>    </code><code>index index.html index.htm index.php;</code>

<code>    </code><code>root </code><code>/usr/local/nginx/html</code><code>;</code>

<code>    </code><code>deny all;</code>

<code>    </code><code>allow 2.2.2.2;</code>

<code>}</code>

<code>[root@localhost vhosts]</code><code># curl -x127.0.0.1:80  192.168.20.30/index.html -I</code>

<code>HTTP</code><code>/1</code><code>.1 403 Forbidden</code>

<code>Server: nginx</code><code>/1</code><code>.6.2</code>

<code>Date: Fri, 15 May 2015 08:46:05 GMT</code>

<code>Content-Type: text</code><code>/html</code>

<code>Content-Length: 168</code>

<code>Connection: keep-alive</code>

禁止某個IP或者IP段通路站點的設定方法:

1、首先建立下面的配置檔案放在nginx的conf目錄下面,命名為deny.ip  

<code># cat /usr/local/nginx/conf/deny.ip</code>

<code>deny 192.168.20.10;</code>

<code>deny 192.168.20.11;</code>

<code>deny 192.168.10.0</code><code>/24</code><code>;</code>

2、在nginx的配置檔案nginx.conf中加入:include deny.ip;

3、重新開機一下nginx的服務:/usr/local/nginx/sbin/nginx -s reload 就可以生效了。

deny.ip 的格式中也可以用deny all; 如果你想實作這樣的應用,除了幾個IP外,其他全部拒絕,

allow 1.1.1.1;

allow 1.1.1.2;

deny all;

針對目錄限制php解析:

location ~ .*(diy|template|attachments|forumdata|attachment|image/.*\.php$ {

        deny all;

}

根據 user_agent 控制用戶端通路

        location / {

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

                return 403;

                }

        }

實驗:限定IE7.0 和 curl 不能通路;IE8.0可以正常打開;

    location / {

        if ($http_user_agent ~ 'MSIE 7.0|curl'){

curl -A 代表浏覽器辨別agent;模拟浏覽器辨別;測試包含bingbot/2.0,MSIE 7.0,curl的傳回值為403;

<code>[root@localhost vhosts]</code><code># curl -x127.0.0.1:80 -A "aabingbot/2.0ss" www.111.com -I</code>

<code>Date: Fri, 15 May 2015 21:09:35 GMT</code>

<code>[root@localhost vhosts]</code><code># curl -x127.0.0.1:80 -A "MSIE 7.0aa"  www.111.com -I</code>

<code>Date: Fri, 15 May 2015 21:13:50 GMT</code>

<code>Content-Length: 570</code>

<code>Date: Fri, 15 May 2015 09:15:27 GMT</code>

二、nginx的rewrite應用

現在有這樣的的需求,通路 www.abc.com  請求到 www.abc.com/abc/

在nginx虛拟主機配置檔案中加入 :

   if ($document_uri !~ 'abc') 

    {

       rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;

    }

而不是單獨加一句rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;

如果隻加rewrite 規則,而不限定條件,那麼會造成死循環。  

實驗測試,隻加一行rewrite規則,redirect 302 臨時重定向;

11

12

13

14

15

16

17

<code>[root@localhost vhosts]</code><code># cat 111.conf </code>

<code>    </code><code>listen 80;</code>

<code>    </code><code>server_name www.aaa.com aaa.com;</code>

<code>    </code><code>root </code><code>/data/www2</code><code>;</code>

<code> </code> 

<code>    </code><code>rewrite ^/(.*)$ </code><code>/aaa/</code><code>$1 redirect;</code>

<code>   </code> 

<code>    </code><code>location ~ \.php$ {</code>

<code>        </code><code>include fastcgi_params;</code>

<code>        </code><code>fastcgi_pass unix:</code><code>/tmp/php-fcgi-www2</code><code>.sock;</code>

<code>        </code><code>fastcgi_index index.php;</code>

<code>        </code><code>fastcgi_param SCRIPT_FILENAME </code><code>/data/www2</code><code>$fastcgi_script_name;</code>

<code>    </code><code>}</code>

<code>[root@localhost vhosts]</code><code># curl -x127.0.0.1:80  www.aaa.com/asdfasdfa -I</code>

<code>HTTP</code><code>/1</code><code>.1 302 Moved Temporarily</code>

<code>Date: Fri, 15 May 2015 09:54:10 GMT</code>

<code>Content-Length: 160</code>

<code>Location: </code>

<code>http:</code><code>//www</code><code>.aaa.com</code><code>/aaa/asdfasdfa</code>

加入if判斷,域名不比對aaa的時候跳轉到aaa位址下;浏覽器通路跳轉正确,出現404錯誤是我們做實驗沒有這個目錄;

    if ($document_uri !~ 'aaa')

     {

     rewrite ^/(.*)$ /aaa/$1 redirect;

     }

<a href="http://s3.51cto.com/wyfs02/M01/6D/10/wKiom1VbCLHyjTsoAACXJYBk58c903.jpg" target="_blank"></a>

三、nginx代理配置

/conf/vhosts/目錄下,建立一個proxy.conf 寫入下面的内容:

<code>[root@localhost vhosts]</code><code># cat proxy.conf</code>

<code>server {</code>

<code>            </code><code>listen 80;</code>

<code>            </code><code>server_name  </code>

<code>            </code><code>location / {</code>

<code>                </code><code>proxy_pass      http:</code><code>//180</code><code>.97.33.108/;</code>

<code>                </code><code>proxy_set_header Host   $host;</code>

<code>                </code><code>proxy_set_header X-Real-IP      $remote_addr;</code>

<code>                </code><code>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</code>

<code>            </code><code>}</code>

<code>        </code><code>}</code>

代理baidu.com,proxy_pass填寫baidu的ip位址;

dig指令檢視baidu的别名和對應的ip位址;

;; ANSWER SECTION:

如果代理的機器有多台,可以實作負載均衡。bbb為自定義的内容;

<code>upstream bbb</code>

<code>            </code><code>server  180.97.33.108:80;</code>

<code>            </code><code>server  180.97.33.107:80;</code>

<code>        </code><code>listen 80;</code>

<code>        </code><code>server_name  </code>

<code>        </code><code>location / {</code>

<code>                </code><code>proxy_pass      http:</code><code>//bbb/</code><code>;</code>

<b></b>

<b>本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1652905,如需轉載請自行聯系原作者</b>