How to Setup and Configure Nginx as Reverse Proxy

Nginx is an open-source web server that can also serve as a reverse proxy. In this tutorial, we will learn how to set up and configure Nginx as Reverse Proxy.

use nginx as reverse proxy server

After a lot of appreciation for our series about Redis tutorials, we received a lot of requests to start a series on Nginx tutorials. So last week we started with a tutorial on how to use Nginx as load balancer for your applications. And now this week we’ll explore how to use Nginx as Reverse Proxy. So, let’s get started!

  • What is a Proxy Server
  • Forward vs Reverse Proxy
  • Configure Nginx as Reverse Proxy
  • Conclusion

What is a Proxy Server

A proxy server acts as a gateway between you and the internet. It’s an intermediary server separating end users from the websites they browse. Proxy servers provide different levels of functionality, security, and privacy as per your use case, or company policy.

With a proxy server, internet traffic flows through the proxy server on its way to the address you requested. The request then comes back through that same proxy server (mostly), and then the proxy server forwards the data received from the website to you.

Forward vs Reverse Proxy

nginx as reverse proxy server

Generally when we speak of the proxy, most of the time we mean the forward proxy. Forward Proxies are great for avoiding country restrictions, like the great firewall of China. The client simply connects to blocked resources via the forward proxy. Forward Proxies can hide user’s identities by changing their IP address. So basically forward proxy sits between the client and the internet, so the end server is not aware of the actual client.

nginx as reverse proxy

Reverse Proxies also act as intermediaries but they sit on the other side of the connection. Reverse proxies are excellent at load balancing, web optimization, and security. Reverse Proxying is typically used to distribute the load among several servers, seamlessly show content from different websites, or pass requests for processing to application servers over protocols other than HTTP.

Configure Nginx as Reverse Proxy

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client. It is possible to proxy requests to an HTTP server or a non-HTTP server using a specified protocol. Supported protocols include FastCGI, uwsgi, SCGI, and Memcached.

To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location. For example:

location /path/to/location/ {

This example configuration results in passing all requests processed in this location to the proxied server at the specified address. This address can be specified as a domain name or an IP address. The address may also include a port:

location ~ \.php {

You can even pass modified or custom headers to the proxied server like below in the example

location /path/to/location/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://localhost:8000;

Nginx also supports buffering which helps improve the performance. With buffering enabled Nginx will store the response it receives from the proxied server as long as the client needs it to download.

Instead of proxying all the requests to a single server you can also set up multiple backend servers and let the Nginx balance load between those proxied servers. We’ve already covered this in our tutorial about nginx load balancing.


In this tutorial, we explored what is a Proxy Server. Difference between forward vs reverse proxy. We also learned by example how to set up nginx as a reverse proxy. In our upcoming tutorials, we’ll discuss more interesting topics about Nginx.


You may find the more related below articles