在为 Apache Tomcat 服务器选择代理解决方案时,有很多选择,Apache HTTPD、HAProxy 和 NGiNX 是目前最常用的一些开源解决方案。
Nginx 是一种流行的开源 Web 服务器和反向代理,以其高性能、稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。
在本快速教程中,我们将介绍如何将 Nginx 配置为 Apache Tomcat 服务器的反向代理。
Nginx 服务器将所有来自 80 端口的流量重定向到端口 8080 上的 Apache Tomcat 服务器
要安装 Nginx Http 服务器,请使用以下命令:
# yum install nginx # On centOS/RHEL # apt install nginx # On Ubuntu
在系统启动时启用并启动 Nginx:
# sudo systemctl start nginx # sudo systemctl enable nginx
让我们创建一个名为 tomcat.conf 的新 Nginx 配置文件,如下所示,或者我们可以编辑默认的 Nginx 配置文件
server { listen 80; listen [::]:80; server_name onitroad.local www.onitroad.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 } }
- 运行以下命令测试配置文件是否存在语法错误
# nginx -t
最后,运行以下命令之一重新加载新的 Nginx 配置文件
# nginx -s reload # systemctl restart nginx
如果我们在 CentOS 7 或者 RHEL 7 上安装了 Nginx 并且启用了 Selinux,则需要执行以下命令:
# setsebool -P httpd_can_network_connect=1
日期:2020-06-02 22:18:52 来源:oir作者:oir