Simple Reverse Proxy with Nginx, Ubuntu

Thankfully, through a few tutorials, trial and error and some coffee, I was able to configure a working configuration for nginx to operate as a reverse proxy. The VPS Nginx is stored on does not have much resources, so instead of having it serve content and consuming its energy, it instead forwards all traffic. No filtering is necessary as we’re passing all HTTP traffic to a secondary server, or “upstream.” Nginx also has the capability of forwarding multiple virtual hosts, but your upstream should also be configured to accept correct domains, as the $upstream will be configured to point directly to an IP – This is to avoid simple DNS issues.

$upstream will usually be a suitable web server, often bulky, and serving plenty of content and often multiple websites. If not, that’s okay. One website will still work! Simply replicate the configuration for any additional websites.

For HTTP setup:

/etc/nginx/sites-available/default

server {

root /var/www/html;

server_name your.tld;

set $upstream your.ip;

location / {

proxy_pass_header Authorization;
proxy_pass http://$upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
client_max_body_size 0;
proxy_read_timeout 36000s;
proxy_redirect off;

}

}

With this simple config, placed both in sites-available and sites-enabled you should be able to forward all incoming HTTP traffic to a secondary web server.

Featured image from Foter




Write a Comment

Your email address will not be published. Required fields are marked *

3 × two =