nginx配置url重定向-反向代理

本文系統:Centos6.5_x64javascript

三臺主機:nginx主機,hostname: master.lansgg.com  IP: 192.168.10.128  
php

          apache主機,hostname: client1.lansgg.com IP:  192.168.10.129css

1、nginx 地址重定向html

2、nginx 反向代理
java

一、地址重定向:是指當使用者瀏覽某個網址時,將他導向到另外一個網址的技術。經常使用在把一串很長的網址,轉成較短的網址。由於當要傳播某網站時,經常由於網址太長,很差記憶;又有可能由於換了網路的免費網頁空間,網址又必需要變動,不知情的使用者還覺得網站關閉了。這時就能夠用網路上的轉址了。這個技術使一個網頁是可藉由不一樣的統一資源定位符(URL)連結。nginx

1.一、這 個模塊容許使用正則表達式重寫URI(需PCRE庫),而且能夠根據相關變量重定向和選擇不一樣的配置。若是這個指令在server字段中指定,那麼將在被 請求的location肯定以前執行,若是在指令執行後所選擇的location中有其餘的重寫規則,那麼它們也被執行。若是在location中執行這 個指令產生了新的URI,那麼location又一次肯定了新的URI。這樣的循環能夠最多執行10次,超過之後nginx將返回500錯誤web

正則表達式匹配,其中:
* ~ 爲區分大小寫匹配
* ~* 爲不區分大小寫匹配
* !~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配
文件及目錄匹配,其中:
* -f和!-f用來判斷是否存在文件
* -d和!-d用來判斷是否存在目錄
* -e和!-e用來判斷是否存在文件或目錄
* -x和!-x用來判斷文件是否可執行
flag標記有:
* last 至關於Apache裏的[L]標記,表示完成rewrite
* break 終止匹配, 再也不匹配後面的規則
* redirect 返回302臨時重定向 地址欄會顯示跳轉後的地址
* permanent 返回301永久重定向 地址欄會顯示跳轉後的地址
一些可用的全局變量有,能夠用作條件判斷
$args, 請求中的參數;
$content_length, HTTP請求信息裏的"Content-Length";
$content_type, 請求信息裏的"Content-Type";
$document_root, 針對當前請求的根路徑設置值;
$document_uri, 與$uri相同;
$host, 請求信息中的"Host",若是請求中沒有Host行,則等於設置的服務器名;
$limit_rate, 對鏈接速率的限制;
$request_method, 請求的方法,好比"GET"、"POST"等;
$remote_addr, 客戶端地址;
$remote_port, 客戶端端口號;
$remote_user, 客戶端用戶名,認證用;
$request_filename, 當前請求的文件路徑名
$request_body_file
$request_uri, 請求的URI,帶查詢字符串;
$query_string, 與$args相同;
$scheme, 所用的協議,好比http或者是https,好比rewrite  ^(.+)$  $scheme://example.com$1  redirect;
$server_protocol, 請求的協議版本,"HTTP/1.0"或"HTTP/1.1";
$server_addr, 服務器地址,若是沒有用listen指明服務器地址,使用這個變量將發起一次系統調用以取得地址(形成資源浪費);
$server_name, 請求到達的服務器名;
$server_port, 請求到達的服務器端口號;
$uri, 請求的URI,可能和最初的值有不一樣,好比通過重定向之類的。

rewrite 指令:可使用在 server, location, if 區域;正則表達式

語法:rewrite regex replacement flag apache

按照相關的正則表達式與字符串修改URI,指令按照在配置文件中出現的順序執行。  
能夠在重寫指令後面添加標記。  
若是替換的字符串以http://開頭,請求將被重定向,而且再也不執行多餘的rewrite指令。  
尾部的標記(flag)能夠是如下的值:
緩存

  • last - 完成重寫指令,以後搜索相應的URI或location。

  • break - 完成重寫指令。

  • redirect - 返回302臨時重定向,若是替換字段用http://開頭則被使用。

  • permanent - 返回301永久重定向。

注 意若是一個重定向是相對的(沒有主機名部分),nginx將在重定向的過程當中使用匹配server_name指令的「Host」頭或者 server_name指令指定的第一個名稱,若是頭不匹配或不存在,若是沒有設置server_name,將使用本地主機名,若是你老是想讓nginx 使用「Host」頭,能夠在server_name使用「*」通配符(查看http核心模塊中的server_name)。例如:

rewrite  ^(/download/.*)/media/(.*)\..*$  $1/mp3/$2.mp3  last;
rewrite  ^(/download/.*)/audio/(.*)\..*$  $1/mp3/$2.ra   last;
return   403;


可是若是咱們將其放入一個名爲/download/的location中,則須要將last標記改成break,不然nginx將執行10次循環並返回500錯誤。

location /download/ {
  rewrite  ^(/download/.*)/media/(.*)\..*$  $1/mp3/$2.mp3  break;
  rewrite  ^(/download/.*)/audio/(.*)\..*$  $1/mp3/$2.ra   break;
  return   403;
}

若是替換字段中包含參數,那麼其他的請求參數將附加到後面,爲了防止附加,能夠在最後一個字符後面跟一個問號:

rewrite  ^/users/(.*)$  /show?user=$1?  last;

注意:大括號({和}),能夠同時用在正則表達式和配置塊中,爲了防止衝突,正則表達式使用大括號須要用雙引號(或者單引號)。例如要重寫如下的URL:

/photos/123456

爲:

/path/to/photos/12/1234/123456.png

則使用如下正則表達式(注意引號):

rewrite  "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;

若是指定一個「?」在重寫的結尾,Nginx將丟棄請求中的參數,即變量$args,當使用$request_uri$uri&$args時能夠在rewrite結尾使用「?」以免nginx處理兩次參數串。  
在rewrite中使用$request_uri將
www.example.com重寫到example.com:

server {
   server_name www.example.com;
   rewrite ^ http://example.com$request_uri? permanent;
}

一樣,重寫只對路徑進行操做,而不是參數,若是要重寫一個帶參數的URL,可使用如下代替:

if ($args ^~ post=100){
  rewrite ^ http://example.com/new-address.html? permanent;
}

注意$args變量不會被編譯,與location過程當中的URI不一樣(參考http核心模塊中的location

示例:當訪問www.lansgg.com的時候跳轉到www.Aries.com;

    server {
        listen  80 default_server;
        server_name     www.lansgg.com lansgg.com;
        access_log      logs/lansgg.access.log main;
        error_log       logs/lansgg.error.log;
        root            /opt/nginx/nginx/html/lansgg;
        index index.html;
        rewrite ^/ http://www.Aries.com/;
        }

break 指令 可以使用server, location, if 區域; 停止Rewirte,不在繼續匹配

last 指令 可server, location, if  區域;

last與break的區別在於,last並不會中止對下面location的匹配。

測驗一下break與last的區別

    server {
    listen    80 default_server;
    server_name    www.lansgg.com lansgg.com;
    access_log    logs/lansgg.access.log main;
    error_log    logs/lansgg.error.log;
    root        /opt/nginx/nginx/html/lansgg;
    index index.html;
    location /c1.html {
    rewrite /c1.html /c2.html break;
    }
    location /c2.html {
    return 508;
    }
    }
[root@master sbin]# echo "c1" > /opt/nginx/nginx/html/lansgg/c1.html
[root@master sbin]# echo "c2" > /opt/nginx/nginx/html/lansgg/c2.html


使用break會中止匹配下面的location,直接發起請求www.lansgg.com/c1.html,他會顯示c2的內容;

wKiom1ReJwmg3h4tAABlg5WLTFc650.jpg
使用last的話,會繼續搜索下面是否有符合條件(符合重寫後的/c2.html請求)的location。此時,/c2.html恰好與面location的條件對應上了,進入花括號{}裏面的代碼執行,這裏會返回508。

    server {
    listen    80 default_server;
    server_name    www.lansgg.com lansgg.com;
    access_log    logs/lansgg.access.log main;
    error_log    logs/lansgg.error.log;
    root        /opt/nginx/nginx/html/lansgg;
    index index.html;
    location /c1.html {
    rewrite /c1.html /c2.html last;
    }
    location /c2.html {
    return 508;
    }
    }

使用firebug 能夠看到;

wKiom1ReJ_bR_WwhAACvgot1dVM022.jpg

if 指令 可以使用server, location 區域;

示例:當訪問http://www.lansgg.com網址的時候跳轉到www.Aries.com;

    server {
        listen  80 default_server;
        server_name     www.lansgg.com lansgg.com;
        access_log      logs/lansgg.access.log main;
        error_log       logs/lansgg.error.log;
        root            /opt/nginx/nginx/html/lansgg;
        index index.html;
        if ($http_host = www.lansgg.com){
        rewrite (.*) http://www.Aries.com;
        }
        }

wKioL1ReMavwbbrPAAEAO-or-Mg289.jpg


return 指令 可以使用server, location, if  區域

語法:return code 
這個指令結束執行配置語句併爲客戶端返回狀態代碼,可使用下列的值:204,400,402-406,408,410, 411, 413, 416與500-504。此外,非標準代碼444將關閉鏈接而且不發送任何的頭部。

rewrite_log  指令  可以使用server, location, if  區域


啓用時將在error log中記錄notice 標記的重寫日誌。


set 指令 可以使用server, location, if  區域

語法:set variable value 
指令設置一個變量併爲其賦值,其值能夠是文本,變量和它們的組合。  
你可使用set定義一個新的變量,可是不能使用set設置$http_xxx頭部變量的值。


uninitialized_variable_warn 指令 可以使用 http, server, location, if  區域

語法:uninitialized_variable_warn on|off  
默認值:uninitialized_variable_warn on 
開啓或關閉在未初始化變量中記錄警告日誌。  
事實上,rewrite指令在配置文件加載時已經編譯到內部代碼中,在解釋器產生請求時使用。 

expires 指令 可 http, server, location 區域

語法: expires [time|epoch|max|off]
默認值: expires off
該指令能夠控制HTTP應答中的「Expires」和「Cache-Control」的頭標,(起到控制頁面緩存的做用)。能夠在time值中使用正數或負數。「Expires」頭標的值將經過當前系統時間加上設定的 time 值來得到。
epoch 指定「Expires」的值爲 1 January, 1970, 00:00:01 GMT。
max 指定「Expires」的值爲 31 December 2037 23:59:59 GMT,「Cache-Control」的值爲10年。
-1 指定「Expires」的值爲 服務器當前時間 -1s,即永遠過時
「Cache-Control」頭標的值由指定的時間來決定:
    負數:Cache-Control: no-cache
    正數或零:Cache-Control: max-age = #, # 爲指定時間的秒數s。其餘的單位有d(天),h(小時)

"off" 表示不修改「Expires」和「Cache-Control」的值
控制圖片等過時時間爲30天,這個時間能夠設置的更長。具體視狀況而定

location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
           log_not_found off;  #不記錄404 not  found 錯誤日誌           expires 30d;
           break;       }

控制匹配/resource/或者/mediatorModule/裏全部的文件緩存設置到最長時間

        location ~ /(resource|mediatorModule)/ {
                root    /opt/demo;
                expires max;
        }

設定某個文件的過時時間;這裏爲600秒,並不記錄訪問日誌

location ^~ /html/scripts/loadhead_1.js {
access_log   off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}


設置GZIP


通常狀況下壓縮後的html、css、js、php、jhtml等文件,大小能降至原來的25%,也就是說,本來一個100k的html,壓縮後只剩下25k。這無疑能節省不少帶寬,也能下降服務器的負載。
在nginx中配置gzip比較簡單
通常狀況下只要在nginx.conf的http段中加入下面幾行配置便可

   gzip  on;
   gzip_min_length  1000;
   gzip_buffers     48k;
   gzip_types       text/plain application/x-javascript text/css text/html application/xml;

能夠經過網頁gzip檢測工具來檢測網頁是否啓用了gzip


臨時重定向示例:訪問www.lansgg.com/c 重定向到www.lansgg.com/cc 

編輯nginx.conf

    server {
        listen  80 default_server;
        server_name     www.lansgg.com lansgg.com;
        access_log      logs/lansgg.access.log main;
        error_log       logs/lansgg.error.log;
        root            /opt/nginx/nginx/html/lansgg;
        index index.html;
        rewrite ^/c/(.*)$ http://www.lansgg.com/cc/$1;
        }
        
[root@master lansgg]# tree
.
├── c
│   └── index.html
├── cc
│   └── index.html
├── index.html
└── it.jpg

2 directories, 4 files

訪問http://www.lansgg.com/c 會跳轉到http://www.lansgg.com/cc

wKioL1RePF6hWtH5AAFKE7zHvzw477.jpg

302即爲臨時重定向;

永久重定向(隱含重定向)

編輯nginx.conf

    server {
        listen  80 default_server;
        server_name     www.lansgg.com lansgg.com;
        access_log      logs/lansgg.access.log main;
        error_log       logs/lansgg.error.log;
        root            /opt/nginx/nginx/html/lansgg;
        index index.html;
        rewrite ^/c/(.*)$ /cc/$1;
        }

訪問 http://www.lansgg.com/c/ 頁面顯示的是跳轉後的頁面,但是url卻沒有變化;firebug也看不到302代碼信息;如今它實際上是301;


二、反向代理:是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個服務器。

2.一、配置nginx實現反向代理;

需求:訪問http://192.168.10.128/other 返回 apache主機的other目錄下的Index.html

wKioL1Rd21mwBFavAAIkMKYLkDs383.jpg

涉及nginx指令:

語法:proxy_pass URL   
可以使用字段:location, location中的if字段      
這個指令設置被代理服務器的地址和被映射的URI,地址可使用主機名或IP加端口號的形式,例如:
proxy_pass http://192.168.10.129/url

2.二、配置nginx配置文件nginx.conf

    server {
    listen    80 default_server;
    server_name    www.lansgg.com lansgg.com;
    access_log    logs/lansgg.access.log main;
    error_log    logs/lansgg.error.log;
    root        /opt/nginx/nginx/html/lansgg;
    location / {
        index index.html;
        }
    location /other {
    proxy_pass          http://192.168.10.129/other;
    proxy_set_header    X-Real-IP $remote_addr; 
        }
    }

2.三、配置client1

mkdir /var/www/html/other
echo "192.168.10.129" > /var/www/html/other/index.html

2.四、測試;

訪問url:    http://www.lansgg.com/other     你會發現跳轉到了 : http://192.168.10.129/other/

wKiom1Rd6hKzwCh2AAB13d4xNOk488.jpg

查看日誌:

[root@client1 ~]# tail -f /var/log/httpd/access_log 
192.168.10.1 - - [06/Nov/2014:21:25:44 +0800] "GET /other/ HTTP/1.1" 200 15 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"
相關文章
相關標籤/搜索