How to Setup Nginx Reverse Proxy

rocket 3122690 640

This post shows students and new users steps to setup a reverse proxy on Nginx HTTP server. Nginx is often used as a reverse proxy for HTTP and non-HTTP servers. A reverse proxy is a service sits between the client and backend servers.

The proxy server takes and directs client requests to the appropriate backend servers. A proxy server can also perform additional tasks such as SSL encryption, caching, compression and load balancing to take the load off the backend servers.

A reverse proxy server is typically used in front of Node.jsPythonJava and other popular applications that do not have web server features enabled. In this case, Nginx is usually the proxy server used to handle requests for clients.

Below we’ll show you how to use Nginx as a reverse proxy for most applications and backend servers. We’ll give you some basic settings that may be used in your environments.

Also, for students and new users learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.

To get started with using Nginx as a reverse proxy, follow the steps below.

How to use Nginx as reverse proxy server

The basic configuration to allow Nginx to serve as a proxy server can be done using the line code below. You configure a server block for the domain and specify a location to the backend server for Nginx to send requests it receives.

server {
    listen 80;
    server_name example.com;

    location /backend_app {
       proxy_pass http://127.0.0.1:8080;
    }
}

The simple configuration above tells Nginx to pass all requests to the /backdend_app location to the proxied server at http://127.0.0.1:8080.

For example, if a client request http://example.com/backend_app, Nginx will forward the requests to the backend server defined on the proxy_pass line: http://127.0.0.1:8080.

There are lots of advanced setup one can perform with proxy server. However, the simple configuration above gives you a good idea of how a proxy server works.

Proxy servers can also handle request for non-HTTP server such as:

  • fastcgi_pass – reverse proxy to a FastCGI server.
  • uwsgi_pass – reverse proxy to a uwsgi server.
  • scgi_pass – reverse proxy to an SCGI server.

A common Nginx reverse proxy to non-HTTP hosts is done with the use of PHP-FPM. An example is how Nginx serve PHP scripts.

server {

    # ... other server block configs

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}

How to configure Nginx reverse proxy options

Reverse proxy servers have common options that define how requests are handled and served to the backend servers or apps. These configuration options are recommended to use with Nginx proxy. In most cases, these headers and parameters should work in all environments where Nginx is being used as a reverse proxy.

 location/ {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version  1.1;
    proxy_cache_bypass  $http_upgrade;

    proxy_set_header Upgrade           $http_upgrade;
    proxy_set_header Connection        "upgrade";
    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_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host  $host;
    proxy_set_header X-Forwarded-Port  $server_port;
  }

You can visit the site for more options to use in your environment.

That should do it!

Conclusion:

This post showed you how to use Nginx as reverse proxy server to handle requests for backend applications or other HTTP servers. If you find any errors above or have something to add, please use the comment form below.

Leave a Reply

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.