Setup Nginx HTTPS Reverse Proxy on Ubuntu 20.04 / 22.04

Setup Nginx HTTPS Reverse Proxy on Ubuntu 20.04 / 22.04. Firstly, Nginx is a popular open source web server used to serve static and dynamic web content.

One of its key features is its ability to act as a reverse proxy, which allows it to forward client requests to other servers and return the server responses to the clients. Nginx is configured to support HTTPS, which is a secure protocol for transmitting data over the internet.

Overall, Nginx is a powerful tool for improving the performance, scalability, and security of web applications. Whether you are building a new application or looking to optimize an existing one, Nginx’s reverse proxy and web server features help you to deliver a faster, more secure, and more reliable user experience.

In addition to its reverse proxy capabilities, Nginx offers a wide range of features that make it a popular choice for web servers, including support for HTTP/2, virtual hosts, and dynamic content generation through server side scripting.

In this article, you learn about Nginx and how it takes the place of a reverse proxy, and the advantages of this configuration. You will also learn how to install and set it up as an HTTPS reverse proxy on Ubuntu 20.04 or 22.04.

What is Reverse Proxy

A reverse proxy is a server that sits between client devices and a web server, forwarding requests to the appropriate server and returning the server’s response to the client. It acts as an intermediary between the client and the server and provides a number of benefits for web applications.

One of the main advantages of using a reverse proxy is that it improves the performance of web applications by load balancing traffic across multiple backend servers. This helps to distribute the workload and prevent any single server from becoming overwhelmed by traffic.

Besides, reverse proxies also provide additional security features like SSL termination, which offloads the task of SSL encryption and decryption from the backend servers. This helps to reduce the load on the servers and improves their performance.

Nginx as a Reverse Proxy

Nginx is a popular choice for a reverse proxy because of its high performance and scalability, as well as its support for advanced features like SSL termination and caching.

This means Nginx reverse proxy offloads resource intensive tasks like SSL encryption and decryption from the backend servers, allowing them to focus on serving content and processing requests more efficiently.

Additionally, Nginx also caches frequently accessed content, reducing the load on backend servers and improving the overall performance of the application.

Moreover, Nginx is designed to handle a large number of simultaneous connections and efficiently distributes traffic across multiple backend servers. This helps to improve the performance and reliability of web applications.

By setting up an Nginx HTTPS reverse proxy on your Ubuntu 20.04 server, you take advantage of these benefits and improve the performance, scalability, and security of your web application.

Advantages of Nginx HTTPS Reverse Proxy 

Configuring an Nginx HTTPS reverse proxy on Ubuntu 20.04 offers a number of advantages for web applications. Some of the key advantages include:

  • Improved performance: Nginx is designed to handle a large number of simultaneous connections. It efficiently distributes traffic across multiple backend servers, improving the performance and reliability of web applications.
  • Increased security: Nginx provide additional security features like SSL termination. Which offloads the task of SSL encryption and decryption from the backend servers. This helps to reduce the load on the servers and improve their security.
  • Load balancing: Nginx is configured to distribute traffic across multiple backend servers, preventing any single server from becoming overwhelmed by traffic.
  • Caching: Nginx can cache frequently accessed content, reducing the load on backend servers and improving the overall performance of the application.
  • Scalability: Nginx is easily scaled horizontally by adding additional servers to the backend pool. It allows applications to handle a larger volume of traffic.
  • Flexibility: Nginx is highly configurable and is used to support a wide range of web applications and use cases.

Overall, configuring an Nginx HTTPS reverse proxy helps to improve the performance, security, and scalability of web applications. It make it a valuable tool for developers and system administrators.

Follow this post to learn how to set up Nginx HTTP reverse proxy on Ubuntu 20.04/ 22.04.

How to Setup Nginx HTTPS Reverse Proxy on Ubuntu 20.04 / 22.04

Prerequisites and Specifications

Before proceeding with setting up an Nginx HTTPS reverse proxy on Ubuntu 20.04/22.04, you need to ensure that the following prerequisites are met:

    • Ubuntu server: You will need a server running Ubuntu 20.04 or 22.04 with a non-root user with sudo privileges.
    • You will need the address of at least one backend server. Or application server to proxy traffic to the servers can be running any web server software, such as Apache or Nginx.
    • A domain name pointing to the public IP of your computer. Your web server acts as a proxy using this setup with Nginx.

    Install Nginx

    To set up an Nginx HTTPS reverse proxy on Ubuntu, you need to have Nginx installed on your server.  Install Nginx using the following commands:

    				
    					sudo apt-get update
    sudo apt install nginx
    				
    			

    Once Nginx is installed, please check its status using the following command:

    				
    					sudo systemctl status nginx
    				
    			

    If Nginx is running, you should see a message indicating active service.

    Configure Firewall to Allow Nginx

    After installing Nginx, you need to configure the firewall to allow HTTP and HTTPS traffic to your server. Run the following command.

    				
    					sudo ufw allow 'Nginx HTTP'
    				
    			

    Configure Your Server or Site

    Under /etc/nginx/sites-available, make a new configuration file.

    				
    					sudo nano /etc/nginx/sites-available/reverse_proxy
    				
    			

    And fill it with the following code:

    				
    					server {
    
        listen 80;
        root /var/www/example.com/html;
        index index.php index.html index.htm;
        server_name example.com;
    
        location / {
            try_files $uri $uri/ /index.php;
        }
    
        location ~ \.php$ {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
        }
        
        location ~* \.(js|css|jpg|jpeg|png|svg|html|htm)$ {
            expires      30d;
        }
        
        location ~ /\.ht {
            deny all;
        }
    }
    				
    			

    Next, enable the configuration file for your site or server.  You need to create a symbolic link from the configuration file to the sites-enabled directory that Nginx reads at startup. 

    				
    					sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
    				
    			

    Install and Configure Apache 

    Install Apache using the following command.

    				
    					sudo apt-get install apache2
    				
    			

    Nginx will be running on port 80. You have to configure Apache to listen on a different port, such as 8080, and configure Nginx to proxy requests to that port. Open the port.conf file. 

    				
    					sudo nano /etc/apache2/ports.conf
    				
    			

    And add the following line to the file. 

    				
    					Listen 127.0.0.1:8080
    				
    			

    Also change VirtualNameHost.

    				
    					NameVirtualHost 127.0.0.1:8080
    				
    			

    Configure Your Virtual Host

    Run the following command to create a configuration file.

    				
    					sudo nano /etc/apache2/sites-available/example.com.conf
    				
    			

    Add the following code to configure virtual hosts to listen on port 8080.

    				
    					
    
        ServerName example.com
        ServerAdmin webmaster@example.com
        DocumentRoot /var/www/example.com/html
    
    
    				
    			

    Next, Enable our virtual host using the following commands.

    				
    					cd /etc/apache2/sites-available/
    sudo a2ensite example.com.conf
    				
    			

    Create a PHP Index File 

    Run the following command to create an index php file.

    				
    					sudo mkdir -p /var/www/example.com/html
    sudo chown $USER:www-data -R /var/www/example.com
    sudo chmod 775 -R /var/www/example.com
    echo '<h1>Hello World</h1>' &gt; /var/www/example.com/html/index.php
    				
    			

    Restart Apache and Nginx Services

    You are done with the setup. Now, restart the Apache and Nginx services to apply all changes.

    				
    					sudo service apache2 restart
    sudo service nginx restart
    				
    			

    Verify Configuration of Nginx Reverse Proxy

    You can run the following command to see open and listening ports to see if the Nginx and Apache servers are configured.

    				
    					sudo netstat -pltn
    				
    			

    Verify Nginx reverse setup by opening your server IP address in the browser.

    That’s it for installing and configuring Nginx HTTP reverse proxy on Ubuntu 20.04/ 22.04. We shall conclude this article now. 

    Setup Nginx HTTPS Reverse Proxy on Ubuntu 20.04 / 22.04 Conclusion

    Overall, Nginx is a valuable tool for developers and system administrators who are looking to improve the performance, scalability, and security of their web applications.

    Following the steps outlined in this article, you can easily configure an Nginx HTTPS reverse proxy on your Ubuntu server and take advantage of these benefits.

    You can leverage Nginx’s reverse proxy, and web server features to help you deliver a faster, more secure, and more reliable user experience.

    Moreover, its advanced features take your applications to the next level and provide a better user experience for your customers.

    Explore our Nginx section to learn more.

    Avatar for Sobia Arshad
    Sobia Arshad

    Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.

    0 0 votes
    Article Rating
    Subscribe
    Notify of
    0 Comments
    Most Voted
    Newest Oldest
    Inline Feedbacks
    View all comments
    0
    Would love your thoughts, please comment.x
    ()
    x