大网数据广告活动
服务器运维问答

Setted Apache subdomain redirect to another subdomain

· 52CE 我爱测

I'm trying to set up two subdomains, for a and b in domain.com . I use two .conf files, which look pretty much the same with according changes to ServerName and ProxyPass: <VirtualHost *:80> ServerName a.domain.com #This was adde…

问题原文

原帖链接 · Jetto Martínez · CC BY-SA 4.0

I'm trying to set up two subdomains, for a and b in domain.com. I use two .conf files, which look pretty much the same with according changes to ServerName and ProxyPass:

<VirtualHost *:80>
        ServerName a.domain.com #This was added as a try for a fix. 
        Redirect permanent /  https://a.domain.com/
</VirtualHost>

<VirtualHost *:443>

        ServerName a.domain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/a

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

        #SSL stuff

        #Proxies

        ProxyPass        /        https://a.domain.com:8444/
        ProxyPassReverse /        https://a.domain.com:8444/
        ProxyPass        /a/      https://a.domain.com:8444/
        ProxyPassReverse /a/      https://a.domain.com:8444/
        ProxyPass        /b/      https://b.domain.com:8445/
        ProxyPassReverse /b/      https://b.domain.com:8445/

</VirtualHost>

This is being done my test environment, replicating something similar to what is currently in production. In /etc/hosts, I added a.domain.com and b.domain.com to 127.0.0.1. The DNS in production has records for what a.domain.com and b.domain.com are, which is the same IP (I also did this in the machine from which I'm testing this).

Also note that I'm not serving any content from their root directories. I added that trying to fix the issue stated in the title. There is a simple html in both directories though.

What is the actual problem?

Simply, when trying a.domain.com, the result is the webapp from localhost:8444, as expected. When trying b.domain.com, the result is also localhost:8444, instead of localhost:8445. Both a.conf and b.conf are enabled, and if I disable a.conf, then I'm correclty getting localhost:8445. If I also try like a.domain.com/b, the redirect resolves back to a.domain.com.

I've read several questions and tutorials, and most of them either have something working as in my configuration or add NameVirtualHost, which I understand is not needed for my version of Apache. I also added a ServerName in port 80 as I thought that perhaps the request for b was being matched by a.conf as they are the same IP, but that didn't worked either.

What am I missing here? Is it something with mod_proxy I seem to be ignoring? If possible, I'd like to keep one file per webapp. Thanks!

This is on Ubuntu 18.04.2, Apache 2.4 (mod_proxy and mod_ssl), Tomcat 9. Any other info you'd like, I'll try my best to deliver.

UPDATE

Tried with this configuration too. Same undesired result.

<VirtualHost *:80>
        ServerAlias a.domain.com
        Redirect permanent /  https://a.domain.com/
</VirtualHost>

<VirtualHost *:443>

        ServerName a.domain.com
        ServerAdmin webmaster@localhost

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

        #SSL stuff

        #Proxies

        Redirect /b https://b.domain.com

        <Location />
                ProxyPass               https://localhost:8444/
                ProxyPassReverse        https://localhost:8444/
        </Location>

        <Location /a>
                ProxyPass               https://localhost:8444/
                ProxyPassReverse        https://localhost:8444/
        </Location>

</VirtualHost>

UPDATE

Ok, I just found something very annoying. If I enter with my browser to either a.domain.com or b.domain.com, they both resolve to a.domain.com. That is the problem I described originally. BUT if try https://a.domain.com or https://b.domain.com, both resolve to the right server: a to 8444 and b to 8445.

As this is very frustrating, I'll take a brake and analize this in a while.

UPDATE

After a long brake, I tried some other random tweaks just to see what happened and yet again nothing worked as expected, except when using HTTPS. I installed Postman to see what was being send in the request and found out that in Postman, both HTTP and HTTPS use the Host correctly. Even better/worse: The actual response shows the different welcome pages for a.domain.com and b.domain.com, meaning that my configurations are working properly, when I use Postman.

I think all this ordeal might just be a cache problem, but my testing browser of choice (Firefox dev ed) is set to not cache stuff. I will check the responses with curl and with my other browsers.

已采纳解答

原帖链接 · Jetto Martínez · CC BY-SA 4.0

Culprit: The cache.

At some point, my browser's configuration changed (Perhaps an update?) and I had over 3GB worth of cache, including the test sites I was trying to set up as subdomains. Yikes.

After clearing the cache prompted an alert about my self-signed certificate when hitting my URLs, as expected. After accepting the risks, I was redirected to the proper sites. This by using b.domain.com without adding the protocol.

To make sure this was working, I tested in all my other 7 browsers (Gotta make sure) and curl with and without the protocol and the results were the desired as well.

In the end the config was ok. I'll post it here in case you need An Apache server proxying to specific Tomcat instances by subdomain, using proxy_mod that also redirects immediately from port 80 to port 433.

<VirtualHost *:80>
        ServerName a.domain.com
        Redirect permanent /  https://a.domain.com/
</VirtualHost>

<VirtualHost *:443>
        
        ServerName a.domain.com
        ServerAdmin [email omitted]

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

        #SSL stuff

        SSLEngine On
        SSLCertificateFile /etc/ssl/certs/DONT_USE_SELF_SIGNED.pem
        SSLCertificateKeyFile /etc/ssl/private/CERTS_IN_PRODUCTIve.key
        SSLVerifyClient none

        #Proxies
        ProxyRequests Off
        SSLProxyEngine on
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
        SSLProxyCheckPeerExpire off

        #Redirections
        Redirect /b https://b.domain.com

        <Location />
                ProxyPass               https://localhost:8444/
                ProxyPassReverse        https://localhost:8444/
        </Location>

        <Location /a>
                ProxyPass               https://localhost:8444/
                ProxyPassReverse        https://localhost:8444/
        </Location>

</VirtualHost>

Please take into consideration that the settings for SSL Proxy and SSL are for Self-signed certs. Please read the relevant documentation for each if you implement this for productive environments. Also note that this is the configuration file for a.domain.com. The equivalent for b.domain.com is exactly the same, with the appropiate ServerName, ServerAlias, redirects, Location and port of localhost.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。