---
title: "Using NGINX Proxypass to Set Up a Reverse Proxy Server"
description: "Dive deep into the power of the NGINX ProxyPass directive and learn how NGINX glues together every service you may need!"
canonical: "https://adamtheautomator.com/nginx-proxypass/"
---

# Using NGINX Proxypass to Set Up a Reverse Proxy Server

> Dive deep into the power of the NGINX ProxyPass directive and learn how NGINX glues together every service you may need!

Source: https://adamtheautomator.com/nginx-proxypass/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Using NGINX Proxypass to Set Up a Reverse Proxy Server](https://adamtheautomator.com/wp-content/uploads/2022/07/Using-NGINX-Proxypass-to-Set-Up-a-Reverse-Proxy-Server.jpg)

# Using NGINX Proxypass to Set Up a Reverse Proxy Server

[![](https://secure.gravatar.com/avatar/c33e3be4c378d7366050eb6c4da41523f27a6c41e9e9d8332a085f42adcdc8e3?s=192&d=mm&r=g)Inam Ul Haq](https://adamtheautomator.com/author/inam-ul-haq/)22 July 20226 min. read

Categories: [DevOps](/category/devops/)

Tags:[NGINX](/tag/nginx/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [What is an NGINX ProxyPass or Reverse Proxy Server?](#what-is-an-nginx-proxypass-or-reverse-proxy-server)
*   [Configuring NGINX Server as a Reverse Proxy Server](#configuring-nginx-server-as-a-reverse-proxy-server)
*   [Passing Headers to Handle Proxied Requests](#passing-headers-to-handle-proxied-requests)
*   [Sending Redirect Response](#sending-redirect-response)
*   [Hiding and Permitting Headers](#hiding-and-permitting-headers)
*   [Increasing Performance with NGINX Load Balancing](#increasing-performance-with-nginx-load-balancing)
*   [Conclusion](#conclusion)

Are you planning to create a web application that is only accessible in a private network? Why not use NGINX ProxyPass (proxy\_pass) to set up NGINX as a reverse proxy server?

The proxy\_pass directive in the ngx\_http\_proxy\_module of NGINX lets you configure NGINX to expose your web application to the world while keeping privacy control. And in this tutorial, you’ll learn about different configurations to define your application’s privacy and increased performance.

Read on and keep your web application secure with NGINX ProxyPass!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   A Linux server – This tutorial uses Ubuntu 20.04 LTS.
    
*   [](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04)[NGINX](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04) and Docker are installed and running on your Linux machine.
    

Related:[How to Install and Use Docker on Ubuntu (In the Real World)](https://adamtheautomator.com/docker-ubuntu/)

*   A web application running on 127.0.0.1:8000.

## What is an NGINX ProxyPass or Reverse Proxy Server?

Before diving deep into NGINX proxy\_pass, take a look at what is a reverse proxy server first. Reverse Proxy servers provide an additional layer of security for web applications.

In addition, reverse proxy servers are used for load-balancing, providing a single face for a server application that spans across different servers.

A reverse proxy server is a kind of server that listens to client requests and forward or relays the requests to the relevant web application. At the same time, it responds to the client with the web application’s response.

## Configuring NGINX Server as a Reverse Proxy Server

By default, NGINX works as a static content web server, so you’ll configure NGINX as a reverse proxy server by editing NGINX’s configuration files. Depending on your Linux server’s operating system and its distribution, NGINX’s configuration files can be found at one of the places below:

*   _/etc/nginx_
*   _/usr/local/nginx/conf_
*   _/usr/local/etc/nginx_

But since this tutorial uses Ubuntu, you’ll work on the configuration file located at the _/etc/nginx_ directory:

1\. Run the following commands to change your working directory (cd) to the /etc/nginx directory, and list all files and directories (ls) inside the working directory.

```powershell
cd /etc/nginx
ls
```

Below, you can see the nginx.conf file, which is the base file for NGINX configurations.

![Listing files inside the /etc/nginx directory](https://adamtheautomator.com/wp-content/uploads/2022/07/image-197.png)

Listing files inside the /etc/nginx directory

2\. Next, run the below rm command, which doesn’t provide output but deletes the sites-available/default file.

```bash
sudo rm sites-available/default
```

3\. Create a new file called sites-available/default file with your preferred text editor.

```bash
sudo nano sites-available/default
```

4\. Now, add the configuration below to the _sites-available/default_ file, save the changes and close the editor. The following server block (defined using the [server directive](http://nginx.org/en/docs/http/ngx_http_core_module.html#server)) contains the entire configuration for a virtual host.

The directives below control the configuration:

*   `[listen](http://nginx.org/en/docs/http/ngx_http_core_module.html#listen) - This directive specifies which port a particular server block should listen to for requests.`
    
*   `[access_log](http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log) and [error_log](http://nginx.org/en/docs/ngx_core_module.html#error_log) - These directives specify the path for a server block to write access and error logs.`
    
*   [](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)[`location`](http://nginx.org/en/docs/http/ngx_http_core_module.html#location) – This directive specifies configurations depending on the URI of the request. In the configuration below, every request to the server block is handled by proxy\_pass.
    
*   [proxy\_pass](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) – This directive specifies the proxied server’s address. The configuration below tells NGINX to pass every request to the proxied application on http://127.0.0.1:8000.
    

```
server {
    # The listen directive serves content based on the port defined
    listen 80; # For IPv4 addresses
    listen [::]:80; # For IPv6 addresses

    # Replace the below if you have a domain name pointed to the server
    server_name your-domain.com;

    access_log /var/log/nginx/reverse-access.log;
    error_log /var/log/nginx/reverse-error.log;

    location / {
				# Specify the proxied server's address
        proxy_pass http://127.0.0.1:8000>;
    }
}
```

> _Note that the NGINX server can serve more than one application with server blocks._

Related:[How To Serve NGINX Subdomains or Multiple Domains](https://adamtheautomator.com/nginx-subdomain/)

5\. Run the following command to test (-t) your NGINX configuration.

```bash
sudo nginx -t
```

![Testing NGINX configuration](https://adamtheautomator.com/wp-content/uploads/2022/07/image-198.png)

Testing NGINX configuration

Related:[Steering NGINX Test Config Before Screwing it Up](https://adamtheautomator.com/nginx-test-config/)

6\. Next, run the below systemctl command to restart the NGINX service so that new configurations can take effect.

```bash
sudo systemctl restart nginx
```

Related:[Correct Way of Using Ubuntu systemctl to Control Systemd](https://adamtheautomator.com/ubuntu-systemctl/)

> _If there’s an error in the configuration, the NGINX service will not restart. Instead, you’ll see instructions on how to find the logs. On ubuntu, you can see logs running the journalctl -xe command._

7\. Finally, navigate to your web server’s domain name or IP address to access the application through NGINX reverse proxy server shown below.

![Accessing the web application through NGINX reverse proxy server](https://adamtheautomator.com/wp-content/uploads/2022/07/image-199.png)

Accessing the web application through NGINX reverse proxy server

## **Passing Headers to Handle Proxied Requests**

Apart from proxy\_pass, NGINX offers many other directives to handle requests to your server blocks. One of these directives is [proxy\_set\_header](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header), which lets you pass/rewrite headers to handle proxied requests.

> _This tutorial can’t cover all headers, but you’ll go through some of the most common headers (Host, X-Forwarded-For, and X-Real-IP)._

1\. Add the following statement above the proxy\_pass in your server block under location to pass the Host header in your _sites-available/default_ file. The Host header is sent by HTTP clients/browsers with the application domain name they are trying to access.

```
proxy_set_header Host $host;
```

By default, NGINX rewrites the Host header to the proxied server’s address [($host)](https://nginx.org/en/docs/http/ngx_http_core_module.html?#var_host) before passing the Host header to the proxied server. The $host variable is set by NGINX, which holds the value of the original request’s Host header or server block’s server\_name value.

When the proxied application receives the request, the Host header is set to http://127.0.0.1:8000. Essentially NGINX sets the Host header to your proxy server’s domain name/IP address. This behavior lets your application know it’s being accessed by a designated address rather than from 127.0.0.1.

2\. Next, add the statement below to the _sites-available/default_ file as you did in step one.

Your application uses the X-Forwarded-For header to identify the client’s IP address trying to access the application. This header identifies proxy servers used for the request to reach your application.

Below, the [$proxy\_add\_x\_forwarded\_for](http://nginx.org/en/docs/http/ngx_http_proxy_module.html) variable holds the value of the request’s X-Forwarded-For header with the client’s IP address.

```
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
```

3\. Add the last statement below to your _sites-available/default_ file.

Web applications use the X-Real-IP header set to the $remote\_addr variable to hold the IP address of the HTTP client accessing the server.

```
proxy_set_header X-Real-IP $remote_addr;
```

After applying the common configurations used for a reverse proxy server with the previous steps, you’ll get the complete configuration below.

```powershell
server {
    listen 80;
    listen [::]:80;

    # Replace the below if you have a domain name pointed to the server
    server_name your-domain.com;

    access_log /var/log/nginx/reverse-access.log;
    error_log /var/log/nginx/reverse-error.log;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass <http://127.0.0.1:8000>;
    }
}
```

4\. Now, run the systemctl command below, which doesn’t provide output but restarts the NGINX service.

```
sudo systemctl restart nginx
```

5\. Finally, try accessing your web application in your browser with the new configuration. If all goes well, you’ll see the output below.

![Verifying the web application is accessible with different configuration](https://adamtheautomator.com/wp-content/uploads/2022/07/image-199.png)

Verifying the web application is accessible with different configuration

## Sending Redirect Response

Imagine your application sends a redirect response using the Location header that looks like this, _Location: <http://127.0.0.1/user/edit/3_. You can see that the _127.0.0.1_ address is not descriptive about what’s being accessed. So can you fix it? Yes!

Add either of the following proxy\_redirect directives inside the location block in the _sites-available/default_ file. This directive rewrites the Location or Refresh header sent by the proxied application to the actual URL being used to access your application.

```
proxy_redirect <http://127.0.0.1/user/edit/3>  <http://your-domain.com/user/edit/3>;
# For changing all URLs, you can use the one below
proxy_redirect <http://127.0.0.1/>  <http://your-domain.com/>;
```

## Hiding and Permitting Headers

NGINX, by default, hides some headers sent to the client by proxied applications. But by using the proxy\_hide\_header directive, you can tell NGINX to hide more headers.

Add the directive below to the location block in the _sites-available/default_ file to automatically hide the headers from the response sent to the HTTP client.

Hiding headers help secure sensitive information of the app and the app headers from being misused by malicious attackers.

```php
proxy_hide_header Cache-Control;
```

Or, perhaps you want some header permitted sent to the client that is otherwise hidden. If so, you can use the `proxy_pass_header` directive like the one below.

```
proxy_pass_header Date;
```

## Increasing Performance with NGINX Load Balancing

Load balancing is a powerful configuration in your server. The upstream directive in NGINX helps your application balance its load to increase the application performance. At the same time, the upstream directive makes your application scalable.

To configure your NGINX server for load balancing:

1\. Create a new file inside the NGINX configuration folder (/etc/nginx/conf.d/) with your text editor, and name the file as load-balancer.conf.

```powershell
sudo nano /etc/nginx/conf.d/load-balancer.conf
```

2\. Next, add the configuration below to the _load-balancer.conf_ file, save the changes and close the file.

You can see that the [upstream directive](https://nginx.org/en/docs/http/ngx_http_upstream_module.html) defines either a web server cluster for load balancing or an app server cluster for routing/load balancing.

```
http {
   upstream your-domain.com {
        server backend1.your-domain.com:443;
        server backend2.your-domain.com:443;
   }

   server {
      listen 80; 

      location / {
         proxy_pass https://your-domain.com;
      }
   }
}
```

3\. Run the below rm command, which doesn’t provide output, but removes the default symbolic link (symlink) from the sites-enabled directory.

```bash
sudo rm /etc/nginx/sites-enabled/default
```

4\. Lastly, restart the NGINX service so new configurations can take effect.

```bash
sudo systemctl restart nginx
```

> _If you get an error in the configuration, open the load-balancer.conf file and double-check if you wrote the configuration correctly._

![Verifying the web application works with load balancing](https://adamtheautomator.com/wp-content/uploads/2022/07/image-199.png)

Verifying the web application works with load balancing

## **Conclusion**

Throughout this tutorial, you’ve learned why and where reverse proxy servers are used with NGINX ProxyPass. You’ve touched on using a couple of directives and headers to handle requests and controlling your web application’s privacy.

Load balancing and hiding headers help your web application’s performance and security. But wouldn’t it be great to add another layer of security? Why not **[activate NGINX to redirect HTTP to HTTPS traffic](https://adamtheautomator.com/nginx-to-redirect-http-to-https/)**?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fnginx-proxypass%2F&text=Using%20NGINX%20Proxypass%20to%20Set%20Up%20a%20Reverse%20Proxy%20Server)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fnginx-proxypass%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fnginx-proxypass%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/02/How-to-Set-Up-NGINX-on-a-Mac-for-Testing.jpg)

### [How to Set Up NGINX on Mac for Testing](/nginx-on-mac/)

Learn how to set up NGINX on a Mac for testing your website without building a separate server or VM in this step-by-step tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2021/07/How-to-Set-up-an-NGINX-Reverse-Proxy-in-Docker-Step-by-Step.jpg)

### [Create an NGINX Reverse Proxy in Docker and Learn 1 New Skill!](/nginx-reverse-proxy-in-docker/)

Learn how to set up an NGINX reverse proxy in Docker using PHP-FM and NodeJS in this step-by-step tutorial.

![](https://adamtheautomator.com/wp-content/uploads/2021/07/Getting-Started-with-NGINX-on-Docker.jpg)

### [Provision NGINX on Docker and Increase Server Capabilities](/nginx-on-docker/)

learn how to provision a new NGINX Docker container running on any operating system such as Windows, macOS, or Linux!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
