简体   繁体   中英

Tomcat to Apache Proxy / URL Sub Directory in Grails

Running a Grails web app on AWS Elastic Beanstalk with an Auto Scaling Elastic Load Balancer.

We have another AWS EC2 instance running our blog, ie. blog.domain.com

Is there a way to route traffic from domain.com/blog to the blog EC2 instance? I know with Apache you can proxy pass to a Tomcat instance, but can you do it the other way around? If so would it even work with Auto Scaling?

You could configure /blog in your grails app to a controller with a method like:

class BlogController {
   def index() {
      redirect(url: "http://blog.domain.com")
   }    
}

Edit: my answer didn't cater for the question specifying it was to be proxied not redirected... So here is my ammended version:

I took an existing Java proxy servlet code provided here: http://edwardstx.net/2010/06/http-proxy-servlet/ and implemented a Grails controller here: https://github.com/AtlasOfLivingAustralia/biocache-hubs/blob/master/grails-app/controllers/au/org/ala/biocache/hubs/ProxyController.groovy (too big to paste)

Add the following to your URL mapping file:

"/blog/$path**" (controller: 'proxy'){
    action = [POST:'doPost']
}
"/blog/$path**" (controller: 'proxy'){
    action = [GET:'doGet']
}

I think from memory this code expects the $path to be a full URL and you might want to store the URL prefix in a field and only pass in the path portion in the links (if that makes sense). Note you won't get any of your layout, etc., being displayed, it will simply reproduce the page as it was created in the other app. You could add some caching to it with the cache plugin .

Edit 2: This code might be overkill for your requirements and you might be better writing a simple GET service and then sending it back to the client... like described here Writing a proxy in grails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM