Last active
June 7, 2019 05:48
-
-
Save karia/83a51c10e63c92dee9217908fe8b51e9 to your computer and use it in GitHub Desktop.
nginxでlocationと正規表現を使い、条件によって異なる複数の内部ホストにproxyし、パスもスラッシュ区切りで改変する例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set $a_host internal-a.example.com; | |
set $b_host internal-b.example.com; | |
# locationブロックでは3個引数があるかどうかをチェックしてる(単に /a 前方一致だと関係無いリクエストがlocationブロックに紛れ込むため) | |
location ~ ^/a/(.+)/(.+)/(.+)$ { | |
rewrite ^/a/(.+)/(.+)/(.+)$ /x=$1,y=$2/$3 break; | |
proxy_pass https://$a_host; | |
} | |
# /a/nanraka/seikihyougen/hoge に来たリクエストが https://internal-a.example.com/x=nanraka,y=seikihyougen/hoge にproxyされる | |
location ~ ^/b/(.+)/(.+)/(.+)$ { | |
rewrite ^/b/(.+)/(.+)/(.+)$ /x=$1,y=$2/$3 break; | |
proxy_pass https://$b_host; | |
} | |
# /b/nanraka/seikihyougen/hoge に来たリクエストが https://internal-b.example.com/x=nanraka,y=seikihyougen/hoge にproxyされる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment