Nginx Reverse Proxy Ubuntu 18.04
In this post, we will show you how to install Nginx Web Server and configure it as a reverse proxy on Ubuntu Server 18.04
Join the DZone community and get the full member experience.
Join For FreeIn this post, we will install an Nginx Reverse Proxy on Ubuntu 18.04. This will allow you to proxy requests to several web servers or apps.
If you have a domain that points to your single public IP address and you want to server more than one website, you usually have to use port forwarding to translate some other port on the outside interface of your router to port 80 on the inside interface of your router that went to one of your webs servers. Here is a diagram that makes it a little clearer.
So to get to Website 1 from the outside you would browse to http://example.com:8080.
And to get to Website 2 from the outside you would browse to http://example.com:8181.
Not the best option but it worked.
A much better option is to use a Reverse Proxy. A reverse proxy can look at the URL in the HTTP header and decide where to route the web traffic from there.
So you could have a URL for website 1 like mysiteisawesome.com and the reverse proxy would recognize that it needs to route the requests to the website 1 server. And for requests to myawesomeapp.com the reverse proxy would route the requests to website 2. All the while using a single public IP address. You still have to create a port forwarding rule to route all traffic on port 80 to your reverse proxy server.
In this post, I will show you how to install Nginx Web Server and configure it as a reverse proxy on Ubuntu Server 18.04
Installing Nginx
First, make sure that you have a server with a fresh installation of Ubuntu 18.04.
Next, make sure that you install all the updates and reboot to start using any new kernels.
apt update && upgrade -y
reboot
Actually installing Nginx Web Server is easy.
apt install nginx -y
Once the installation is complete, we can start and enable the service to start on each boot.
systemctl start nginx.service
systemctl enable nginx.service
Now we are ready to start configuring our reverse proxy.
Configuring an Nginx Reverse Proxy
With Nginx installed and running we need to configure our reverse proxy settings.
Open up /etc/nginx/sites-available/default in your favorite editor.
First, we will add our upstream servers. These are the web servers that are behind you proxy that will server your websites.
Using our example from above:
upstream website1 {
server http://192.168.1.30;
}
upstream website2 {
server http://192.168.1.40;
}
Here I am defining two upstream servers, one for each website.
You don't have to use IP addresses here if you have local DNS configured.
For example:
upstream website1 {
server http://webstite1.example.home;
}
upstream website2 {
server http://website2.example.home;
}
Now we need to change the server definition so that Nginx will proxy our requests.
server {
listen 80;
server_name myawesomewebsite.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://website1;
}
}
Here we state the external domain name that Nginx will match in the HTTP header using the server_name
parameter.
For our example, we will use myawesomewebsite.com.
Next, we configure some options for using a reverse proxy with the proxy_set_*
parameters.
The really important part is in the location section.
Here we set the proxy_pass
parameter to point to our upstream URL of http://website1.
Notice we only put what matches the upstream which is not the FQDN of the internal server.
Continuing on, we add another server section for our other website.
server {
listen 80;
server_name myawesomeapp.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://website2;
}
}
Save the file and exit.
Check your Nginx configuration with this command:
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
We are good to restart the Nginx web server.
systemctl restart nginx.service
You now have a completed Nginx reverse proxy on Ubuntu 18.04.
Conclusion
I hope you enjoyed this article.
If you did please consider sharing on social media and commenting below.
Need More Ubuntu Training?
Checkout this Ubuntu Fundamentals course from Pluralsight. Pluralsight has great training at a great price. Free 10-day trial.
Published at DZone with permission of Bill Ward, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments