nginx替換響應頭(重點:如何在替換時加上if判斷)

  在實現微信小程序內嵌非業務域名時,經過nginx作鏡像網站繞太小程序業務域名檢測,但有一些表單頁面提交後會返回一個302狀態,由響應頭Location的值決定提交成功後的跳轉地址。那麼問題來了,這個地址也是屬於非業務域名,這個時候咱們就須要將這個響應頭也替換掉,那麼nginx如何替換響應頭呢,請看下面教程:php

  1、安裝使用ngx_headers_more模塊定製響應頭:html

    ngx_headers_more 用於添加、設置和清除輸入和輸出的頭信息。nginx沒有內置該模塊,須要另行添加。nginx

    (1)下載地址:https://github.com/openresty/headers-more-nginx-module/tagsgit

    (2)平滑升級nginx(參考上篇爲nginx平滑添加SSL模塊的文章:http://www.cnblogs.com/kenwar/p/8295907.html 添加參數add-module=/解壓縮後文件路徑)github

    (3)指令說明(我這邊只用到設置響應頭的指令,因此只介紹一個指令,若有興趣請自行查找其使用說明):      正則表達式

        more_set_headers
        語法:more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
        默認值:no
        配置段:http, server, location, location if
        階段:輸出報頭過濾器
        替換(若有)或增長(若是不是全部)指定的輸出頭時響應狀態代碼與-s選項相匹配和響應的內容類型的-t選項指定的類型相匹配的。
        若是沒有指定-s或-t,或有一個空表值,無需匹配。所以,對於下面的指定,任何狀態碼和任何內容類型都講設置。小程序

        more_set_headers "Server: my_server";
        more_set_headers "Server: my_server";

        具備相同名稱的響應頭老是覆蓋。若是要添加頭,能夠使用標準的add_header指令代替。
        單個指令能夠設置/添加多個輸出頭。如:後端

        more_set_headers 'Foo: bar' 'Baz: bah';
        more_set_headers 'Foo: bar' 'Baz: bah';

        在單一指令中,選項能夠屢次出現,如:微信小程序

        more_set_headers -s 404 -s '500 503' 'Foo: bar';

        more_set_headers -s 404 -s '500 503' 'Foo: bar';

        等同於:微信

        more_set_headers -s '404 500 503' 'Foo: bar';

        more_set_headers -s '404 500 503' 'Foo: bar';

        新的頭是下面形式之一:
        Name: Value
        Name:
        Name
        最後兩個有效清除的頭名稱的值。Nginx的變量容許是頭值,如:

        set $my_var "dog";
        more_set_headers "Server: $my_var";
        set $my_var "dog";
        more_set_headers "Server: $my_var";

        注意:more_set_headers容許在location的if塊中,但不容許在server的if塊中。下面的配置就報語法錯誤:

        # This is NOT allowed!
        server {
        if ($args ~ 'download') {
          more_set_headers 'Foo: Bar';
        }
        ...
        }

  2、簡單替換302狀態下的響應頭Location:

    location /{
      more_set_header "Location" "https://www.demo.com/xxx/index.html"        
    }

  3、(重點)使用正則表達式有選擇的替換Location:

    咱們若是須要根據響應頭裏的內容來選擇何種替換方式,該怎麼作?

    需求:在location中判斷響應頭Location字段若是值爲a(假設值),則將Location設置爲b,其餘忽略

    (1)初步嘗試:

    location /{
      if($upstream_http_Location ~ a){
        more_set_header "Location" "https://www.demo.com/xxx/index.html"
      }
        
    }

      然而這裏的if怎麼都進不去,經過google得知是由於,在請求傳遞到後端以前,if就已經判斷了,因此$upstream_http_Location是沒有值的,這裏能夠使用一個map來有根據響應頭的值有條件的執行操做

 map $upstream_http_Location $location{
    ~a https://www.democom/xxx/success.html;
    default $upstream_http_Location;
}
server{
    listen 80;
    listen 443 ssl;
    ssl_certificate /usr/local/nginx/ssl/www3.xiaolintong.net.cn/www3.xiaolintong.net.cn-ca-bundle.crt;
    ssl_certificate_key /usr/local/nginx/ssl/www3.xiaolintong.net.cn/www3.xiaolintong.net.cn.key;
    autoindex on;
    #開啓讀取非nginx標準的用戶自定義header(開啓header的下劃線支持)
    underscores_in_headers on;
    server_name xxxxxxxxx;
    access_log /usr/local/nginx/logs/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
........
     location ^~/f/ {
     more_set_headers -s '302' 'Location $location';

 }
.......
}

      這裏僅提供一個思路,請根據本身的需求靈活的使用map

相關文章
相關標籤/搜索