Reverse-proxying web applications with nginx

While you can use Apache as a reverse proxy, it tries to be too smart. Try nginx instead, it's much simpler to set up.

After struggling with Apache for quite a while, since I simply could not make it pass through the Digest Authentication of my target host (that I proxied to), I switched to nginx. Here is what I did.

  1. Have nginx

    sudo apt-get install nginx
    
  2. Define your nginx config, e.g. at /etc/nginx/conf.d/reverse-proxy.conf:

    server {
      listen 127.0.0.1;
      
      location /foo/ {
        proxy_pass https://www.example.com/;
      }
    }
    
  3. Reload your config (if it fails run sudo nginx -t to debug)

    sudo service nginx reload
    
  4. Done. Visit http://localhost/foo/ and you'll see results of https://www.example.com/.

Note that nginx handles HTTPS out of the box.
Also, should the target host require some kind of HTTP Authentication, it will simply work as nginx passes along all requests (there are multiple for Digest Auth).

Full disclosure: I did not have to rewrite cookies (as shown in the Apache Reverse Proxy guide) in my case, but I assume nginx supports that. Update this card if you know how, or drop us a line.

Arne Hartherz Over 8 years ago