Skip to content

Instantly share code, notes, and snippets.

@weiland
Created July 14, 2014 14:08
Show Gist options
  • Save weiland/59916409e676a4a5c1ce to your computer and use it in GitHub Desktop.
Save weiland/59916409e676a4a5c1ce to your computer and use it in GitHub Desktop.
Reverse Proxy Setup

Reverse Proxy Setup

for Nginx and Apache

Local usage

(a normal reverse tunnel: ssh -R 61001:localhost:22 user@myhost.tld)

Asuming your local application runs on port 3000 (http://localhost:3000/ in the browser shows your nice webpage) we login with a user noroot and the ssh port 22 on the host myhost.tld to forward that to the server port 61001. Being logged in on the the server noroot@myhost.tld:22 the command curl -v http://localhost:61001/ should be equal to on the local machine http://localhost:300
just run: ssh -i ~/.ssh/server.key -l noroot -p 22 myhost.tld -R 61001:localhost:3000

Apache Setup

Enable mod_rewrite (in case you dont have it yet enabled) and mod_proxy with some sub modules:

a2enmod rewrite
a2enmod proxy
a2enmod proxy_connect
a2enmod proxy_http

and restart Apache /etc/init.d/apache2 restart or service apache2 restart.

An .htaccess could look like:

RewriteEngine on
RewriteRule ^srv/(.*) http://localhost:61001/$1 [P]

don't forget the [P] flag

And a mod_virtualhost conf in the sites-enabled/ directory could look like:

<VirtualHost *:*>
    ProxyPreserveHost On

    # ProxyPass / http://[IP ADDR]:[PORT]/
    # ProxyPassReverse / http://[IP ADDR]:[PORT]/
    
    ProxyPass / http://0.0.0.0:8080/
    ProxyPassReverse / http://0.0.0.0:8080/

    ServerName localhost
</VirtualHost>

And the SSL virtual host conf would look like:

Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/cert.pem
    ProxyPass / http://0.0.0.0:8080/
    ProxyPassReverse / http://0.0.0.0:8080/
</VirtualHost>

Nginx Setup

Nginx proxy_pass Conf

location ^~ / {
	error_page 500 502 404 403 /info.html;
	proxy_pass	http://localhost:61001;	
}

location = /info.html {
	root /home/web/static_content
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment