Apache httpd 反向代理非80端口和二级域名配置

当服务器80端口被占用时,我们会使用非80端口。比如 使用 Docker 执行一条命令即可部署 Wekan 文中 Wekan使用是8082端口,并且在浏览器中要访问 Wekan 输入的网址为 http://ip:port,这样就存在下面问题:

  • 通过浏览器访问的网址为 IP 加端口号,而不是域名。
  • 现在都是域名,谁会去记IP。
  • 服务器对外暴露的端口越多越不安全。

要解决这些问题,就得通过 Web 服务器的反向代理来实现。
本文将介绍下如何使用 Apache httpd Web 服务器实现反向代理非80端口和二级域名配置。

步骤

  1. 添加域名解析,我这里使用的是阿里云,我把wekan添加到一个二级域名wekan.xxx.com

  2. 先查看 Wekan 使用的端口,确认为 docker 中运行的 Wekan 使用8080端口,映射到本机的也是8080端。

  3. 测试网站可以访问,执行如下命令进行访问测试:

    shell
    1
    curl http://127.0.0.1:8080
  4. 配置httpd反向代理,编辑httpd.conf:

    shell
    1
    sudo vim /etc/httpd/conf/httpd.conf

    任选如下一种配置方式,添加到 httpd.conf。

    • 方式一
    httpd.conf
    1
    2
    3
    4
    5
    <VirtualHost *:80>
    ServerName wekan.xxx.com
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
    </VirtualHost>
    • 方式二
    httpd.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <VirtualHost *:80>
    ServerName wekan.xxx.com
    ServerAlias wekan.xxx.com
    ProxyRequests off
    <Proxy *>
    Order deny,allow
    Allow from all
    </Proxy>
    <Location />
    ProxyPass http://localhost:8080/
    ProxyPassReverse http://localhost:8080/
    </Location>
    </VirtualHost>
  5. 重启httpd服务

    shell
    1
    $ sudo systemctl restart httpd.service
  6. 打开浏览器访问wekan.xxx.com

Apache httpd 反向代理非80端口和二级域名配置

https://ganzhixiong.com/p/711a2f0d/

Author

干志雄

Posted on

2021-08-17

Updated on

2021-08-17

Licensed under

Comments