There are lots of options when it comes to choosing a proxy solution for your Apache Tomcat servers, Apache HTTPD, HAProxy, and NGiNX are currently some of the most commonly used all around open source solutions.
Nginx is a popular open-source web server and reverse proxy, known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
In this quick tutorial we are going to show you how to configure Nginx as a reverse proxy for Apache Tomcat Server. The Nginx Server going to redirect all the traffics coming on port 80 to Apache Tomcat Server on port 8080
How to install Apache Tomcat 9 Server on CentOS 7 / RHEL 7
– To install Nginx Http Server use the below commands:
# yum install nginx # On centOS / RHEL # apt install nginx # On Ubuntu
– Enable and start Nginx on system boot:
# sudo systemctl start nginx # sudo systemctl enable nginx
– Let’s a create a new Nginx configuration file called tomcat.conf as below or you can edit the default Nginx configuration file
server { listen 80; listen [::]:80; server_name yallalabs.local www.yallalabs.local;# YOUR DOMAIN NAME location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://localhost:8080;# YOUR TOMCAT IP ADDRESS } }
-Run the following command to test the configuration file for syntax errors
# nginx -t
– Finally, run one of the following commands to reload the new Nginx Configuration file
# nginx -s reload # systemctl restart nginx
– If you installed Nginx on CentOS 7 or RHEL 7 and the Selinux is enabled you need to execute the following command:
# setsebool -P httpd_can_network_connect=1
We hope this tutorial was enough helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!
6 comments
Let’s a create a new Nginx configuration file called tomcat.conf
“Where / Which Location ?”
The file should be created under the directory /etc/nginx/conf.d/
I am using centos 7, there i have edited default Nginx conf file & save it, But it is showing “502 bad Gateway”.
After your reply, i created conf file in conf.d/ folder, but now it is redirecting to default webpage of Nginx.
Tomcat is running still on 8080, but not redirecting to 80 port of Nginx.
In the tomcat.conf I created a new vhost called YallaLabs.local that listen on port 80 and proxy calls to tomcat on port 8080, to replicate that you need to create an entry dns on your host file with your domain name
Once the reverse proxy has been activated, the websocker applications don’t work anymore.
Hi, can you please provide more details about your issue.
Thank you.