【问题标题】:Set up an apache reverse proxy with SSL certs and multiple domains使用 SSL 证书和多个域设置 apache 反向代理
【发布时间】:2021-03-05 22:13:14
【问题描述】:

我需要帮助将 Apache 配置为具有 https 和多个域的反向代理,以便 www.myfirstdomain.comwww.myseconddomain.com 都指向 x.x.x.x,然后服务器将选择性地转发到,比如说 x.x.x.x:2400 (myfirstdomain.com, http)、x.x.x.x:2401 (myfirstdomain.com, https)、x.x.x.x:2600 (myseconddomain.com, http) 和 x.x.x.x:2601 (mysecondomain.com, https)。

我尝试了很多选择,但最后我陷入了困境,因为我每周发布超过 5 个证书(续订),而且我无法让它发挥作用。

myfirstdomain.comwww.myfirstdomain.com(http 和 https)配置如下:

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:2400/
    ProxyPassReverse / http://127.0.0.1:2400/
</VirtualHost>

<VirtualHost *:443>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:2401/
    ProxyPassReverse / http://127.0.0.1:2401/
</VirtualHost>

<VirtualHost *:2400>
    ServerName myfirstdomain.com
    ServerAlias www.myfirstdomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myfirstdomain/public
    <Directory /var/www/html/myfirstdomain/public>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =myfirstdomain.com
    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    RewriteCond %{SERVER_NAME} =www.myfirstdomain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:2401>
    ServerName myfirstdomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myfirstdomain/public
    <Directory /var/www/html/myfirstdomain/public>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =myfirstdomain.com
    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

然后我为 www 和非 www 生成了带有 certbot --apache 的证书,我有这个文件:

/etc/apache2/sites-available/000-default-le-ssl.conf:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:2401/
        ProxyPassReverse / http://127.0.0.1:2401/
    </VirtualHost>

    <VirtualHost *:2401>
        ServerName myfirstdomain.com
        ServerAlias www.myfirstdomain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/myfirstdomain/public
        <Directory /var/www/html/myfirstdomain/public>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

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

        RewriteEngine on
        # Some rewrite rules in this file were disabled on your HTTPS site,
        # because they have the potential to create redirection loops.

        #     RewriteCond %{SERVER_NAME} =myfirstdomain.com
        #     RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

        #     RewriteCond %{SERVER_NAME} =www.myfirstdomain.com
        #     RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]


        SSLCertificateFile /etc/letsencrypt/live/www.myfirstdomain.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.myfirstdomain.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>

我也尝试重新生成 HTTPS 证书,但它不起作用。我该怎么做?

【问题讨论】:

    标签: apache reverse-proxy mod-proxy


    【解决方案1】:

    首先,请记住 Apache 侦听一系列端口,例如 80 和 443。

    虚拟主机使用相同的端口(80 和 443),Apache 使用您使用的域名选择正确的文件夹。

    例如,myfirstdomain.com 可以显示 /var/www/html/myfirstdomain/public,但是 如果 apache 监听 80 端口,它永远不会匹配 *:2400 的规则。

    我没有尝试过,但您可以将 ProxyPass 和 ProxyPassReverse 放在每个虚拟主机中,并将端口保留为 :80 或 :443。

    当您调用 myfirstdomain.com:80 时,apache 上的规则匹配并执行代理到另一台服务器。

    this answer

    LetsEncrypt 创建一个可以从 Internet 访问的 ACME 质询(带有随机字符串的文件)。认证服务器搜索该文件,如果存在则发布证书;如果不是,它会抛出一个错误。

    我不记得文件的正确路径,但你必须验证:

    • 您可以从 Internet 访问“myfistdomain.com”吗?
    • 外部服务器可以访问“myfistdomain.com”吗? (检查路由器的 dns 名称和端口转发
    • 你能打开 ACME 挑战文件吗?
    • 服务器能否打开 ACME 质询文件?

    在某些 Plesk 安装中,无法访问 acme 文件,因为 Plesk 添加了一些自动规则。

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 2017-01-18
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2020-10-12
      相关资源
      最近更新 更多