2016-07-20 4 views
0

これはこの質問の更新版です。今許可されている文字列や変数のドキュメントの組み合わせによるNginxの地図の中で複数の変数を補間する地図の指示

Variable interpolation inside Map directive

(私は、元にコメントすることはできませんので、私はここに質問をしたことがありません)。ここに見られるように:ここでhttp://nginx.org/en/docs/http/ngx_http_map_module.html#map

とを:https://trac.nginx.org/nginx/ticket/663

得られた値は、テキスト、変数(0.9.0)、及びそれらの組み合わせ(1.11.0)を含むことができます。

しかし、私はまだそれを動作させることはできません。

リダイレクトが多数あり、次のようにmapディレクティブを使用して実装しようとしています。

map $uri $redirect { 
    /wrong/path /right/path/; 
    ~^/another/wrong/path/(?<path>.*)$ $path 
} 

は、だから私は、変数が問題ではないことを知っている:これはこれは正常に動作し、文字列リテラル/another/right/path/$path

を返し

map $uri $redirect { 
    /wrong/path /right/path/; 
    ~^/another/wrong/path/(?<path>.*)$ /another/right/path/$path; 
} 

:ここ

https://serverfault.com/questions/450325/nginx-and-try-files-try-named-location-with-rewrites-before-fallbackは、例えば、マッピングであります。

マップディレクティブで文字列と変数を組み合わせる方法を知っている人はいますか?私はここで何が欠けていますか?

私はバージョン1.11.2です。

答えて

0

以下の例では、文字列を組み合わせることができます。

map $host $_env_right_url { 
    default right-path.com; 
} 

map $uri $redirect_uri { 
    ~^/wrong/path/?$ https://$_env_right_url/rightPath; 
    ~^/second/wrong/path/?$ https://$_env_right_url/second/rightPath; 
} 

server { 
    listen 80; 
    server_name WRONG-path.com; 

    location/{ 
     try_files $uri $uri/ @redirect-map; 
    } 

    location @redirect-map { 
     if ($redirect_uri) { # redirect if the variable is defined 
      return 301 $redirect_uri; 
     } 
    } 
}