Search code examples
apachereverse-proxy

Two reverse proxies on the same domain, diferent path


I'm trying to configure two different reverse proxies on the same machine with diferent paths. They are set to proxy SonarQube (localhost:9000/sonar) and Jenkins (localhost:8080/jenkins).

I'm trying to achive that calling to http://server.name/sonar, proxifys to http://localhost:9000/sonar.

I have two issues:

  • Sonar is correctly proxified but on the callback url (clicking in the logo), will point to http://server.name, instead http://server.name/sonar. I have tried with the X-Forwarded-Host header (which should be also included in ProxyPreserveHost directive).
  • Jenkins proxy is not working. It crashes with a 404.

What I'm doing wrong?

ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
  ServerName server.name/sonar
  ProxyPass /sonar http://localhost:9000/sonar
  ProxyPassReverse /sonar http://localhost:9000/sonar
  ErrorLog logs/sonar/error.log
  CustomLog logs/sonar/access.log common
</VirtualHost>

# VIRTUAL HOSTS
####################
<VirtualHost *:80>
  ServerName server.name/jenkins
  ProxyPass /jenkins http://localhost:8080/jenkins
  ProxyPassReverse /jenkins http://localhost:8080/jenkins
  ErrorLog logs/jenkins/error.log
  CustomLog logs/jenkins/access.log common
</VirtualHost>

Solution

  • The problem is in the way you define your Virtual Hosts. ServerName does not understand /something, only domain names.

    Option 1 define domain prefixes.

    <VirtualHost *:80>
        ServerName sonar.server.name
        
        ProxyPass /sonar http://localhost:9000/sonar/
        ProxyPassReverse /sonar http://localhost:9000/sonar/
        
        ErrorLog logs/sonar/error.log
        CustomLog logs/sonar/access.log common
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName jenkins.server.name
        
        ProxyPass /jenkins http://localhost:8080/jenkins/
        ProxyPassReverse /jenkins http://localhost:8080/jenkins/
        
        ErrorLog logs/jenkins/error.log
        CustomLog logs/jenkins/access.log common
    </VirtualHost>
    

    You would then have to use http://jenkins.server.name/jenkins/ or http://sonar.server.name/sonar/.


    Option 2 like option 1, but without the /sonar or /jenkins which are not required anymore.

    <VirtualHost *:80>
        ServerName sonar.server.name
        
        ProxyPass / http://localhost:9000/sonar/
        ProxyPassReverse / http://localhost:9000/sonar/
        
        ErrorLog logs/sonar/error.log
        CustomLog logs/sonar/access.log common
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName jenkins.server.name
        
        ProxyPass / http://localhost:8080/jenkins/
        ProxyPassReverse / http://localhost:8080/jenkins/
        
        ErrorLog logs/jenkins/error.log
        CustomLog logs/jenkins/access.log common
    </VirtualHost>
    

    Here, use http://jenkins.server.name/ or http://sonar.server.name/.


    Option 3 Keep one domain, no prefix and split on the /something

    All in one Virtual Host. Like this:

    <VirtualHost *:80>
        ServerName server.name
        
        ProxyPass /sonar http://localhost:9000/sonar/
        ProxyPassReverse /sonar http://localhost:9000/sonar/
    
        ProxyPass /jenkins http://localhost:8080/jenkins/
        ProxyPassReverse /jenkins http://localhost:8080/jenkins/
        
        ErrorLog logs/error.log
        CustomLog logs/access.log common
    </VirtualHost>
    

    Sadly doing it this way you loose the separate log files. CustomLog is only applicable in the global server configuration or inside a VirtualHost.

    Use http://server.name/jenkins/ or http://server.name/sonar/.


    Finally ensure you load module mod_proxy_html along with the other proxy modules.