Tuesday, September 27, 2022

Setup Nginx as a reverse proxy for multiple tomcat application with SSL

Description: Here I have explain, How to setup tomcat, deploy multiple application and setup nginx as reverse proxy for tomcat application. 

Setup:

  • Install tomcat on port 8080 with 2 sample application
  • Install Nginx on port 80 and 443 to serve as a reverse proxy
  • 34.222.157.147 Public IP address of the machine 
Install Tomcat on Ubuntu 22: 

Install java for tomcat, first we are going to install java for tomcat

# apt install openjdk-11-jdk

Verify the java version after  installation 

# java --version

Install tomcat after validating the java 

# apt install tomcat9 tomcat9-admin

After installation verify the port number listening. You can get port 8080 [default port of tomcat] in list

# netstat -tnlp









Make necessary security changes for allow port 8080 from out side

After installation, set user credentials for admin url. To set the credentials open tomcat-users.xml file and modify as follow. Here we have set admin user with password 'admin'

<tomcat-users> <role rolename="manager-gui" /> <user username="manager" password="admin" roles="manager-gui" /> <role rolename="admin-gui" /> <user username="admin" password="admin" roles="manager-gui,admin-gui" /> </tomcat-users>

After tomcat application, I am going to setup 2 sample application with below name 

/SampleWebApp ==   http://34.222.157.147:8080/SampleWebApp
/sample                ==   http://34.222.157.147:8080/sample


So both the url accessible directly, Now I am installing nginx and configure it as proxy for both the url with self signed SSL. 

Install and configure Nginx

# apt install nginx
# systemctl start nginx
# systemctl enable nginx

After installation of nginx, configuring 2 virtual host for each application 

  • application1.local  ==  http://34.222.157.147:8080/SampleWebApp
  • application2.local  ==  http://34.222.157.147:8080/sample
To create the vhost file navigate to /etc/nginx/conf.d and create application1.conf  for application1.local site as follow 


server { listen 80; server_name application1.local; } server { listen 443 http2 ssl; server_name application1.local; ssl_certificate /etc/ssl/application1/application1.crt; ssl_certificate_key /etc/ssl/application1/application1.key; access_log /var/log/nginx/application1-access.log; error_log /var/log/nginx/application1-error.log; location = / { return 301 https://application1.local/SampleWebApp/; } location / { 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://127.0.0.1:8080; } }

Now create vhost file on /etc/nginx/conf.d path with application2.conf file name for application2.local site as follow

server { listen 80; server_name application2.local; } server { listen 443 http2 ssl; server_name application2.local; ssl_certificate /etc/ssl/application2/application2.crt; ssl_certificate_key /etc/ssl/application2/application2.key; access_log /var/log/nginx/application2-access.log; error_log /var/log/nginx/application2-error.log; location = / { return 301 https://application2.local/sample/; } location / { 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://127.0.0.1:8080; } }

After setting up both the vhost, need to setup either self signed or upload purchased SSL and upload to respective location. 

To apply the changes restart the nginx service and verify by browse both the url on browser. Both the application will redirect to its url with application path as follow

https://application1.local/













https://application2.local/



No comments:

Post a Comment