LoginSignup
38

More than 5 years have passed since last update.

nginxでURLに渡すパラメータのメモ

Last updated at Posted at 2016-03-07

いつものように個人的なメモ

URLにパラメータを付与した際の動作
hoge.jp へのアクセスを foo.jp へ飛ばすと想定

関連:Nginxのリダイレクト設定のメモ

1. 何も無し

  location / {
    rewrite ^(.*)$ http://foo.jp redirect;
  }

hoge.jp?bar -> foo.jp/?bar となる

2. $request_uri

  location / {
    rewrite ^(.*)$ http://foo.jp$request_uri redirect;
  }

hoge.jp?bar -> foo.jp/?bar?bar となる

3. $uri

  location / {
    rewrite ^(.*)$ http://foo.jp$uri redirect;
  }

hoge.jp?bar -> foo.jp/?bar となる

4. $uri$args

  location / {
    rewrite ^(.*)$ http://foo.jp$uri$args redirect;
  }

hoge.jp?bar -> foo.jp/bar?bar となる

5. $uri?$args

  location / {
    rewrite ^(.*)$ http://foo.jp$uri?$args redirect;
  }

hoge.jp?bar -> foo.jp/?bar&bar となる

6. ?止め

  location / {
    rewrite ^(.*)$ http://foo.jp? redirect;
  }

hoge.jp?bar -> foo.jp/ となる

7. set $args '';

  location / {
    set $args '';
    rewrite ^(.*)$ http://foo.jp$request_uri redirect;
  }

hoge.jp?bar -> foo.jp/?bar となる

メモ

動作に差がでるので注意

参考

ようへいの日々精進XP: 深夜メンテナンスに役立ちそうな nginx 小ネタ
stackoverflow: Remove parameters within nginx rewrite

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38