0

How can I redirect all outgoing traffic from a Tomcat server by Url patterns?

Example: redirect all requests from tomcat to

It is only for development purpose on a windows machine. I don't want to modify my host-file because I still want to access everything as-is in my browser.

Is there a way to config tomcat like this?

Edit: clarification:

currently all the services run on one backendserver, but i want to deploy them on different servers. so based on the url-part after the / i want to route it to a different server.

i.e. :

  • /service1 -> server1/service
  • and /service2 -> server2/service
sotix
  • 101
  • 6

3 Answers3

1

You may proxy the request before tomcat, for example, install an apache server on the same web server on which tomcat is running (obviously on a different port). Install the mod_proxy for apache and redirect correctly to your front-end or back-end server.

here it is an example of the apache configuration:

ProxyPass /my-service http://a.backend.url:8080/my-service-a
ProxyPassReverse /my-service http://a.backend.url:8080/my-service-a

Please remeber to load the properly module in apache, you may refer to the documentation look for proxy-howto apache.

M Terlizzi
  • 11
  • 4
  • Thank you, this seems like the right direction. But what file do I have to edit? In the tomcat proxy howto they talk about httpd.conf-file but that is nowhere in my tomcat directory – sotix Jun 22 '17 at 11:19
  • You should install an apache server, listening on port 8080, and tomcat on port 8081. The proxy directive may be configured in the apache http.conf or mod_proxy.conf. I'll write a more descriptive response. – M Terlizzi Jun 22 '17 at 12:27
0

Use a property to configure which backend server to use. Configure tomcat on your laptop to use the development backend. You can either provide a default property value with the production backend, or set the appropriate backend on your production server. This gives you great flexibility to configure staging and performance test environments.

If you have multiple backend services consider using a property per service. This provides the ability to deploy use one service for multiple environments while deploying other services differently. It also allows scaling services, in cases where the load on a service requires one or more dedicated servers.

BillThor
  • 27,737
  • 3
  • 37
  • 69
  • This is already the case. But I want to change the url in more depth. I wrote more clarification in the original post. – sotix Jun 22 '17 at 10:27
  • @sotix It is not uncommon to use a property per back-end service. There are usually not that many. – BillThor Jun 22 '17 at 10:35
0

I installed a load balancer (here nginx) locally on my machine and pointed to it as backend-server.

The config looks like this:

# This routes all traffic to the LB to a default backend server
location / {
    proxy_pass      http://my.backend.url:8080;
    proxy_redirect  http://my.backend.url:8080/ /;
    proxy_read_timeout 60s;

    proxy_set_header          Host            $host;
    proxy_set_header          X-Real-IP       $remote_addr;
    proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
}

# This routes all traffic under "/my-service" to the different backend:
# a.backend.url:8080/my-service-a
location /my-service {
    proxy_pass      http://a.backend.url:8080/my-service-a
    proxy_redirect  http://a.backend.url:8080/my-service-a/ /;
    proxy_read_timeout 60s;

    proxy_set_header          Host            $host;
    proxy_set_header          X-Real-IP       $remote_addr;
    proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
}
sotix
  • 101
  • 6
  • Ugh, terminology. You've installed a "frontend". It **would** be called load balancer if there were multiple backends per one `location`. – kubanczyk Jun 23 '17 at 07:48
  • But nginx is a load balancer right? By your comment I used it only as a frontend/proxy – sotix Jun 23 '17 at 10:54