如何通过域名访问localhost:port

问题描述 投票:0回答:1

我将 localhost:8080 转发到 mysite1.myhost1.com 并将 localhost:9000 转发到 mysite2.myhost2.com

我可以通过 localhost:8080 和 localhost:9000 分别访问网站。

我想要的是我想在浏览器中输入 mysite1.myhost1.com ,它应该重定向到 localhost:8080 ...这将依次转发到真正的 mysite1.myhost1.com ...同样我想输入 mysite2.myhost2 .com 在浏览器中,它应该重定向到 localhost:9000 ...这将依次转发到真正的 mysite2.myhost1.com。

这可能吗? 我正在使用 apache 网络服务器 请告诉我如何实现这一目标?

apache portforwarding hosts-file
1个回答
0
投票

听起来您的解决方案将使用 Apache 的反向代理。

首先您必须启用 Apache 代理模块:

sudo a2enmod proxy

通过将其添加到虚拟主机文件中,您可以将请求传递到不同的后端服务器(例如 localhost:8080):

ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

如果您还想传递 Websocket 请求,您可以添加以下内容:

RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule .* ws://localhost:8080%{REQUEST_URI} [P]

您的最终配置将类似于此(对于 mysite1.myhost1.com):

<VirtualHost *:80>
        ServerName mysite1.myhost1.com

        ProxyPreserveHost On
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/

        RewriteEngine on
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule .* ws://localhost:8080%{REQUEST_URI} [P]

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

请记住,为了简单起见,我在示例中使用了 http 而不是 https,但推荐使用 https。

© www.soinside.com 2019 - 2024. All rights reserved.