在 tomcat 前面使用 apache 设置 SSL

Sai*_*een 9 mod-rewrite ssl tomcat virtualhost apache-2.2

我正在尝试使用 SSl 设置 Apache,并将 SSL 请求代理到我的 tomcat 实例。我想我使 SSL 工作,但仍然出现错误:

Bad Gateway

The proxy server received an invalid response from an upstream server.
Run Code Online (Sandbox Code Playgroud)

* SSL 虚拟主机 *

LoadModule ssl_module modules/mod_ssl.so

Listen 443
<VirtualHost _default_:443>
SSLEngine On
SSLProxyEngine On
DocumentRoot "/var/apache-tomcat-7.0.34/webapps/Learn2Gether/"

SSLCertificateFile /etc/pki/tls/learn2gether/cert-6090205098829887.pem
SSLCertificateKeyFile /etc/pki/tls/learn2gether/private_key_unlocked.pem
SSLCertificateChainFile /etc/pki/tls/learn2gether/rubca-chain.pem


BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

ServerName www.learn2gether.rubel.rub.de
ServerAlias learn2gether.rubel.rub.de

#RewriteRule ^\/$ /Learn2Gether/index.html [PT]
##RewriteRule ^/(.*)$ /$1 [PT]

ProxyRequests Off
ProxyPreserveHost On
ProxyPass / https://localhost:8443/
ProxyPassReverse / https://localhost:8443/

</VirtualHost>
~               
Run Code Online (Sandbox Code Playgroud)

HTTP VH 重定向到 HTTPS

NameVirtualHost *:80

<VirtualHost _default_:80>
   ServerName www.learn2gether.rubel.rub.de
   ServerAlias learn2gether.rubel.ruhr-uni-bochum.de
RewriteEngine on
# DocumentRoot "/var/apache-tomcat-7.0.34/webapps/Learn2Gether/"
RewriteCond %{HTTP_HOST} !^learn2gether.rubel.ruhr-uni-bochum\.de [NC]
RewriteRule ^/(.*)$ http://learn2gether.rubel.ruhr-uni-bochum.de/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}$1 [L]

#RewriteRule ^\/$ /Learn2Gether/index.html [PT]
#RewriteRule ^/(.*)$ /$1 [PT]

#ProxyPass / https://localhost:8443/
#ProxyPassReverse / https://localhost:8443/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

Tomcats apache 连接器

    <Connector port="8443"
  protocol="HTTP/1.1"
   connectionTimeout="20000"
    compression="on"
     compressionMinSize="32"
      noCompressionUserAgents="gozilla, traviata"
       compressableMimeType="text/html,text/xml,text/javascript,application/x-javascript,text/css"
        redirectPort="8443"
         URIEncoding="UTF-8"
          proxyPort="443"
           proxyName="learn2gether.rubel.ruhr-uni-bochum.de"
            scheme="https"
             secure="true"
/>
Run Code Online (Sandbox Code Playgroud)

Kri*_*ien 14

将 http 或 https 代理到 https 时,您需要将 apache 配置为 ssl 客户端。当 apache 与您的 Tomcat 服务器通信时,它毕竟充当 Web 客户端。但是,Apache 通常不会作为开箱即用的 SSL 客户端运行。

首先,我建议您首先考虑是否真的需要这个,为什么要这样做。当 Tomcat 和 Apache 位于同一台服务器上时,通常的做法是让 Tomcat 只提供普通的 http(或 ajp)并将 ssl 卸载到 Apache 服务器。apache和tomcat服务器之间通常不需要ssl。在 tomcat 服务器上没有 ssl 将为您节省很多麻烦。

例如,您需要做的就是在您的 tomcat 实例的端口 8080 上定义一个 HTTP 连接器,并从您的 apache SSL 虚拟主机中重定向所有请求:

<VirtualHost _default_:443>
SSLEngine On

SSLCertificateFile /etc/pki/tls/learn2gether/cert-6090205098829887.pem
SSLCertificateKeyFile /etc/pki/tls/learn2gether/private_key_unlocked.pem
SSLCertificateChainFile /etc/pki/tls/learn2gether/rubca-chain.pem


BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

ServerName www.learn2gether.rubel.rub.de
ServerAlias learn2gether.rubel.rub.de

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

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

但是,如果您仍然决定确实需要 ssl 到 ssl 代理,您将需要添加更多更改。Apache 需要能够充当 SSL 客户端以及 SSL 服务器。当 Apache 使用 https 与另一台服务器通信时,它毕竟扮演了客户端的角色。这并不容易,您可能会遇到很多问题。您需要添加以下内容:

# turn on SSL proxying.
SSLProxyEngine On

# to tell Apache where to find CA certificates to check server certificates with:
# (You can choose yourself where you put these certificates)
SSLProxyCACertificatePath /path/to/ca/certificates.
Run Code Online (Sandbox Code Playgroud)

然后在此路径中,您需要放置用于签署您与之通信的服务器使用的证书的 CA 证书。如果您使用“自签名”证书,则需要将其放在此目录中。

完成后,您需要在该目录中运行“c_rehash”。c_rehash 是标准 openssl 发行版的一部分。c_rehash 在此目录中创建散列别名。Apache 需要这些。

为了测试是否一切都在那里,您可以执行以下操作:

 openssl s_client -CApath /path/to/ca/certificates -connect remoteserver:8443
Run Code Online (Sandbox Code Playgroud)

如果连接成功,您将收到一个提示,您可以在其中输入请求。尝试一些东西。

 GET /
Run Code Online (Sandbox Code Playgroud)

看看你有没有得到什么。如果这个测试成功,apache 也应该可以工作。

您现在可以添加 ReWriteRule 或 Proxy 语句以将连接转发到您的 https 服务器。