How to install and configure NGINX as reverse proxy

NGINX is a web server with excellent performance and low memory footprint. NGINX can be used as a reverse HTTP proxy, as a generic TCP / UPD proxy, as a mail proxy server and as a load balancer, today we will install NGINX on Debian GNU/Linux and use it as a generic TPC/UPD proxy.

Install

# apt install nginx 

Start

# systemctl start nginx

Stop

$ systemctl stop nginx 

Restart

# systemctl restart nginx

Start with the OS

# systemctl enable nginx

Reload configuration

You must run the following command after making any modification to any configuration file.

# systemctl reload nginx

Check configuration files

Below we show where the configuration files are found for Debian/Ubuntu

/etc/nginx/
├── conf.d
├── modules-available
├── modules-enabled
├── sites-available
├── sites-enabled
└── snippets

DIRs to pay attention:

  • sites-available: It contains the configuration of all our sites.
  • sites-enabled: It contains the configuration of all our enable sites.

With this hierarchy it is very easy to enable/disable sites: it would be enough to create/delete a symbolic link from sites-enabled DIR.

Configure as reverse proxy.

Create a template under sites-available in order to make easy the new site configuration.

server {
	listen 80;
	
	# Put here you domain	
	#
	server_name midominio.com;

	# Max file size useful for file uploading 
	# 
	client_max_body_size 8M;
	
	location / {	
	    # NGINX acting as reverse proxy
	    #
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_set_header X-Forwarded-Proto $scheme;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header Host $http_host;
	    #
	    # Set the backend url and port, port is optional for standard services
	    # when there is a variable as a part of proxy_pass URL a resolver is needed.
	    #
	    proxy_pass http://miurl.internal:port;
	}
}

Let’s comment some directives:

  • listen: Sets the address and port for IP, or the path for a UNIX-domain socket on which the server will accept requests. Both address and port, or only address or only port can be specified. An address may also be a hostname.
  • server_name: Sets names of a virtual server.
  • proxy_set_header: Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations.

Add new site

Exec:

# cp -v /etc/nginx/sites-available/proxy /etc/nginx/sites-available/mynewsite

Edit the mynewsite and set the server_name and proxy_pass directives according your needs.

Enable the new site

# ln -s /etc/nginx/sites-available/minuevodominio /etc/nginx/sites-enabled/mynewsite 

Reload configurations

# systemctl reload nginx

Disable a site

# rm -v /etc/nginx/sites-enabled/mynewsite 

You must reload configurations after disable a site.

YouTube Video

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.