Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOw to run multiple applications in play framework on the same port

I am new to Play Framework. I am using Play 2.0.2 and I want to run multiple applications on Play on the same port.

It should be like http://localhost:9000/Project1/(controller) & http://localhost:9000/Project2/(controller)

I have found that you can run it on different ports but found nothing regarding running it on the same port.

Is this even possible?

like image 244
Abdul Rafae Avatar asked Dec 15 '22 15:12

Abdul Rafae


2 Answers

You can't run two applications on the same port and it's not only Play's problem.

Use frontend HTTP server to proxy applications. If you have to run only java apps then nginx will be good choice, if you need to work also with PHP systems depending on Apache specific features you can use Apache proxy as well.

In general: you need to set the server to listen on port 80 and then add a Server Block (Virtual Host in Apache) for each application using some pseudo domains like http://app1.loc, http://app2.loc, etc add them to your hosts file, to make them available in your system. Next configure each Server Block to be a proxy for application on different ports (nginx):

server {
  server_name app1.loc www.app1.loc;
    listen 80;

    location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:9021;
            proxy_redirect http://127.0.0.1:9021/ http://app1.loc/;
    }
}

and then start your first app at port 9021.

Do the same for other application(s), each time using other port.

Finally to make sure, that you always running the app1 on required port 9021 write a bash script (or bat file in windows) which will run it always with the same settings i.e. run.sh:

#!/bin/bash
play "~run 9021";
like image 171
biesior Avatar answered May 26 '23 02:05

biesior


That's not possible with play alone, since each application runs in its own process and there can only be one process listening on one port at one time. What you can do is run your play applications on ports 9001 and 9002 and then run a server like nginx on port 9000 and configure it to route requests for the different URLs to your play applications.

See this example: http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html The only difference to your case is that you would have one server {...} block with two location blocks in it. It would look something like:

upstream play1 {
      server localhost:9001;
}

upstream play2  {
      server localhost:9002;
}

server {
    listen       localhost:9000;
    server_name  www.example.com;

    access_log  /var/log/nginx/log/www.example.access.log  main;
    error_log  /var/log/nginx/log/www.example.error.log;
    root   /usr/share/nginx/html;
    index  index.html index.htm;

    ## send requests to play1 ##
    location /Project1/ {
        proxy_pass  http://play1;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    ## send requests to play2 ##
    location /Project2/ {
        proxy_pass  http://play2;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}
like image 39
Kim Stebel Avatar answered May 26 '23 00:05

Kim Stebel