Setting up a reverse proxy

watch 3m, 9s
views 2

08:30, 25.11.2022

Article Content
arrow

  • Why do we need a reverse proxy?
  • Using Nginx as a reverse proxy
  • How to configure Nginx as a reverse proxy for a proxy server without HTTP

A reverse proxy server is a kind of proxy server that broadcasts client requests from an external network to one or more servers located within the internal network. In the following article, we will tell you what it's for and how to configure it if necessary using the example of the Nginx web server.

Why do we need a reverse proxy?

There can be several scenarios for using the technology. Let's consider the main ones:

  1. Hiding the existence of the servers it polls, as well as their characteristics.
  2. Protection against DoS and DDoS – for this in conjunction with the reverse proxy system administrators put a software firewall.
  3. As a crutch – if the main site does not support connection via SSL, you can connect a reverse proxy server with a hardware SSL acceleration.
  4. Use as a load balancer – such a server can provide even load distribution between two, three, or more servers.
  5. Reducing the load on the primary server by placing dynamic and static content on the reverse proxy. This method is often called acceleration.
  6. Data compression in order to reduce load time.
  7. Can be used for secure and convenient A/B testing.

Now you know what the reverse proxy is for. Let's move on to practice.

Using Nginx as a reverse proxy

The first thing to do is to open the configuration file of the domain server block. In it, you need to specify the location and the proxy server

server {
    listen 80;
    server_name www.example.com example.com;
 
    location /app {
       proxy_pass http://127.0.0.1:7070;
	}

}


The URL of the server to be proxied is set with the proxy_pass directive proxy_pass. This allows you to use HTTP or HTTPS:

  • as a protocol;
  • domain;
  • IP address;
  • optional port;
  • unified resource identifier as an address.

The above configuration instructs Nginx to forward all requests to /app to the proxy at http://127.0.0.1:7070. You can change it to the IP you want. This is provided for reference only.

setting up a reverse proxy

Files with server blocks are stored in /etc/nginx/sites-available – if you use Ubuntu or Debian, as well as in /etc/nginx/conf.d if using CentOS.

To illustrate how the proxy_pass location and proxy_pass directive works, let's take a simple example:

server {
    listen 80;
    server_name www.example.com example.com;
 
    location /blog {
       proxy_pass http://node1.com:6000/wordpress/;
	}

}


When a visitor goes to http://example.com/blog/my-post, the Nginx web server proxies the request to http://node1.com:6000/wordpress/my-post.

If the address of the proxied server contains a unified resource identifier, ( /wordpress/ ), the request URI translated to the proxied server is replaced by the URI specified in the directive. In case the proxy server address is specified without a unified resource identifier, the full request URI is sent to the proxy server.

How to configure Nginx as a reverse proxy for a proxy server without HTTP

One of the four directives is used for this:

  • fastcgi_pass – reverse proxy to FastCGI server.
  • uwsgi_pass – reverse proxy to uwsgi server.
  • scgi_pass – reverse proxy to SCGI server.
  • memcached_pass – reverse proxy to Memcached server.

For example, very often Nginx is used as a reverse proxy PHP-FPM:

server {
 
	# ... other directives
 
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
	}

}



If you change the configuration file, be sure to restart the Nginx web server for the changes to take effect. If you still have questions, please contact our experts via live chat.

Share

Was this article helpful to you?

VPS popular offers

-10%

CPU
CPU
6 Epyc Cores
RAM
RAM
16 GB
Space
Space
150 GB NVMe
Bandwidth
Bandwidth
Unlimited
KVM-NVMe 16384 Linux

60.5 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-KVM-SSD 4096 Linux

60.5 /mo

/mo

Billed annually

-10.2%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
100 Mbps
DDoS Protected SSD-KVM 16384 Linux

123 /mo

/mo

Billed semiannually

-21.5%

CPU
CPU
2 Xeon Cores
RAM
RAM
2 GB
Space
Space
75 GB SSD
Bandwidth
Bandwidth
300 GB
wKVM-SSD 2048 HK Windows

26 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
400 GB HDD
Bandwidth
Bandwidth
300 Gb
KVM-HDD HK 16384 Linux

40.55 /mo

/mo

Billed annually

-15.5%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
80 Mbps
DDoS Protected SSD-KVM 8192 Linux

95 /mo

/mo

Billed annually

-20.6%

CPU
CPU
6 Xeon Cores
RAM
RAM
8GB
Space
Space
100GB SSD
Bandwidth
Bandwidth
500GB
KVM-SSD 8192 HK Linux

59 /mo

/mo

Billed annually

-15.6%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
30 Mbps
DDoS Protected SSD-KVM 1024 Linux

38 /mo

/mo

Billed annually

-10%

CPU
CPU
8 Xeon Cores
RAM
RAM
32 GB
Space
Space
200 GB SSD
Bandwidth
Bandwidth
12 TB
KVM-SSD 32768 Metered Linux

150 /mo

/mo

Billed annually

-7.1%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 4096 Windows

21 /mo

/mo

Billed annually

Other articles on this topic

cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.