Carrying cookies across redirects when using Apache as a reverse proxy

5,167

Solution 1

You can't have a port specification on ProxyPass /app:81 - instead, the port to listen on should be determined by the VirtualHost block that the ProxyPass resides in.

Create a second VirtualHost set to port 81, then place a ProxyPass /app http://x.com:81 inside of it to get the behavior you're looking for.

Solution 2

Well, you could kind of use "port" numbers in the path just don't use ":"'s.

ProxyPassReverse /app ht+p://x.com
ProxyPassReverse /app81 ht+p://x.com:81

Assuming your site is ht+p://y.com:

y.com/app --> x.com

y.com/app81 --> x.com:81

I prefer using names of the applications on the backend. In fact, you may have many different application servers but want all web requests to look like they came from the same spot:

ProxyPassReverse /games ht+p://x.com:8080/games
ProxyPassReverse /finance ht+p://f.com:7000/money
ProxyPassReverse /movies ht+p://m.com:8001/cinema

y.com/games --> x.com:8080/games

y.com/finance --> f.com:7000/money

y.com/movies --> m.com:8001/cinema

Share:
5,167

Related videos on Youtube

dbotha
Author by

dbotha

Co-founder of Kite.ly (acq'd by Canon). Aspiring game developer.

Updated on September 18, 2022

Comments

  • dbotha
    dbotha over 1 year

    I'm attempting to setup a reverse proxy using Apache on my local machine, I have the following in my httpd.conf:

    ProxyPass /app http://x.com
    ProxyPassReverse /app http://x.com
    

    Everything works great and browsing to 127.0.0.1/app/* works as expected. The problem arises when I browse to a url that performs a redirect to a resource at the same hostname but a different port. Initially I thought I could handle this situation as follows:

    ProxyPass /app:81 http://x.com:81
    ProxyPassReverse /app:81 http://x.com:81
    

    But whilst this works, cookies don't appear to carry across the redirect. How would I get cookies to carry across the redirect? Also whilst the port is actually known in advance, is there a more robust method of handling this such that a redirect to any arbitrary port is handled correctly?