[JAVA] - Java Solution
March 13, 2019 |JAVA Solution
Tips:
1. Read Response Body from Bad Request :
// Check HTTP Response Code #200 after write code below InputStream errorstream = connection.getErrorStream(); String response = ""; String line; BufferedReader br = new BufferedReader(new InputStreamReader(errorstream)); while ((line = br.readLine()) != null) { response += line; } Log.d("body of Bad Request HttpURLConnection", "Response: " + response);
2. Redirect page wrong with https
Description:
- If server run HTTP port = 8080, nginx proxy port = 443. When you redirect to another URL, it wrong port.
Ex:
https://ip:443/login => redirect:/home => http://ip:8080/home
Solution:
Config in server.xml of tomcat
location / {
proxy_pass http://localhost:8080;
#proxy_set_header Host $host:$proxy_port;
proxy_set_header Host $host:$server_port; => add here
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
}
Config nginx.conf in Nginx
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
scheme="https"/> => Add scheme
3. Get Real IP from Tomcat when use nginx
Problem: You get Real IP in your application and use Tomcat container and Nginx Proxy but you can not get Real IP address because Remote Address always return local address instead of Remote address.
Solution:
Config Nginx
$vi /etc/nginx/nginx.conf
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Config tomcat
$vi server.xml
<!-- Replaces the apparent client remote IP address and hostname for
the request with the IP address list presented by a proxy or a
load balancer -->
<Valve className="org.apache.catalina.valves.RemoteIpValve"
requestAttributesEnabled="true"
internalProxies="127\.0\.0\.1" />
is updating