Introduction
NGINX is a powerful web server and reverse proxy that can be used to improve the performance and security of your web applications. It can be used to serve static content, handle load balancing, and provide secure access to your web applications. In this guide, we will show you how to set up and use NGINX as a reverse proxy. We will cover the basics of configuring NGINX, setting up a reverse proxy, and using NGINX to improve the performance and security of your web applications.
How to Set up & Use NGINX as a Reverse Proxy
1. Install NGINX
The first step is to install NGINX on your server. This can be done using the package manager of your choice. For example, on Ubuntu, you can use apt-get to install NGINX:
sudo apt-get install nginx
2. Configure NGINX
Once NGINX is installed, you need to configure it to act as a reverse proxy. This can be done by editing the nginx.conf file.
In the nginx.conf file, you need to add the following lines:
http {
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
}
}
}
This will tell NGINX to listen on port 80 and forward all requests to port 8080 on localhost.
3. Start NGINX
Once you have configured NGINX, you can start it by running the following command:
sudo service nginx start
4. Test NGINX
To test that NGINX is working correctly, you can use curl to make a request to your server:
curl http://example.com
If everything is working correctly, you should see the response from the server on port 8080.
What is a Reverse Proxy?
A standard proxy server works on behalf of clients, often by providing privacy or filtering content. A reverse proxy works on behalf of a server, intercepting traffic and routing it to a separate server.
There are several reasons you might want to install a reverse proxy. One of the main reasons is privacy.
If you have multiple servers, a reverse proxy can help balance loads between servers and improve performance. As a reverse proxy provides a single point of contact for clients, it can centralize logging and report across multiple servers.
Nginx can improve performance by serving static content quickly and passing dynamic content requests to Apache servers.
This guide will help you install and configure an Nginx reverse proxy on your system.
Prerequisites
- A Linux server with Apache, PHP, and a firewall
- Access to a root user with sudo access
- Linux command-line or terminal (Ctrl–Alt–T for Ubuntu, Alt–F2 for CentOS)
- Package manager (such as APT)
Setting Up an Nginx Reverse Proxy
Step 1: Install Nginx from Default Repositories
Open a terminal window and enter the following:
sudo apt-get update
Allow the package manager to finish refreshing the software lists, then enter the following:
sudo apt-get install nginx
Allow the process to complete.
Note: This is the easiest way to install Nginx on CentOS or Ubuntu, but it may not load the latest stable release. Move on to Step 2 to add and install from the Nginx software repositories.
Step 2 (optional): Install Nginx from Official Repository
Add Security Key
In a terminal window, enter the following:
sudo wget https://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
This downloads the signing key for Nginx, which verifies that you’re downloading authentic software.
Open sources.list File for Editing
In the terminal, enter the following:
sudo vi /etc/apt/sources.list
Add Nginx Sources to Repository List
Enter the following lines in the /etc/apt/sources.list
file you just opened:
deb https://nginx.org/packages/mainline/debian/ <CODENAME> nginx
deb-src https://nginx.org/packages/mainline/debian/ <CODENAME> nginx
Replace <CODENAME>
with the codename for your distribution of Debian.
Save the file and exit.
Note: Nginx developers maintain different directories for different Linux distributions. This guide suggests installing the mainline supported release. As with most software, there are more recent but untested packages. See the documentation for the specific package for your distribution.
Install Latest Release of Nginx
To install the latest release of Nginx, use the commands:
sudo apt-get remove nginx-common
sudo apt-get update
sudo apt-get install nginx
Step 3: Start Nginx and Configure to Launch on Reboot
To start Nginx:
sudo systemctl start nginx
To enable Nginx:
sudo systemctl enable nginx
To check Nginx is running:
sudo systemctl status nginx
The output should show you the service is active (running), as in the image below:
Step 4: Unlink Default Configuration File
In the terminal, enter the following:
sudo unlink /etc/nginx/sites-enabled/default
Step 5: Create New Configuration File
To create a new configuration file, enter:
cd /etc/nginx/sites-available/
sudo vi custom_server.conf
Replace custom_server
with a name that’s meaningful to you. In the new file, enter:
server {
listen 80;
location / {
proxy_pass http://my_server;
}
}
This is a very basic Nginx reverse proxy example. Nginx is set to listen for all traffic on port 80 for all traffic.
The proxy_pass
command directs all traffic on port 80 to http://my_server
. Just change http://my_server
to the location of your choice, and Nginx will intercept client requests and route them to the location you specify. Once you’ve finished, save the file and exit.
Step 6: Link and Activate Configuration File
To activate the new Nginx file, enter:
ln -s /etc/nginx/sites-available/custom_server.conf
/etc/nginx/sites-enabled/custom_server.conf
As usual, replace custom_server
with the name of the configuration file you created in Step 5.
Step 7: Test and Restart Nginx
To test Nginx:
sudo service nginx configtest
To restart Nginx:
sudo service nginx restart
Optional Nginx Configuration Options
Proxy Buffers
By default, Nginx buffers traffic for servers that it proxies for. Buffers improve server performance as a server response isn’t sent until the client finishes sending a complete response.
To turn the buffer off, open the configuration file from Step 5. Under the location/section, add the following:
proxy_buffering off;
Request Headers
Headers provide the server information about the requests made, or about the client.
Nginx redefines two of the header fields: host
is configured for $proxy_host
, and connection
is configured for close
. If you use those headers, be sure to change the behavior in the configuration file.
If any header strings are empty, Nginx simply eliminates those fields.
To change the way Nginx handles heathers, use the following commands in your configuration file:
location / {
proxy_set_header Host $host;
}
This example tells Nginx to set host
to the $host
variable.
To prevent a header field from being passed to the proxied server, use an empty string as follows:
location / {
proxy_set_header header-variable "";
}
Load Balancing
You can use the configuration file to route traffic to several servers. To use this configuration, your configuration file will look similar to this example:
http {
server {
proxy_pass http://my_server
}
}
In other words, the HTTP configuration goes outside the server configuration from Step 5.
To create a name for a group of servers, Use the upstream
command:
http {
upstream server_group {
server my.server1.com weight=3;
server my.server2.com;
}
server {
location / {
proxy_pass http://server_group;
}
}
}
This designation takes two servers – my.server1.com
and my.server2.com
– and bundles them together. Nginx proxies that group of servers under the name http://server_group
. You can rename them anything you’d like.
This example uses the weight
command to route three requests to my.server1.com
, then 1 request to my.server2.com
. This is one option to manually balance client load between servers. Another method is to simply omit any designation, in which Nginx will round-robin the requests evenly among the listed servers.
Conclusion
Now you know how to set up an Nginx reverse proxy.
It’s an excellent tool for a multiple-server environment, creating a unified client experience. It can also be useful for simpler tasks like keeping a single server anonymous.
Additionally, you can also use our Knowledge Base to learn how to deploy NGINX reverse proxy on Docker.
How to Set up & Use NGINX as a Reverse Proxy
Reverse proxies are a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing as if they originated from the proxy server itself. NGINX is a popular reverse proxy server that can be used to set up a reverse proxy for your web applications.
Step 1: Install NGINX
The first step is to install NGINX on your server. This can be done using your package manager, such as apt-get or yum. For example, on Ubuntu you can use the following command:
sudo apt-get install nginx
Once the installation is complete, you can start the NGINX service using the following command:
sudo service nginx start
Step 2: Configure NGINX
Once NGINX is installed, you need to configure it to act as a reverse proxy. This can be done by editing the NGINX configuration file, which is located at /etc/nginx/nginx.conf. In this file, you need to add the following lines:
server { listen 80; server_name example.com; location / { proxy_pass http://localhost:8080; } }
This configuration tells NGINX to listen on port 80 and to forward all requests to the localhost on port 8080. You can also add additional configuration options, such as SSL support or caching.
Step 3: Test the Configuration
Once the configuration is complete, you can test it by running the following command:
sudo nginx -t
This will check the configuration for any errors. If there are no errors, you can restart the NGINX service using the following command:
sudo service nginx restart
Once the service is restarted, you can test the reverse proxy by accessing your web application through the NGINX server.
Conclusion
Setting up and using NGINX as a reverse proxy is a simple process that can be completed in a few steps. Once the configuration is complete, you can access your web applications through the NGINX server, allowing you to take advantage of the features that NGINX provides.