Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot + tomcat RewriteValve

Tags:

spring-boot

Is it possible to somehow customize embedded tomcat with RewriteValve? As I can see in api there are currently only methods for addContextValves and addEngineValves but as pointed tomcat in documentation, RewriteValve should be placed in Host or in a webapp's context.xml. I don't understand if addContextValves could work for this.

Thanks

like image 917
bilak Avatar asked Sep 14 '16 14:09

bilak


People also ask

Can I deploy a Spring Boot project in Tomcat?

Conclusion: As explained above, a spring boot project can be deployable in Tomcat. Do remember certain important points as listed: By default, Spring Boot 1.4.4.RELEASE requires Java 7 and Spring Framework 4.3.6.RELEASE or above Higher versions of Tomcat will be helpful to deploy spring-boot applications.

How to show Tomcat's logs in Spring Boot?

Spring Boot comes with an embedded Tomcat server, which is super-handy. However, we can't see Tomcat's logs by default. In this tutorial, we'll learn how to configure Spring Boot to show Tomcat's internal and access logs via a toy application. 2. Sample Application First of all, let's create a REST API.

How do I implement rewritevalve in Tomcat?

These are two steps you need to follow: Implement your own TomcatEmbeddedServletContainerFactory bean and set up the RewriteValve import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; ... import org.apache.catalina.valves.rewrite.RewriteValve; ...

What is the maximum number of Tomcat threads in Spring Boot?

In Spring Boot, we can define the maximum amount of Tomcat worker threads: server.tomcat.threads.max=200. When configuring a web server, it also might be useful to set the server connection timeout. This represents the maximum amount of time the server will wait for the client to make their request after connecting before the connection is closed:


1 Answers

Yes, it is possible. These are two steps you need to follow:

  1. Implement your own TomcatEmbeddedServletContainerFactory bean and set up the RewriteValve

      import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;  
      ...
      import org.apache.catalina.valves.rewrite.RewriteValve; 
      ... 
    
      @Bean TomcatEmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort(8080);
        factory.addContextValves(new RewriteValve());
        return factory;
      }
    
  2. Add a rewrite.conf file to the WEB-INF directory of your application and specify the rewrite rules. Here is an example rewrite.conf content, which I'm using in the angular application to take advantage of the angular's PathLocationStrategy (basicly I redirect everything to the index.html as we just use spring boot to serve the static web content):

      RewriteCond %{REQUEST_URI} !^.*\.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map)$
      RewriteRule ^(.*)$ /index.html [L]
    
like image 60
Krzysztof Miernik Avatar answered Oct 29 '22 07:10

Krzysztof Miernik