Apache HTTP 服务器重定向到端口 8085 如果 url 包含"demo" ELSE 重定向到端口 8080



我有两个应用程序在8080和8085端口上运行。两者都有不同的URL查询/路径,端口8085上的应用程序在其所有URL查询/通路上都有关键字demo,而其他应用程序则没有。因此,它充当了一个唯一的标识符

我可以对/etc/httpd/conf/httpd.conf文件进行任何更改吗?这样,如果服务器将demo标识为关键字,则请求将到达端口8085上的应用程序,否则请求将到达8080上的端口应用程序?

如果有办法,请提供进入httpd.conf文件的示例配置

编辑1:

在尝试了下面的前两个答案后,我没能做到这一点。我不知道ProxyPass和ProxyPassReverse是否不允许实现这一点。我试着评论它们,在VirtualHost等中添加它们,但无济于事。

这是我们期望的流程:用户将点击像-https://example.com/demo这样的URL(没有提及端口(,这将被路由到端口8085上的应用程序,其他路由到8080

可能正在查看我的完整httpd.conf可能会对有所帮助

链接到我的httpd.conf-https://gofile.io/d/tWIHvX

如果查询字符串包含单词",则重写条件;demo":

<VirtualHost *:8080>          
<Directory /var/www/example/>
Allow From All
RewriteEngine On
RewriteCond %{SERVER_PORT} !^8085$
RewriteCond %{QUERY_STRING} (demo)
RewriteRule ^Demo http://%{HTTP_HOST}:%{SERVER_PORT}/$1 [L,R]
</Directory>
</VirtualHost>

如果查询字符串不包含单词",则重写条件;demo":

<VirtualHost *:8085>          
<Directory /var/www/example/>
Allow From All
RewriteEngine On
RewriteCond %{SERVER_PORT} !^8080$
RewriteCond %{QUERY_STRING} (!demo)
RewriteRule ^Demo http://%{HTTP_HOST}:%{SERVER_PORT}/$1 [L,R]
</Directory>
</VirtualHost>

如果SERVER_PORT不工作,请直接尝试如下端口:

RewriteRule ^Demo http://%{HTTP_HOST}:8085/$1 [L,R]

尝试:

<VirtualHost *:8080>
#some conf
RewriteCond %{REQUEST_URI} demo [OR]
RewriteCond %{QUERY_STRING} demo
RewriteRule ^ http://%{HTTP_HOST}:8085%{REQUEST_URI} [QSA,L]
#some more conf
</VirtualHost>

<VirtualHost *:8085>
#some conf
RewriteCond %{REQUEST_URI} !demo
RewriteCond %{QUERY_STRING} !demo
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [QSA,L]
#some more conf
</VirtualHost>

注意:在能够实现此后自行回答

使用LocationMatch指令很容易实现。以下是配置。

由于这取决于正则表达式,您可以更改为检查条件,如以结尾、以开头、包含等。以下两个LocationMatch条件都在我的httpd.conf 中

1.如果URLlike 192.168.1.113/demo,则转到8085

<LocationMatch "^/(demo)(.*)$">
RewriteEngine on
ProxyPass         http://localhost:8085/ nocanon
ProxyPassReverse  http://localhost:8085/
</LocationMatch>

2.如果URLnot like 192.168.1.113/demo,则转到8080

<LocationMatch "^/((?!demo).)*$">
RewriteEngine on
ProxyPass         http://localhost:8080/ nocanon
ProxyPassReverse  http://localhost:8080/
</LocationMatch>

相关内容

最新更新