如何配置 Apache 反向代理服务器 ?

将 Apache 配置为反向代理意味着将 Apache 设置为侦听和引导 web 流量到后端服务器或服务。这有助于管理和平衡服务器上的负载,提高安全性,并使您的 web 服务更高效。您还可以将其设置为监听标准 HTTP 和 HTTPS 端口上的请求,并将其重定向到运行在不同端口上的后端服务。

案例场景

假设您在服务器上安装了 Apache,任何人都可以从 internet 访问该服务器。Apache 正在监听常规 HTTP 和 HTTPS 端口上的流量。此外你还运行了一些其它应用程序:

  • 一个应用程序运行在与 Apache 相同的服务器上,但使用不同的端口,比如 3000。
  • 其他应用程序在同一网络内的不同服务器上运行,但该服务器不能从 internet 访问。

Step 1: Setup Apache Proxy Module

Redhat-based systems

编辑配置文件 /etc/httpd/conf.modules.d/00-proxy.conf,注释掉以下条目。

复制代码
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Debian-based systems

使用以下命令启用 Apache 的 Proxy 模块

复制代码
sudo a2enmod proxy proxy_http

启用模块后,您需要重新启动 Apache 服务以立即应用更改。

Step 2: Configure Apache Virtual Host

为了演示,我们创建几个 Apache 虚拟主机,如下所示。

(1) Reverse Proxy to Local Application

将发送到 www.yourdomain.com 的所有请求转发到端口 3000 上本地运行的后端应用程序。

apacheconf 复制代码
<VirtualHost *:80>
	ServerName www.yourdomain.com

	ProxyPreserveHost On

	# Reverse proxy for the application running on port 3000 on the same server
	ProxyPass / http://localhost:3000/
	ProxyPassReverse / http://localhost:3000/

	# Change log as per server
	# ErrorLog ${APACHE_LOG_DIR}/error.log
	# CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(2) Reverse Proxy to Local with Sub URL

将特定的子目录 URL 转发到后端应用程序。例如,将发送到 www.yourdomain.com/api 的所有请求转发到在端口 3000 上本地运行的后端应用程序。

ADVERTISEMENT

apacheconf 复制代码
<VirtualHost *:80>
	ServerName www.yourdomain.com

	ProxyPreserveHost On

	# Reverse proxy for the application running on port 3000 on the same server
	ProxyPass /api http://localhost:3000/
	ProxyPassReverse /api http://localhost:3000/

	# Change log as per server
	# ErrorLog ${APACHE_LOG_DIR}/error.log
	# CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(3) Reverse Proxy to Backend Host Application

将发送到 www.yourdomain.com 的所有请求转发到端口 3000 上运行在不同服务器 (IP 192.168.1.100) 上的后端应用程序。

apacheconf 复制代码
<VirtualHost *:80>
	ServerName www.yourdomain.com

	ProxyPreserveHost On

	# Reverse proxy for the application running on a different server
	ProxyPass / http://192.168.1.100:3000/
	ProxyPassReverse / http://192.168.1.100:3000/

	# Change log as per server
	# ErrorLog ${APACHE_LOG_DIR}/error.log
	# CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(4) Reverse Proxy to Multiple Backend Applications

将请求转发到基于 URL 路径的不同后端应用程序。例如,将发送到 www.yourdomain.com/app1 的请求转发到本机端口 3000 上的应用程序,将发送到 www.yourdomain.com/app2 的请求转发到其他服务器(IP 192.168.1.100)上的端口 5000 上的应用程序。

apacheconf 复制代码
<VirtualHost *:80>
	ServerName www.yourdomain.com

	ProxyPreserveHost On

	# Reverse proxy for different applications
	ProxyPass /app1 http://localhost:3000/
	ProxyPassReverse /app1 http://localhost:3000/

	ProxyPass /app2 http://192.168.1.100:5000/
	ProxyPassReverse /app2 http://192.168.1.100:5000/

	# Change log as per server
	# ErrorLog ${APACHE_LOG_DIR}/error.log
	# CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(5) Reverse Proxy to Application on Different Ports

将请求转发到同一服务器上的不同后端应用程序。例如,将发送到 www.yourdomain.com/app1 的请求转发到端口 3000 上的应用程序,将发送到 www.yourdomain.com/app2 的请求转发到端口 5000 的应用程序。

apacheconf 复制代码
<VirtualHost *:80>
	ServerName www.yourdomain.com

	ProxyPreserveHost On

	# Reverse proxy for different applications
	ProxyPass /app1 http://localhost:3000/
	ProxyPassReverse /app1 http://localhost:3000/

	ProxyPass /app2 http://localhost:5000/
	ProxyPassReverse /app2 http://localhost:5000/

	# Change log as per server
	# ErrorLog ${APACHE_LOG_DIR}/error.log
	# CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 3: Restart Apache to Apply Changes

创建 Apache 虚拟主机后,需要重新启动 Apache 服务。

Redhat-based systems:

复制代码
sudo systemctl restart httpd

Debed-based systems:

复制代码
sudo systemctl restart apache2

我的开源项目

相关推荐
李昊哲小课15 分钟前
Apache Shiro 教程
apache·shiro
java_logo4 小时前
Transmission Docker 容器化部署指南
运维·docker·容器·kubernetes·apache·rocketmq·transmission
veteranJayBrother10 小时前
适配小程序的下滑上滑播放视频组件
小程序·apache·音视频
鸠摩智首席音效师10 小时前
Apache Prefork 和 Worker 有什么区别 ?
apache
鹿衔`2 天前
StarRocks 2.5.22 混合部署实战文档(CDH环境)
starrocks·apache·paimon
Rover.x2 天前
apache.poi XSSFWorkbook创建失败,空指针
apache
鹿衔`2 天前
Apache Doris 2.1.10 集群部署与 Paimon 数据湖集成实战文档
apache·doris·paimon
鹿衔`2 天前
Apache Doris 4.0.1 集群部署与 Paimon 数据湖集成实战文档
flink·apache·doris·paimon
SelectDB技术团队3 天前
面向 Agent 的高并发分析:Doris vs. Snowflake vs. ClickHouse
数据仓库·人工智能·科技·apache·知识图谱
初願致夕霞3 天前
C++文件压缩及解压缩小程序的实现
c++·小程序·apache