How Can We Help?
NGINX Reverse Proxy
NGINX is a webserver to serve your desired websites. However, do you want NGINX to act as a reverse proxy to redirect traffic to the correct (web) server? This article will explain how to configure NGINX to be a reverse proxy.
Prerequisites
For this article, we assume you already have an NGINX webserver configured, if not have a look at this article.
Configuration
To let NGINX forward traffic to your desired (web) server, you first have to create a configuration (YOUR_SITE.conf) in /etc/nginx/sites-available
. Configure it as follows:
server {
listen 80;
listen [::]:80;
server_name example.com;
access_log /var/log/nginx/example-access.log;
error_log /var/log/nginx/example-error.log;
location / {
include proxy_params;
proxy_pass https://127.0.0.1;
}
}
Make sure to modify the configuration to be named to your website. In the above configuration, a file called proxy_params
is included. This file must be located in /etc/nginx
. Either open or create the file and add the following configuration.
# Defines the HTTP protocol version for proxying, by default it it set to 1.0;
# For Websockets and keepalive connections you need to use the version 1.1
proxy_http_version 1.1;
# Sets conditions under which the response will not be taken from a cache
proxy_cache_bypass $http_upgrade;
# Proxy headers
# These header fields are required if your application is using Websockets
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# The $host variable in the following order of precedence contains:
# - hostname from the request line
# - or hostname from the Host request header field
# - or the server name matching a request
proxy_set_header Host $host;
# Forwards the real visitor remote IP address to the proxied server
proxy_set_header X-Real-IP $remote_addr;
# A list containing the IP addresses of every server the client has been proxied through
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# When used inside an HTTPS server block:
# each HTTP response from the proxied server is rewritten to HTTPS
proxy_set_header X-Forwarded-Proto $scheme;
# Defines the original host requested by the client
proxy_set_header X-Forwarded-Host $host;
# Defines the original port requested by the client
proxy_set_header X-Forwarded-Port $server_port;
This configuration will enhance the headers to be forwarded to your (web) server, to further increase the functionality of your (web) server.
To enable your site link to /etc/nginx/sites-enabled
using:
ln -s /etc/nginx/sites-available/YOUR_SITE.conf /etc/nginx/sites-enable/YOUR_SITE.conf
To validate if the configuration is ok, run nginx -t
.
If the configuration is ok, NGINX can be reloaded by nginx -s reload
.
2 Responses
I appreciate the distinctive perspective you present in your writing.
This article spoke to me on a personal level; it hits all the right chords.