This post shows students and new users the steps to configure a reverse proxy on the Nginx HTTP server. Nginx is often used as a reverse proxy for HTTP and non-HTTP servers. A reverse proxy is a service that sits between the client and the 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 ease the load on the backend servers.

Usually a reverse proxy server is used against Node.js, Python, Java, and other popular applications that do not have web server functions enabled. In this case, Nginx is usually the proxy server used to handle client requests.

Next, we will show you how to use Nginx as a reverse proxy for most applications and backend servers. We will give you some basic settings that can be used in your environments.

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

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

How to use Nginx as a reverse proxy server

The basic configuration to allow Nginx to serve as a proxy server can be done using the line code below. Configure the server block for the domain and specify a location to the backend server for Nginx to send the 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 proxy server in http://127.0.0.1:8080.

YOU CAN ALSO READ:   How to run an antivirus scan

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

There are many advanced settings that can be done with a proxy server. However, the simple setup above gives you a good idea of ​​how a proxy server works.

Proxy servers can also handle requests for non-HTTP servers, such as:

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

A common Nginx reverse proxy for non-HTTP hosts is made with the use of PHP-FPM. An example is how Nginx serves 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 settings

Reverse proxy servers have common options that define how requests to back-end servers or applications are handled and served. It is recommended to use these settings with the Nginx proxy. In most cases, these headers and parameters should work in all environments where Nginx is 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.

You should do that!

Conclusion:

In this tutorial we have seen how to use Nginx as a reverse proxy server to handle requests from back-end applications or other HTTP servers. If you find any errors above or have something to add, please use the comment form below.

Write A Comment