LoginSignup
5
2

More than 5 years have passed since last update.

nginxでPOSTメソッドのリダイレクトを行う

Last updated at Posted at 2019-01-28

アプリ側でやればいいじゃん、という話は無し。
保守とかで出番がありそうなので、備忘録。

前提条件

terminal
% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.2
BuildVersion:   18C54

% brew --version
Homebrew 1.9.2
Homebrew/homebrew-core (git revision 77b54; last commit 2019-01-21)
Homebrew/homebrew-cask (git revision 44e62; last commit 2019-01-22)

nginxでPOSTをリダイレクトする設定

default.conf
server {
    listen       80;
    server_name  _;
    root  /var/www/html;
    charset utf-8;

    location / {
        index  index.php;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # '/hoge' を '/test/test.php' で置き換える
    location ~^/hoge$ {
        return 307 http://localhost:8080/test/test.php?$args;
    }

    location ~ [^/]\.php(/|$) {
        root           /var/www/html;
        fastcgi_pass   php7:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

解説・補足

HTTP 307 Temporary Redirect

307はメソッド本文を変更しないことが保証されるので、POSTのリダイレクトが実現します。

$args

POSTなのにGETのようなパラメタがついている場合に渡すことができます。

IPで制御

ifは使うべきではないですが、先のをIPで振り分ける場合。

default.conf
    # '/hoge' を '/test/test.php' で置き換える
    location ~^/hoge$ {
        if ($remote_addr = "172.0.0.1") {
            return 307 http://localhost:8080/test/test.php?$args;
        }
    }

雑感

良くないと書いてあっても。不可能では無いので試しにやってみました。
アプリ側で制御できれば禁じ手のような対処をせずに済むので、そういう話に持っていくことがベストでしょうか。。。

5
2
0

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
5
2