How to set Nginx as a reverse proxy for Tomcat

If you have installed Tomcat on your server and you need to install http proxy for your tomcat app that is set on port 8080 yoo can use this simple config for it:

server{
server_name domain.com;
location / {

proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://domain.com:8080/app/;
#rewrite ^/app/(.*)$ /$1 last;
}
location /app/ {

proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://domain.com:8080/app/;
}
}

 

If this does not work for you.. you can always check nginx error.log. In my case i got this error :

2016/11/20 21:40:40 [crit] 8666#8666: *1 connect() to *******:8080 failed (13: Permission denied) while connecting to upstream, client:

It seams that SELinux was blocking my upstream connection.. so I used this to fix it..

/usr/sbin/setsebool httpd_can_network_connect true

Remember this is tested on CentOS !