请在同一端口上的play框架中运行多个应用程序

Abd*_*fae 4 playframework

我是Play Framework的新手.我正在使用Play 2.0.2,我想在同一端口上在Play上运行多个应用程序.

它应该像 http://localhost:9000/Project1/(controller)&http://localhost:9000/Project2/(controller)

我发现你可以在不同的端口上运行它,但没有发现在同一端口上运行它.

这甚至可能吗?

bie*_*ior 7

您无法在同一端口上运行两个应用程序,这不仅仅是Play的问题.

使用前端HTTP服务器代理应用程序.如果你只需要运行java应用程序,那么nginx将是不错的选择,如果你还需要使用PHP系统,这取决于Apache特定功能,你也可以使用Apache代理.

一般:您需要设置服务器侦听端口80,然后用像一些伪域添加服务器块(在Apache虚拟主机)为每个应用程序http://app1.loc,http://app2.loc等它们添加到您的hosts文件,以使它们在你的系统中可用.接下来将每个服务器块配置为不同端口(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/;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在端口9021启动您的第一个应用程序.

每次使用其他端口时,对其他应用程序执行相同操作.

最后要确保,你总是在所需的端口9021上运行app1写一个bash脚本(或windows中的bat文件),它将始终使用相同的设置运行它,即run.sh:

#!/bin/bash
play "~run 9021";
Run Code Online (Sandbox Code Playgroud)


Kim*_*bel 5

单独播放是不可能的,因为每个应用程序都在自己的进程中运行,并且一次只能有一个进程在一个端口上进行侦听.您可以做的是在端口9001和9002上运行您的播放应用程序,然后在端口9000上运行像nginx这样的服务器,并将其配置为将不同URL的请求路由到您的播放应用程序.

请参阅此示例:http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html 与您的情况唯一不同的是,您将拥有一个server {...}包含两个location块的块.它看起来像:

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

}
Run Code Online (Sandbox Code Playgroud)