Labels

Saturday, March 31, 2018

Configure Tomcat with Apache using Proxy Module and Sticky Session

Configuring Tomcat Load Balancer with Apache web server using Mod Proxy is quite easy.
It’s easy when you follow the sequence, and all goes well. I have listed following step-by-step on how to configure Apache with Tomcat to configure Load Balancer using Mod Proxy.
Having load-balanced is always recommended in a production environment for better availability.

Apache Web Server Configuration

  • Enable proxy_moduleproxy_balancer_module and proxy_http_module in httpd.conf of Apache web server
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Add proxy pass along with balancer name for application context root.
In this example, I have a proxy path as examples and balancer name as mycluster.
Very important to include stickysession as not having this option will distribute the same request to multiple Tomcat server, and you will have session expiry issues in an application.
<IfModule proxy_module>
ProxyRequests Off
ProxyPass /examples balancer://mycluster stickysession=JSESSIONID
ProxyPassReverse /examples balancer://mycluster stickysession=JSESSIONID
<Proxy balancer://mycluster>
BalancerMember http://localhost:8080/examples route=server1
BalancerMember http://localhost:8090/examples route=server2
</Proxy>
</IfModule>
As you can see in above configuration, I have added a route in BalancerMember so route value can be appended to session ID.
Now, let’s configure Apache to print JSESSIONID in access logs.
  • Add following in LogFormat directive
%{JSESSIONID}C
Ex:
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""%{JSESSIONID}C"" combined
  • Restart Apache Web Server

Tomcat Configuration

You must configure tomcat instances with same route id as you did in BalancerMember above.
  • Add jvmRoute parameter in server.xml of Tomcat. This must be added in Engine name tag.
Tomcat instance configured with 8080 port
<Engine name="Catalina" defaultHost="localhost" jvmRoute="server1">
Tomcat instance configured with 8090 port
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="server2">
  • Restart Tomcat server

Verification

Generate some load on application and check access log of apache server to ensure your request is getting routed to only one tomcat instance.
You will also notice your session ID is appended to the route as shown in below example.
Ex:
127.0.0.1 - - [18/Sep/2013:10:02:02 +0800] "POST /examples/servlets/servlet/RequestParamExample HTTP/1.1" 200 662 "http://localhost/examples/servlets/servlet/RequestParamExample" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:06 +0800] "GET /examples/servlets/servlet/RequestInfoExample HTTP/1.1" 200 693 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:17 +0800] "GET /examples/servlets/reqinfo.html HTTP/1.1" 200 3607 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:20 +0800] "GET /examples/servlets/servlet/SessionExample HTTP/1.1" 200 1124 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:26 +0800] "POST /examples/servlets/servlet/SessionExample HTTP/1.1" 200 1142 "http://localhost/examples/servlets/servlet/SessionExample" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:28 +0800] "GET /examples/servlets/servlet/SessionExample?dataname=fda&datavalue=fadaf HTTP/1.1" 200 1159 "http://localhost/examples/servlets/servlet/SessionExample" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B4EC1D73CF8C7482B7D46.server2" 
127.0.0.1 - - [18/Sep/2013:10:02:32 +0800] "GET /examples/servlets/servlet/SessionExample?dataname=foo&datavalue=bar HTTP/1.1" 200 1174 "http://localhost/examples/servlets/servlet/SessionExample?dataname=fda&datavalue=fadaf" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:36 +0800] "GET /examples/servlets/servlet/RequestHeaderExample HTTP/1.1" 200 1423 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
I hope this helps you in configuring Tomcat Load Balancer with Apache Mod Proxy and Session Sticky.

ports

Application/Web Servers

  • Tomcat Startup - 8080
  • Tomcat Startup (SSL) - 8443
  • Tomcat Shutdown - 8005
  • Tomcat AJP Connector - 8009
  • GlassFish HTTP - 8080
  • GlassFish HTTPS - 8181
  • GlassFish Admin Server - 4848
  • Jetty - 8080
  • Jonas Admin Console - 9000
  • IHS Administration - 8008
  • JBoss Admin Console - 8080
  • WildFly Admin Console - 9990
  • WebLogic Admin Console - 7001
  • WAS Admin Console (SSL) - 9043
  • WAS Admin Console - 9060
  • WAS JVM HTTP - 9080 (first one only)
  • WAS JVM HTTPS - 9443 (first one only)
  • Alfresco Explorer/Share - 8080
  • Apache Derby Network Server - 1527
  • OHS - 7777
  • OHS (SSL) - 4443

Well-Known Common Protocols

  • FTP - 21
  • HTTP - 80
  • HTTPS - 443
  • LDAP - 389
  • LDAP (SSL) - 636
  • SNMP - 161
  • SSH - 22
  • Telnet - 23
  • SMTP - 25
  • Microsoft RDP - 3389
  • DNS Service - 53
  • NNTP - 119

Database/Datastore

  • DB2 - 50000
  • Redis Server - 6379
  • Oracle Listener - 1521
  • mongoDB - 27017
  • MySQL - 3306
  • MS SQL - 1433
  • Memcached - 11211
  • MariaDB - 3306

Messaging/Transfer

  • MQ Listener - 1414
  • IBM Connect:Direct - 1364
  • RabbitMQ Web UI - 15672
  • Tibia RV Daemon - 7474
  • GoToMyPC - 8200

Some of the abbreviation used in above list

  • WAS - WebSphere Application Server
  • AJP - Apache JServ Protocol
  • SSL - Secure Socket Layer
  • HTTP - Hyper Text Transfer Protocol
  • LDAP - Lightweight Directory Access Protocol
  • SSH - Secure Shell
  • SMTP - Simple Mail Transfer Protocol
  • IHS - IBM HTTP Server
  • NNTP - Network News Transport Protocol
  • SNMP - Simple Network Management Protocol
I hope this cheat sheet helps you as a reference guide at your work. If you like this, please help to share with your friends.
One of the challenging tasks for an administrator is to remember the default port number of various protocol, application services. You may remember the most common one like HTTP, FTP, SSH but if you are working on multiple technology stacks then it’s difficult to remember all of them.
Here I have listed the default port numbers of various applications to help you in the real world.

Application/Web Servers

  • Tomcat Startup – 8080
  • Tomcat Startup (SSL) – 8443
  • Tomcat Shutdown – 8005
  • Tomcat AJP Connector – 8009
  • GlassFish HTTP – 8080
  • GlassFish HTTPS – 8181
  • GlassFish Admin Server – 4848
  • Jetty – 8080
  • Jonas Admin Console – 9000
  • IHS Administration – 8008
  • JBoss Admin Console – 8080
  • WildFly Admin Console – 9990
  • WebLogic Admin Console – 7001
  • WAS Admin Console (SSL) – 9043
  • WAS Admin Console – 9060
  • WAS JVM HTTP – 9080 (first one only)
  • WAS JVM HTTPS – 9443 (first one only)
  • Alfresco Explorer/Share – 8080
  • Apache Derby Network Server – 1527
  • OHS – 7777
  • OHS (SSL) – 4443

Well-Known Common Protocols

  • FTP – 21
  • HTTP – 80
  • HTTPS – 443
  • LDAP – 389
  • LDAP (SSL) – 636
  • SNMP – 161
  • SSH – 22
  • Telnet – 23
  • SMTP – 25
  • Microsoft RDP – 3389
  • DNS Service – 53
  • NNTP – 119

Database/Datastore

  • DB2 – 50000
  • Redis Server – 6379
  • Oracle Listener – 1521
  • MongoDB – 27017
  • MySQL – 3306
  • MS SQL – 1433
  • Memcached – 11211
  • MariaDB – 3306

Messaging/Transfer

  • MQ Listener – 1414
  • IBM Connect:Direct – 1364
  • RabbitMQ Web UI – 15672
  • Tibia RV Daemon – 7474
  • GoToMyPC – 8200

Some of the abbreviation used in above list

  • WAS – WebSphere Application Server
  • AJP – Apache JServ Protocol
  • SSL – Secure Socket Layer
  • HTTP – Hyper Text Transfer Protocol
  • LDAP – Lightweight Directory Access Protocol
  • SSH – Secure Shell
  • SMTP – Simple Mail Transfer Protocol
  • IHS – IBM HTTP Server
  • NNTP – Network News Transport Protocol
  • SNMP – Simple Network Management Protocol

HTTP Status Code

HTTP Status Code is returned when an HTTP request is made to the server. The server returns an HTP Status Code in the response to your request. There are five classes of HTTP Status Code as following.
  1. Informational – 1XX
  2. Success – 2XX
  3. Redirection – 3XX
  4. Client Error – 4XX
  5. Server Error – 5XX
Let’s take a look at below diagram, which gives an idea how a status code is returned from the web server.
http-status-return
So now you have an idea how the status code is generated and here are some of the popular HTTP status return code in Infographics.

Success Response

200 – OK: The standard HTTP response for successful HTTP requests. In another way, the web server will return 200 when requested content is served successfully.
202 – Accepted: The server has accepted your request and yet to process them.
206 – Partial Content: Only partial content is delivered due to the range header sent by a client like wget.

Redirection Response

301 – Moved permanently: Your requested page has been moved permanently to a new location. This instructs search engine bot to crawl new location.
302 – Moved temporarily: Your requested is served from a different location but that is temporary arrangement. This instructs search engine bot to crawl the original location.
305 – Use proxy: The requested resource is only available through a proxy. That means you must use a relevant proxy to get the requested page successfully.
304 – Not modified: Usually when cached page is served when a resource has not been modified.

Client Error

400 – Bad request: The server is confused what you have requested. Probably bad syntax or trying to include characters in URI which server doesn’t understand.
401 – Not authorised: The requested page is protected and requires authentication. You must login in order to get the requested page successfully.
403 – Forbidden: You have to try to access which you don’t have permission. This, not necessary resource is protected by the password; it could also be when files/folder permission doesn’t allow viewing the requested page.
404 – Not found: Probably the most famous one – your requested page is not found on the server. You are trying to access something, which doesn’t exist.
405 – Method not allowed: You are requesting a page with the wrong method. For example, you are doing GET on POST data. Or you are trying the method, which is disabled for example – TRACE, PUT, DELETE.
408 – Request timeout: The server timed out waiting for the request
411 – Length required: Your request doesn’t meet the length of its content, which is required by the requested resource.

Server Error

500 – Internal server error: A very generic error when server encountered an error due to various reasons. Logs must be examined to see why the server has responded internal error.
502 – Bad gateway: The server was acting as a gateway or proxy and received an invalid response from the upstream server like TomcatWebSphere.
503 – Service unavailable: The server can’t serve your request. This could be due to either server is too busy in other stuff or almost dead.
I hope now you have an idea about HTTP status return code. If you find this useful, help in sharing with your friends.

Default ports

Application/Web Servers

  • Tomcat Startup - 8080
  • Tomcat Startup (SSL) - 8443
  • Tomcat Shutdown - 8005
  • Tomcat AJP Connector - 8009
  • GlassFish HTTP - 8080
  • GlassFish HTTPS - 8181
  • GlassFish Admin Server - 4848
  • Jetty - 8080
  • Jonas Admin Console - 9000
  • IHS Administration - 8008
  • JBoss Admin Console - 8080
  • WildFly Admin Console - 9990
  • WebLogic Admin Console - 7001
  • WAS Admin Console (SSL) - 9043
  • WAS Admin Console - 9060
  • WAS JVM HTTP - 9080 (first one only)
  • WAS JVM HTTPS - 9443 (first one only)
  • Alfresco Explorer/Share - 8080
  • Apache Derby Network Server - 1527
  • OHS - 7777
  • OHS (SSL) - 4443

Well-Known Common Protocols

  • FTP - 21
  • HTTP - 80
  • HTTPS - 443
  • LDAP - 389
  • LDAP (SSL) - 636
  • SNMP - 161
  • SSH - 22
  • Telnet - 23
  • SMTP - 25
  • Microsoft RDP - 3389
  • DNS Service - 53
  • NNTP - 119

Database/Datastore

  • DB2 - 50000
  • Redis Server - 6379
  • Oracle Listener - 1521
  • mongoDB - 27017
  • MySQL - 3306
  • MS SQL - 1433
  • Memcached - 11211
  • MariaDB - 3306

Messaging/Transfer

  • MQ Listener - 1414
  • IBM Connect:Direct - 1364
  • RabbitMQ Web UI - 15672
  • Tibia RV Daemon - 7474
  • GoToMyPC - 8200

Some of the abbreviation used in above list

  • WAS - WebSphere Application Server
  • AJP - Apache JServ Protocol
  • SSL - Secure Socket Layer
  • HTTP - Hyper Text Transfer Protocol
  • LDAP - Lightweight Directory Access Protocol
  • SSH - Secure Shell
  • SMTP - Simple Mail Transfer Protocol
  • IHS - IBM HTTP Server
  • NNTP - Network News Transport Protocol
  • SNMP - Simple Network Management Protocol
I hope this cheat sheet helps you as a reference guide at your work. If you like this, please help to share with your friends.
One of the challenging tasks for an administrator is to remember the default port number of various protocol, application services. You may remember the most common one like HTTP, FTP, SSH but if you are working on multiple technology stacks then it’s difficult to remember all of them.
Here I have listed the default port numbers of various applications to help you in the real world.

Application/Web Servers

  • Tomcat Startup – 8080
  • Tomcat Startup (SSL) – 8443
  • Tomcat Shutdown – 8005
  • Tomcat AJP Connector – 8009
  • GlassFish HTTP – 8080
  • GlassFish HTTPS – 8181
  • GlassFish Admin Server – 4848
  • Jetty – 8080
  • Jonas Admin Console – 9000
  • IHS Administration – 8008
  • JBoss Admin Console – 8080
  • WildFly Admin Console – 9990
  • WebLogic Admin Console – 7001
  • WAS Admin Console (SSL) – 9043
  • WAS Admin Console – 9060
  • WAS JVM HTTP – 9080 (first one only)
  • WAS JVM HTTPS – 9443 (first one only)
  • Alfresco Explorer/Share – 8080
  • Apache Derby Network Server – 1527
  • OHS – 7777
  • OHS (SSL) – 4443

Well-Known Common Protocols

  • FTP – 21
  • HTTP – 80
  • HTTPS – 443
  • LDAP – 389
  • LDAP (SSL) – 636
  • SNMP – 161
  • SSH – 22
  • Telnet – 23
  • SMTP – 25
  • Microsoft RDP – 3389
  • DNS Service – 53
  • NNTP – 119

Database/Datastore

  • DB2 – 50000
  • Redis Server – 6379
  • Oracle Listener – 1521
  • MongoDB – 27017
  • MySQL – 3306
  • MS SQL – 1433
  • Memcached – 11211
  • MariaDB – 3306

Messaging/Transfer

  • MQ Listener – 1414
  • IBM Connect:Direct – 1364
  • RabbitMQ Web UI – 15672
  • Tibia RV Daemon – 7474
  • GoToMyPC – 8200

Some of the abbreviation used in above list

  • WAS – WebSphere Application Server
  • AJP – Apache JServ Protocol
  • SSL – Secure Socket Layer
  • HTTP – Hyper Text Transfer Protocol
  • LDAP – Lightweight Directory Access Protocol
  • SSH – Secure Shell
  • SMTP – Simple Mail Transfer Protocol
  • IHS – IBM HTTP Server
  • NNTP – Network News Transport Protocol
  • SNMP – Simple Network Management Protocol