Nginx Reverse Proxy
In this tutorial, we will discuss another Nginx feature. The Nginx web server has a number of sophisticated features that most specialised applications do not, like load balancing, TLS/SSL capabilities, and acceleration. Nginx HTTPS reverse proxy receives a client request, forwards it to one or more servers, and then returns the server's response to the client.
Why should you use Nginx Reverse Proxy?
Better Performance: Nginx is more efficient at analysing URLs and delivering static content files. Load balancing is a function that a Nginx reverse proxy can carry out to assist distribute client requests equally among backend servers. It also makes redundancy better. The reverse proxy switches requests to a different server in accordance with the routing policy if one server goes down.
Enhanced security: Your backend servers' first line of protection is a reverse proxy like Nginx. By setting up a reverse proxy, you may prevent anyone from discovering the identity of your backend servers. Users gain access to a secure HTTPS connection that is secured using TLS, securing their data, by encrypting the connection between the client and the Nginx reverse proxy.
How to add a domain to nginx?
1. Add reverse proxy configuration
$ sudo touch /etc/nginx/sites-available/reverse-proxy.conf
2. Open the settings file in a text editor.
$ sudo vi /etc/nginx/sites-available/reverse-proxy.conf
How to configure Nginx as a reverse proxy server?
The simplest use case of Nginx as a reverse proxy server is to listen on a specific port and forward the query to another serer, i.e, apache. Nginx's proxy pass directive enables reverse proxy capabilities.
server {
listen 80;
location / {
proxy_pass http://192.168.0.2;
}
}
Here, the nginx reverse proxy is listening on port 80 for the http server running on different IP.
You can specify the domain name which the reverse proxy responds to. Also, a single server_name
can join numerous application servers through a proxy into a single unified web application by adding additional location blocks as necessary.
server {
listen 80;
server_name domain.com www.domain.com;
location / {
proxy_pass http://192.x.x.2;
}
}
The common parameters used in Nginx configuration file with a reverse proxy configuration to tweak the working of proxy are shown in the configuration below:
proxy_http_version
- Defines the HTTP protocol version (the default is set to 1.0
)
proxy_cache_bypass
- This configuration allows responses to bypass cache.
X-Forwarded-For $proxy_add_x_forwarded_for
- Defines the address of the client connected to the proxy.
X-Real-IP $remote_addr
- Contains the client IP address. It forwards the real visitor remote IP address to the proxied server.
X-Forwarded-Host $host
- Defines the original host requested by the client.
X-Forwarded-Proto $scheme
- If defined in an HTTPS server block, the HTTP responses from the proxied server are rewritten to HTTPS.
X-Forwarded-Port $server_port
- Defines the original port requested by the client.
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
server_name domain.com www.domain.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
Finally, You can save the file and exit the editor.
Verify our configuration, then launch Nginx.
It is a recommended practice to test new configurations before loading them to production server because a single syntax mistake will prevent the Nginx service from running, which will prevent users from accessing your website. You can use the following command to do so.
$ sudo nginx -t -c /etc/nginx/sites-available/reverse-proxy.conf
Enable the site if no issues were discovered.
Finally, Start/Restart/Reload the server to load the changes:
$ sudo systemctl restart nginx
or
$ sudo service nginx restart