跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Apache] 请教下 Apache2 代理二级域名的问题

发表于

主机上有两个 web 服务,分别占用 4000 端口和 3000 端口,基于域名+端口可成功访问

希望实现子域名访问对应端口,如下:

sub1.domain.com -- ip:3000

sub2.domain.com -- ip:4000

添加域名解析

在域名解析供应商处完成如下配置

20241006-1910-Fig-1

Apache2 反向代理配置

参考以下步骤,在服务器上完成 apache2 反向代理配置子

在 Apache 中,你可以使用mod_proxy模块来设置反向代理,从而隐藏后端服务的真实端口号。通过这种方式,用户访问子域名时就像访问普通的 HTTP 或 HTTPS 网站一样,而不会看到后端服务运行的具体端口。

以下是基于 Apache 完成反代配置的步骤:

  1. 确保安装了 Apache 和必要的模块: 确保你的 Apache 服务器已经安装了mod_proxymod_proxy_http等模块。如果没有安装,可以通过以下命令安装(以 Debian/Ubuntu 系统为例):

    sudo a2enmod proxy
    sudo a2enmod proxy_http
    

    安装完成后需要重启 Apache 服务:

    sudo systemctl restart apache2
    
  2. 配置虚拟主机: 在 Apache 的配置文件目录下(通常是/etc/apache2/sites-available/),创建一个新的虚拟主机配置文件,或者编辑现有的配置文件。假设你想要为子域名service.example.com配置反向代理,并且该子域名将指向本地机器上运行的一个服务,例如运行在 3000 端口上的 Web 应用。

  3. 添加反向代理配置: 编辑配置文件并添加如下内容:

    <VirtualHost *:80>
        ServerName service.example.com
    
        # 可选:重定向所有请求到 HTTPS
        Redirect permanent / https://service.example.com/
    
    </VirtualHost>
    
    <IfModule mod_ssl.c>
        <VirtualHost *:443>
            ServerName service.example.com
    
            # SSL 证书配置
            SSLEngine on
            SSLCertificateFile /path/to/certificate.crt
            SSLCertificateKeyFile /path/to/private.key
            # 如果有中间证书
            SSLCertificateChainFile /path/to/chain.pem
    
            # 开启日志记录
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            # 反向代理配置
            ProxyRequests Off
            <Proxy *>
                Order deny,allow
                Allow from all
            </Proxy>
            ProxyPass / http://localhost:3000/
            ProxyPassReverse / http://localhost:3000/
        </VirtualHost>
    </IfModule>
    

    这个配置做了几件事:

    • 设置了一个监听 80 端口的虚拟主机,它会把所有请求永久重定向到 HTTPS 。
    • 设置了一个监听 443 端口的安全虚拟主机,用于处理加密连接。
    • 配置了 SSL 证书。
    • 启用了反向代理,并指定了后端服务的地址(这里假设是同一台机器上的 8080 端口)。
  4. 启用站点并重启 Apache: 保存更改后的配置文件,然后启用这个站点配置:

    sudo a2ensite your-config-file.conf
    sudo systemctl reload apache2
    
  5. 测试配置: 使用浏览器或其他工具(如 curl )访问http://service.example.com,应该会被自动重定向到https://service.example.com,并且最终可以看到运行在 8080 端口上的服务内容。

问题

完成配置后,访问http://sub1.example.com ,无法访问,提示 502

请教

  1. 上述配置方法可能是哪里出错了?请有实操经验的大佬指导下

  2. 除了上面这个方法,有没有其他方法?(已尝试宝塔面板)

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.