Hello, and welcome to the article on installing and configuring NGINX as a reverse proxy.
This is a whitepaper, so we will not discuss the design and reason for using a reverse proxy. Instead, we’ll go straight to configuration and configure an NGINX reverse proxy.
First of all, we need to install the CentOS or Ubuntu servers, the latest releases are fine, and on top of that, we will install an NGINX reverse proxy.
Configure NGINX Reverse Proxy in CentOS
In my case, I am using CentOS 7.
So, after logging in, we need to run the following commands to enable the repository and install NGINX:
# yum install epel-release
This command adds the repositories where NGINX packages are located.
After the command completes, we should be greeted with the output “ Complete!” .
The next command we need to execute:
# yum install nginx
This command actually installs the NGINX package and includes it.
Our next steps are to enable the NGINX service, start the service, and add some firewall rules.
We need to execute the commands in the following order:
# systemctl enable nginx
# systemctl start nginx
# firewall-cmd –permanent –zone = public –add-service = http
# firewall-cmd –permanent –zone = public –add-service = https
# firewall-cmd –reload
The first two lines enable the service and start it. The following lines add HTTP and HTTPS exceptions to the firewall and reload the firewall configuration to apply the changes.
Now we can check if everything is working fine by going to the IP address or hostname of the server to see if we get the NGINX popup page.
It worked! Then let’s set it up as a reverse proxy.
To do this, we need to change the default configuration file. To do this, we will need to run this command:
# vi /etc/nginx/nginx.conf
Now that the file is open for editing, we press the INSERT key and look for the following configuration block:
As you can see in my case, by default it listens on port 80. Now, to set up a reverse proxy in the location block, we simply add the following line in curly braces:
proxy_pass http : // applications server;
Where applicationserver is the IP address of the web server to which you want to forward packets.
It is so simple!
Another thing to look out for is the location block. As you can see, there is one forward slash. This means that all requests sent to the NGINX server will be redirected to the internal host.
We can specify multiple locations, each of which will be directed to a different host, but we’ll talk about that in the next article.
Thanks for your time and I hope you enjoyed reading the article!