Before reading this article, make sure that you are already hosting a grails application on Tomcat’s default port along with Apache and mod_proxy running in the server. We are about to explain some customizations in tomcat that you may require.
Tomcat Customizations
1) Hiding Tomcat port in the address bar:
Tomcat loads all applications under the default port 8080. We would be able to access the application in the browser only using this port. If an application is accessed using port numbers other than 80 and 443, all browsers would not hide the port number in the address bar. Accessing the application without the port number using an fqdn is a secure way and gives your web application a more professional look.
In our sample application we have Apache as the front end and Tomcat as the back end. Apache is configured with mod_proxy to route requests that are received in the server’s port 80 to the application’s default port (8080).
Please refer the below Apache configuration:
ServerName myapp.com
ServerAlias www.myapp.com
ProxyPass / http://myapp.com:8080/
ProxyPassReverse / http://myapp.com:8080
Apache routes Internet traffic for the site myapp.com on port 80 to the server’s port 8080 with the above configuration. You can also have a permanent redirect to Apache’s SSL port with a virtual host entry for the site for port 443.
ServerName myapp.com
ServerAlias www.myapp.com
ProxyPass / http://myapp.com:8080/
ProxyPassReverse / http://myapp.com:8080
Now you know how to give your web application an elegant look!
2) External a folder for your Application:
We could have a directory in the server to serve images or custom files via your application as storing and fetching images from a db is either a tedious job or does affect the performance of the site drastically.
You have to nest a Context element inside the Host element. Here’s how you can do it using the Tomcat’s configuration file. Shutdown your Tomcat instance. Edit the Tomcat’s configuration file (server.xml) located under the “conf” directory in your installation. Add the below code after your Host element in the server.xml file. Restart Tomcat.
Context docBase="/path/to/your/images/directory/in/the/server" path="/images"
With the above configuration any request URI in the application beginning with “/images” is served from the specified directory.
Note: If you are running your Tomcat instance as an user other than root, make sure the user has necessary permissions for the directory you are specifying in server.xml file. The modes 755 for directories and 644 for files should suffice. It is strongly advised to run Tomcat as a non root user for security purposes.
Stay tuned! We will keep you posted on more customizations on Tomcat.