问题
Is the scenario below possible? A warning that I understand DNS only in its basic form.
We have an API (built using Play) that we would like to make available via a an address say http://api.publicname.com
However, we would like to split this API in 2 Play projects (e.g. myapione and myapitwo). Then access them using only 1 domain but two separate "subfolders"
So I have been looking for the possibility of mapping say...
http://myinternal.domain:9000 to http://api.publicname.com/myapione
... and another Play application
http://myinternal.domain:9001 to http://api.publicname.com/myapitwo
The end result we are looking for is something like below. We would have calls looking like...
http://myinternal.domain:9000/products/123 is also http://api.publicname.com/myapione/products/123
http://myinternal.domain:9001/orders/456 is also http://api.publicname.com/myapitwo/orders/456
回答1:
The objective:
Public URL -> maps to -> internal URL
http://api.publicname.com/myapione -> http://localhost:9000
http://api.publicname.com/myapitwo -> http://localhost:9001
is achieved, as @applicius said, by a "front-facing" or "origin" HTTP server which proxies the request through to the lower level "application" or "service" HTTP servers. It isn't really about virtual hosts though.
Nginx, Apache, etc are common. I think nginx is great. A commercial product which does this and is amazing is Zeus or ZXTM. It was bought out I think so I'm not sure if it's still available on its own.
Configuration for nginx for the above, something like:
server {
listen 80;
server_name api.publicname.com/myapione;
location /myapione {
proxy_pass http://localhost:9000;
proxy_set_header X-Real-IP $remote_addr;
}
location /myapitwo {
proxy_pass http://localhost:9001;
proxy_set_header X-Real-IP $remote_addr;
}
}
This approach lets you "stitch" together as many services as you like and appear as one to external callers. For example, to serve static file assets under static
and a front-facing HTML server for everything not matched under /
:
location /static/ {
alias /app/myapp-pages/static;
}
location / {
proxy_pass http://localhost:8000;
proxy_set_header X-Real-IP $remote_addr;
}
With that in place:
Public URL -> maps to -> internal URL
http://api.publicname.com/myapione -> http://localhost:9000
http://api.publicname.com/myapitwo -> http://localhost:9001
http://api.publicname.com/static -> local file assets
http://api.publicname.com/... -> http://localhost:8000
This doesn't do any load balancing, but you could put the other services on some host:port that does the load balancing, and then point nginx at it.
The ZXTM
product is interesting because it does both the above proxying and load balancing. (I'm not endorsing it more than anything else, it's just that I have seen it used in production and it's impressive.)
Side note that Play, while excellent, is better suited to rendering pages and providing a higher level API that combines calls to lower HTTP services. Lower-level HTTP services can be written with a toolkit like DropWizard, which focuses only on providing an API not pages.
回答2:
Play doesn't manage virtualhost concept. If required, you have to do it with an HTTP frontend server: Apache, Nginx, Varnish
For example in Nginx:
server {
listen: 80
server_name main.virtual.host alias.virtual.host;
proxy_pass http://localhost:3000;
}
来源:https://stackoverflow.com/questions/25211452/can-i-dns-map-one-subdomain-to-multiple-play-framework-entry-points