Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@pickles

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

More than 1 year has passed since last update.

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

前提条件

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;
        }
    }

雑感

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

2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
pickles
ぺちっとな

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?