Thursday, 24 October 2013

504 Gateway Timeout Error

Problem: My load balancer machine is giving me 504 Gateway Timeout Error.

HTTP request flow :

Browser <-> nginx(load balaencer on port 80) <-> Apache ( reverse proxy on port 85) <-> haproxy (load balaencer on 7070) <-> Tomcat ( Servlet Enginge 8080)


When I am accessing resource http://www.mydomain.com/helloworld/hello.jsp I receive 504 Gateway Timeout Error gateway time out. I am clue less which server is sending this error. I verified the logs of ngnix,apache,haproxy,tomcat none of them revel evident root cause.

Approach Taken to solve the Problem:

Let’s access the resource from bottom to top


http://tomcat-ip:8080 /helloworld/hello.jsp  (Resource is served in  5min. No time out)
http://Apache-ip:8585 /helloworld/hello.jsp (Received 504 Gateway time out)
http://nginx:80 /helloworld/hello.jsp (Received 504 Gateway time out)

We identified the root cause for this problem is haproxy and we need to increase the load_balancer time out of it.

Solution:

  • Stop haproxy .  ( I  greped for process id and killed the process)
  • Edit  /etc/haproxy/haproxy.cfg
  • Increase the value of
  •        clitimeout              9930000
  •         contimeout              9930000
  •         srvtimeout              9930000
  • Start haproxy  (./haproxy-1.4.24 -f /etc/haproxy/haproxy.cfg)



Finding process path associated to a port on linux

Problem: I want to find out process path that is associated to port 8080 on my linux system.

Solution:
We can use netstat along with grep to do this.

Command:
 [root@dragon ~]# netstat -tulpn | grep 8080

Output:
 tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      14491/./haproxy-1.4


In the above command. We can observe ./haproxy-1.4 is a process with process id 14491 is associated to port 8080.

We can use ps command to find out process path associated to process id

Command:
 [root@dragon ~]# ps -aef | grep 14491 


Output:
80       14491     1  0 Oct24 ?        00:00:04 ./haproxy-1.4.24-pcre-40kses-linux-i586.stripped -f /etc/haproxy/haproxy.cfg
 


We can observe haproxy was started using command:

./haproxy-1.4.24-pcre-40kses-linux-i586.stripped -f /etc/haproxy/haproxy.cfg

Finding files inside a jar - Linux


Problem: I have to find a jar file in which class named DataSource is present.

Solution: We can use linux find command along with grep to do this job.

 
find -name "*.jar" |  xargs grep -l "DataSource"
The above command searches for DataSource class in all jar files located in current working directory

Tuesday, 17 September 2013

Running tomcat in debug mode linux

Probelm: How can I run tomcat in debug mode in Linux

Solution:
Naviagate to tomcat bin directory



 
#cd /opt/webservers/tomcat/bin
//Execute below script
#export JPDA_ADDRESS=8000
#export JPDA_TRANSPORT=dt_socket
#export JPDA_SUSPEND=n
#./catalina.sh jpda start

Hint: Connect to 8000 port for debugging the application

Monday, 16 September 2013

Dynamically change request cookie name apache


Problem:
I want to dynamically replace cookie name coming from browser to Apache web server.

Solution: The below example will replace all the incoming requests that has cookie name as "SESSION" to "LUCKY.SESSION"

Edit:  httpd.conf

 
RequestHeader edit Cookie "SESSION" "LUCKY.SESSION"
Hint: Please include headers module to work. For ex: Uncomment "LoadModule headers_module modules/mod_headers.so" in httpd.conf

Send Cookie and custom HTTP header using java.net.URLConnection

Problem: Often sites accepts custom http headers and cookies to get data from them. In below example we are going to read data from URL by send http cookie and header.

Solution:

 
package com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
 * It reads data from a site by sending cookie and custom http header
 * @author lucky academy
 *
 */

public class URLReaderDemo {
public static void main(String[] args)throws Exception {
 //URL of the site
 URL site = new URL("http://www.luckyacademy.com/services/odata/Person");
 //Open Connection
 URLConnection uc = site.openConnection();
 //Set Cookie
 uc.setRequestProperty("Cookie", "JSESSIONID=F2845FC15FCF6660EB093D00DAE73BB2; rememberMe=false");
 //Set custom http header
 uc.setRequestProperty("tenantId","ebay");
 //read data from the site
 //Setting http headers after opening input stream will not have any effect
 BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
 String inputLine;
 while ((inputLine = in.readLine()) != null) {
  System.out.println(inputLine);
 }

 in.close();

 
}
}

Wednesday, 11 September 2013

Find and Replace a string in a file

Problem: In linux I want to find a string and replace it with other string in a file.

Solution:  Use the below command.

 
find . -type f  -print | xargs sed -i 's/abc/123/g'

The above command will recursively searches for all the files in current working directory and it replaces all the occurrences of abc with 123 in the found files.