LoginSignup
1
1

More than 5 years have passed since last update.

sinatraをサブディレクトリ(sub uri)で動かす

Posted at

先に結論

sinatraに飛ばすloationの中で、リクエストURIルートを差し替えておく。
以下、ningx.confの抜粋

location /subdir/ {
  rewrite /subdir/(.*) /$1  break; #ここでURLを差し替える
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_pass http://supapp;  #upstream で定義したドメインソケットにプロキシ
}

こう書いていいこと

http://domain/subdir/

とアクセスされても、

get '/' do

で、受け取れるようになる。配置するディレクトリ(実際にはサブURI)によって、アプリのソースを書き換えないでいい。

副作用

htmlの中に、
src="/js/xxxx.js"
なんて書いてると、やっぱりディレクトリがずれちゃうので、そういうことをしたかったら、Railsみたいな、フルスペックのフレームワークに頼った方がよくなる。Railsでもサブディレクトリをつかうのはちょっと面倒。

nginxとアプリケーションサーバを使いたいのなら、アプリケーション単位でルートを切るのが無難。公開ディレクトリにフォルダ解凍するだけで別アプリが配置できるのは、Apche+phpの特殊機能と割り切った方がいい。

1
1
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
1
1