当存在多个虚拟主机标签 Ubuntu14.04 时,Apache 2.4.7 重定向不起作用

Apache 2.4.7 redirect not working when multiple Virtual host tags are present Ubuntu14.04

我的 conf 文件中有 2 个虚拟主机标签。

1st Tag contains the redirect rule for request coming through internal IP address.

2nd Tag contains the other redirect rules for request coming through public ip or any other place

当我删除第一个 VirtualHost 标签时,重定向和 ssl 工作得很好,但我也需要第一个 VirtualHost 标签

我的配置文件

这位于 /etc/apache2/sites-available/site.conf

<VirtualHost 10.1.0.7:80>
    ProxyPreserveHost On
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ServerName servername.com
    ProxyPass /Service http://localhost:8080/Service
    ProxyPassReverse /Service http://localhost:8080/Service
</VirtualHost>
<VirtualHost *:80>
    ProxyPreserveHost On
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ServerName servername.com
    RewriteEngine On    # Turn on the rewriting engine
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R,L] 
</VirtualHost>

如果我删除第一个包含私有 IP 的 VirtualHost 标签,一切正常,但一旦我添加它,服务器就不会重定向到 443 端口。

我的 ssl 配置文件:

位于/etc/apache2/sites-available/site-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost _default_:443>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/html
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
     ProxyPreserveHost On
        ServerName server.com
    # Redirections:
    ProxyPass /server-status http://localhost:7570/server-status
    ProxyPassReverse /server-status http://localhost:7570/server-status

    ProxyPass /mCare/subscribe ws://localhost:8080/mCare/subscribe
    ProxyPassReverse /mCare/subscribe ws://localhost:8080/mCare/subscribe

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

    ProxyPass /solr http://localhost:8280/solr
    ProxyPassReverse /solr http://localhost:8280/solr

    SSLEngine on
            SSLCertificateFile      /mnt/opt/ssl/2015/sha2/b89516b0cdc9a701.crt
            SSLCertificateKeyFile   /mnt/opt/ssl/2015/sha2/mcare.pem
            SSLCertificateChainFile /mnt/opt/ssl/2015/sha2/gd_bundle.crt

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
            SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-6]" \
            nokeepalive ssl-unclean-shutdown \
            downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

</VirtualHost>

这是我的 ports.conf 文件:

Listen 80

<IfModule ssl_module>
      Listen 443
</IfModule>

我也已经创建了所有需要的软链接。

我终于成功了。

I am posting this so that others facing the same issue may get help.

我只是用服务器的内部IP避免了虚拟主机标签,并设法在RewriteRule中写了一个更好的正则表达式

<VirtualHost *:80>
    ProxyPreserveHost On

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





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


    ServerName server.com

    RewriteEngine On    # Turn on the rewriting engine
    RewriteCond %{HTTPS} !=on

    RewriteRule ^((?!/Service).)*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

</VirtualHost>

So now, my /etc/apache2/sites-available/site.conf file contains only one virtual tag.

我写了表达式,所有包含服务的请求都不需要重定向到 https,我也为此提供了 proxyPass。

这解决了我的问题

如果谁有更好的解决方案也欢迎大家回答