DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Ansible and the Pre-Container Arts
  • regreSSHion: Should We Panic About the New OpenSSH Vulnerability?
  • Getting Started With OCR docTR on Ubuntu
  • Linux Mint Debian Edition Makes Me Believe It’s Finally the Year of the Linux Desktop

Trending

  • Agile and Quality Engineering: A Holistic Perspective
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  1. DZone
  2. Coding
  3. Frameworks
  4. Nginx Reverse Proxy Ubuntu 18.04

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

By 
Bill Ward user avatar
Bill Ward
·
Oct. 23, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
37.6K Views

Join the DZone community and get the full member experience.

Join For Free

In 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.

ubuntu

Published at DZone with permission of Bill Ward, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Ansible and the Pre-Container Arts
  • regreSSHion: Should We Panic About the New OpenSSH Vulnerability?
  • Getting Started With OCR docTR on Ubuntu
  • Linux Mint Debian Edition Makes Me Believe It’s Finally the Year of the Linux Desktop

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!