docker-archive / dockercloud-haproxy

HAproxy image that autoreconfigures itself when used in Docker Cloud
https://cloud.docker.com/
652 stars 181 forks source link

Redirect url to root url of swarm service #185

Closed devcoder18 closed 7 years ago

devcoder18 commented 7 years ago

I am running dockercloud/hello-world in docker swarm.

I want to redirect requests for http://myhost.domain/helloworld to the root url of the helloworld service. How can I do this? I tried the following but incoming requests are hitting the helloworld nginx container on /helloworld instead of the root url /

Logs from helloworld/nginx container: 10.0.0.3 - - [03/Apr/2017:23:24:47 +0000] "GET /helloworld HTTP/1.1" 200 485 "-" "Mozilla/5.0

Helloworld service:

sudo docker service create -e SERVICE_PORTS="80" --name helloworld \
-e VIRTUAL_HOST="*/helloworld, */helloworld/*" \
--network proxy dockercloud/hello-world

haproxy service:

sudo docker service create --name haproxy --network proxy \
--mount target=/var/run/docker.sock,source=/var/run/docker.sock,type=bind \
-p 80:80 --constraint "node.role == manager" \
dockercloud/haproxy
tifayuki commented 7 years ago

@devcoder18 This can be done using haproxy url rewrite describing here: https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#set-the-url-path

And you can add the instruction using, for example, EXTRA_FRONTEND_SETTINGS_80 environment variable describing in the README.

devcoder18 commented 7 years ago

Thanks. I used EXTRA_SETTINGSbecause I need the rewrite in the backend. This seems to work:

sudo docker service create -e SERVICE_PORTS="80" --name helloworld \
-e VIRTUAL_HOST="*/helloworld, */helloworld/*" \
-e EXTRA_SETTINGS="reqrep ^([^\ ]*\ /)helloworld[/]?(.*)     \1\2" \
--network proxy dockercloud/hello-world
devcoder18 commented 7 years ago

Closing this issue. Would recommend documenting this method in README.md examples section as it is a common use-case for web/rest services