12
12

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 5 years have passed since last update.

Scala Play のサービスを nginx を使ってサブディレクトリに置くときに嵌ったこと

Last updated at Posted at 2015-02-19

起こったこと

Play で作ったサービスを nginx を使って、サブディレクトリに置きたかったんですが、その際に嵌ったので記録しておきます。

例えば、http://example.com/myapp/ にアクセスした時に、サービス自体にはアクセスできるのですが、css とか js とかの assets の類で404が出てしまいました。

その時の nginx の設定ファイルはこんな感じ。

default.conf
upstream play_app {
  server example.com:9000;
}

server {
    listen       80;
    server_name  example.com;

    location /myapp/ {
      proxy_pass http://play_app;
    }
}

トップページ自体にはアクセス可能ですが、assets へのパスが、http://example.com/assets/javascripts/index.js のようになってしまい、404が出てしまっていました。

どうしたか

基本的には、以下の記事のとおりにすれば、解決出来ました。
http://nil639.blogspot.jp/2013/06/play.html

記事では、Apache を使っていますが nginx 的に書くと、上記のdefault.confを次のように書き換えます。

default.conf
upstream play_app {
  server example.com:9000;
}

server {
    listen       80;
    server_name  example.com;

    location /myapp/ {
      proxy_pass http://play_app/myapp;
    }
}

その上で、 play の conf/application.confで、application.contextを指定します。

conf/application.conf
application.context = "/myapp" 

※追記 (2015/02/19 12:48)
確認したらちゃんと動いてなかったので再度検証しました。
最後に/を入れないとうまく行きませんでした。

conf/application.conf
application.context = "/myapp/"

※追記(2015/02/19 19:57)
もう少し調べてみたら、activator run等のコマンドを実行するタイミングで指定することもできるようです。

activator run -Dapplication.context="/myapp/"

application.contextを指定すると、全ての route の先頭に prefix がつくようになるので、記事のように全て指定しなくても大丈夫です。

ここまでやると、例えばhttp://example.com/myapp/にアクセスすると、サービスの /myapp に渡されます。assets のパスはhttp://example.com/myapp/assets/javascripts/index.jsになりますので、サービスの /myapp/assets/javascripts/index.jsに渡されるようになり、今度はちゃんとアクセスできます。

これで、無事解決しました。

おわりに

あとは、activator run等のコマンドを打った時に、application.contextを指定することができれば、サービスの設定ファイルをいじらなくても、nginx の設定を変更するだけでここまでの作業が可能になるのですが、そのあたりのやり方がよくわかりません。

というより、Scala と Play 自体もあまりよくわかっていないのでそのあたりからちゃーんと勉強が必要そうです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?