skip to Main Content

I’m trying to route any links that start with /api/ to port 3002 on my server but they always get routed to 3008. For example https://example.com/api/customers should be proxied/routed to localhost:3002

<VirtualHost *:443>
    ServerAdmin (redacted)
    ServerName (redacted)
    ServerAlias (redacted)

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/(redacted)/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/(redacted)/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/(redacted)/chain.pem

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

    <Location "/api/(.*)/">
        ProxyPass http://localhost:3002/
        ProxyPassReverse http://localhost:3002/
    </Location>

    <Location "/">
        ProxyPass http://localhost:3008/
        ProxyPassReverse http://localhost:3008/
    </Location>
</VirtualHost>

Using this config going to domain.com works and shows my website but domain.com/api/customers returns an error from the webapp on port 3008 so it’s not being routed correctly (it should go to 3002).

The apps on port 3008 and 3002 are running correctly so that’s not the issue.
I’ve tried putting domain/ first and domain/api last in the config file but that didnt seem to fix it. And the config file is enabled

I’ve tried different regexes to match the api endpoint aswell but this one should work

Apache is listening on port 443

These mods are enabled which should be needed for this:
proxy_module (shared)
proxy_http_module (shared)
proxy_wstunnel_module (shared)

Please let me know if you want any extra information

2

Answers


  1. Chosen as BEST ANSWER

    I ended up solving it like this:

    RewriteEngine  on
    RewriteRule "/api/(.*)" "http://localhost:3002/api/$1" [P] 
    
    <Location "/">
        ProxyPass http://localhost:3008/
        ProxyPassReverse http://localhost:3008/
    </Location>
    
    

  2. Modify your config as below and have a try.

    Post the access log and curl response if not working.

    curl -ILKv https://domain.name/api/anything

    curl -ILKv https://domain.name/api

    https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </Proxy>
    ProxyPass /api http://localhost:3002
    ProxyPassReverse /api http://localhost:3002
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search