Pages

Thursday, July 17, 2014

Apache Rewrite URLs

Share it Please
In many cases , applications deployed into production servers will be accessed with a specific URL rather then accessing the same application with localhost Url. These Url are configured specifically for every application deployed into the production machines. In this article we will see the basics of configuring the Apache Rewrite module which will help in rewriting normal Url to a different one.

For this demo purpose, I will be using 2 sample application written by and deployed in tomcat which are being accessed normally using

http://localhost:8080/Sample-app/test.html
http://localhost:8080/myApp/

Now I want to configure different URL for both these applications which the below URL

http://localhost/Sample-app/test.html
http://localhost/myApp/

Now for the Rewrite to work , we need to start making changes to the httpd.conf file for the apache server.

1. The first thing we need to do is the enable the mod_rewrite module by adding,
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module/mod_proxy.so

2.Now create a VirtualHost element for out configuration. Here is the configuration that I have used for this article

<VirtualHost *:80>
   ServerAdmin webmaster@domain.com
   DocumentRoot /var/www/webroot/ROOT
   ServerName tester123.com

   ServerAlias *
   RewriteEngine On

   RewriteRule ^/application1/(.*) http://localhost:8080/myApp/ [P]
   ProxyPassReverse /application1/ http://localhost:8080/myApp/

   RewriteRule ^/application2/(.*) http://localhost:8080/Sample-app/test.html [P]
   ProxyPassReverse /application2/ http://localhost:8080/Sample-app/test.html

   RewriteLog "/var/log/httpd/rewrite.log"

   ErrorLog logs/apache-rewrite-test-error_log
   CustomLog logs/apache-rewrite-test common
</VirtualHost>

In the above snippet ,

  • RewriteEngine On enables rewriting ability
  • RewriteRule and ProxyPassReverse specify the conditions and the result of rewriting for both applications
  • RewriteLog is optionally added to store the logs of rewriting in the specified location

So if we take the statements

   RewriteRule ^/application1/(.*) http://localhost:8080/myApp/ [P]
   ProxyPassReverse /application1/ http://localhost:8080/myApp/


The first line tell you that when ever a request comes for application1 in the URL path, redirect  the request to the backend servers and access http://localhost:8080/myApp/ URL from the back end servers.

Once the changes are done, restart the back end tomcat and apache server.

Now access the application using, http://localhost:80/application1 which will access the application from tomcat.

Issues faced:
While working with the above article ,I have seen the below exception in the apache httpd server logs files

(13)Permission denied: proxy: HTTP: attempt to connect to 127.0.0.1:8080 (localhost) failed – this error is because of httpd/apache2 has been denied permission to connect to IP address and port.

The main reason for this is the SELinux , The SELinux in here is not permitting httpd/apace2 to make a network connections.

We can resolve this disabling the SELinux or we can allow the httpd to make network connection like

Run either of following command to allow SELinux to permit httpd/apache2 to make network connections:

$ /usr/sbin/setsebool httpd_can_network_connect 1
                         or
$ /usr/sbin/setsebool httpd_can_network_connect true

Then restart httpd/apache2.
 

More to learn , Happy Learning.

2 comments :

  1. I must thank you for the efforts you've put in writing this website.
    I really hope to see the same high-grade blog posts from you
    later on as well. In fact, your creative writing abilities has encouraged me to get my very own website now ;
    )

    Also visit my weblog - Handphone Dengan Kamera Depan Terbaik

    ReplyDelete
  2. Good short article I loved reading this and I defiantly will recombed this article to my mates as well as I will have toward short article this on Fb, twitter and more.
    I experience which include this should be shared in the course of the net.
    I’ve been within the technologies industry for relating to a ten years and I often take
    pleasure in looking at fresh posts that may perhaps either
    support me be added well-informed inside my marketplace.

    I experience together with I acquired a lot out of what oneself wrote and I’m psyched in the direction of percentage this with my
    family members and buddies.

    ReplyDelete