2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nginx で下層ディレクトリにアプリをインストールしたときの REQUEST_URI の書き換え設定

Last updated at Posted at 2020-03-22

やりたいこと

URL は http://example.com/foo/bar/action_name?id=1 なんだけど、 REQUEST_URI には /action/name?id=1 が入っててほしい場合の nginx の config の書き方。

設定例

/etc/nginx/conf.d/sample.conf
# アクセスされたURLの /foo/bar/ 以下の部分を、後で使うため変数 $request_uri_for_cgi, $query_string_for_cgi にセットする。
if ($request_uri ~* ^/foo/bar/(.*)$) {
    set $request_uri_for_cgi "/$1";
}
if ($request_uri ~* ^/foo/bar/(.*)\?(.*)$) {
    set $query_string_for_cgi "$2";
}

location ~* /foo/bar/(.*)$ {
    include /etc/nginx/fastcgi_params;

    fastcgi_pass   unix:/var/run/php-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    # REQUEST_URI と QUERY_STRING を設定する
    fastcgi_param REQUEST_URI $request_uri_for_cgi;
    fastcgi_param QUERY_STRING $query_string_for_cgi;

    try_files $uri $uri/ /foo/bar/index.php;
}

※nginx の設定は初めてなうえ、試行錯誤で作ったものなので、無駄な行があったりするかと思います。

結果

http://ecample.com/foo/bar/search?id=1234 にアクセスが有ると、 http://example.com/foo/bar/index.php が呼ばれて、 $_SERVER['REQUEST_URI']search?id=1234, $_SERVER['QUERY_STRING']id=1234 が入るようになります。

2
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?