Ssl – Apache ProxyPass only one file to a different localhost port

Apache2PROXYproxypassssl

I haven't been able to find a solution to this exact problem:

  • I have one app running on localhost:3000
  • I have a proxy set up for my localhost:3000 app with qa.mysite.com (for other js)
  • I added a second app running on localhost:3333

The first app needs to reference some localhost:3333 css and js files:

<link rel="stylesheet" href="/css/my-css.css"/>
<script src="/js/my-js.js" defer></script>

I tried this conf (and variations):

<VirtualHost *:443>
    ServerName qa.mysite.com
    SSLEngine on
    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    SSLCertificateFile "/etc/apache2/server.crt"
    SSLCertificateKeyFile "/etc/apache2/server.key"
    ProxyVia Full
    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:3000/"
    ProxyPassReverse "/" "http://localhost:3000/"

    ProxyPass "/css/my-css.css" "http://localhost:3333/css/my-css.css"
    ProxyPassReverse "/css/my-css.css" "http://localhost:3333/css/my-css.css"
    ProxyPass "/js/my-js.js" "http://localhost:3333/js/my-js.js"
    ProxyPassReverse "/js/my-js.js" "http://localhost:3333/js/my-js.js"

</VirtualHost>
  • Hitting qa.mysite.com yields the first app running at localhost:3000.
  • But also CORS errors or refused connections.

Any idea what I'm doing wrong? 🙂

Best Answer

You need to put the more specific ProxyPass rules first. "/css/my-css.css" will evaluate "/" first. Since that holds true, you'll get port 3000.

Put the ProxyPass to "/" last as it's the all-inclusive fallback.

<VirtualHost *:443>
    ServerName qa.mysite.com
    SSLEngine on
    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    SSLCertificateFile "/etc/apache2/server.crt"
    SSLCertificateKeyFile "/etc/apache2/server.key"
    ProxyVia Full
    ProxyPreserveHost On

        #Specific /css
    ProxyPass "/css/my-css.css" "http://localhost:3333/css/my-css.css"
    ProxyPassReverse "/css/my-css.css" "http://localhost:3333/css/my-css.css"

        #Specific /js
    ProxyPass "/js/my-js.js" "http://localhost:3333/js/my-js.js"
    ProxyPassReverse "/js/my-js.js" "http://localhost:3333/js/my-js.js"

       #Catch ALL for servername
    ProxyPass "/" "http://localhost:3000/"
    ProxyPassReverse "/" "http://localhost:3000/"
</VirtualHost>
Related Topic