How to configure Nginx on Ubuntu 22.04 is a common question for developers and system administrators looking to set up a high-performance web server. Nginx is an open-source, lightweight, and scalable web server that is widely used for hosting websites, acting as a reverse proxy, and handling large amounts of traffic efficiently.

Overview

Nginx is a powerful, open-source web server widely used for hosting websites, acting as a reverse proxy, and handling high-concurrency traffic. Its lightweight design and performance optimizations make it a preferred choice for developers and system administrators.

In this tutorial, we will cover Nginx configuration on Ubuntu 22.04, including how to set up virtual hosts and optimize Nginx for better performance. Whether you’re hosting multiple websites or looking to improve server speed, this guide will help you get the most out of your Nginx installation.

If you need a reliable Ubuntu VPS, check out VPSWindows.com. They offer multi-national VPS solutions, full versions of Linux operating systems (Ubuntu, CentOS, Debian), and Windows VPS, making it easier to set up and manage your Nginx server.

Installing Nginx

n our previous article, we provided a detailed step-by-step guide on how to install Nginx on Ubuntu 22.04. If you haven’t installed it yet, you can do so by running the following commands:

sudo apt update
sudo apt install nginx

To check if Nginx is accessible, open a web browser and enter your server’s IP address. If you see this page, you have successfully installed Nginx on your web server.

welcome-to-nginx

Configure Nginx on Ubuntu 22.04

2. Creating a Custom Website Directory

Before configuring Nginx, it’s best practice to create a separate directory for your website rather than using the default /var/www/html/📋Copied! location.

Step 1: Create a New Directory

Run the following commands to create a dedicated folder for your website files:

cd /var/www
sudo mkdir tutorial
cd tutorial
📋Copied!

Step 2: Add a Sample Web Page

Create an index.html📋Copied! file inside this directory:

sudo "${EDITOR:-vi}" index.html
📋Copied!

Paste the following content into the file:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello, Nginx!</title>
</head>
<body>
<h1>Hello, Nginx!</h1>
<p>We have just configured our Nginx web server on Ubuntu!</p>
</body>
</html>
📋Copied!

Save and close the file. This basic webpage will be displayed once Nginx is configured correctly.

3. Setting Up a Virtual Host in Nginx

A virtual host (or server block) allows multiple websites to run on the same server, making it a useful feature for shared hosting or multi-project environments.

Step 1: Create a New Virtual Host Configuration File

Navigate to the Nginx configuration directory and create a new configuration file:

cd /etc/nginx/sites-available
sudo "${EDITOR:-vi}" tutorial
📋Copied!

Paste the following Nginx configuration:

server {
listen 81;
listen [::]:81;📋Copied!
📋Copied!
server_name example.ubuntu.com;📋Copied!📋Copied!root /var/www/tutorial;
index index.html;📋Copied!📋Copied!location / {
try_files $uri $uri/ =404;
}
}

Step 2: Enable the Virtual Host

To activate this configuration, create a symbolic link in the sites-enabled📋Copied! directory:

cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/tutorial tutorial
📋Copied!

Step 3: Restart Nginx to Apply Changes

After making the changes, restart Nginx:

sudo systemctl restart nginx
📋Copied!

Now, open a browser and visit your server’s IP address with port 81:

http://your-server-ip:81📋Copied!
Configure-Nginx-on-Ubuntu-22

If everything is configured correctly, you should see the “Hello, Nginx!” page. We have just configured Nginx web server.

Optimizing Nginx Configuration for Performance

Once your virtual host is set up, optimizing Nginx can significantly improve speed and resource efficiency.

Enable Gzip Compression

Gzip reduces file sizes, speeding up website load times. To enable it, edit /etc/nginx/nginx.conf📋Copied! and add:

gzip on;
gzip_types text/plain text/css application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 5;
📋Copied!

Restart Nginx:

sudo systemctl restart nginx
📋Copied!

Increase Worker Connections

By default, Nginx has a limit on simultaneous connections. To improve performance, modify /etc/nginx/nginx.conf📋Copied!:

worker_processes auto;
events {
worker_connections 4096;
multi_accept on;
}
📋Copied!

Restart Nginx again:

sudo systemctl restart nginx
📋Copied!

Enable Caching for Static Files

To improve load speeds, configure caching for static assets inside your server block:

location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|mp4|webm|ogg|ogv|json)$ {
expires 7d;
add_header Cache-Control "public, no-transform";
}
📋Copied!

This allows browsers to cache images, stylesheets, and scripts for seven days, reducing repeated downloads and improving page load times.

Conclusion

In this tutorial, we have covered configuring Nginx on Ubuntu 22.04 with a focus on setting up virtual hosts and optimizing performance. By creating a dedicated website directory, configuring Nginx server blocks, and enabling essential optimizations like Gzip compression, caching, and worker connections, your web server will be more efficient and faster.

If you are looking for a reliable Ubuntu USA VPS to deploy your Nginx web server, check out VPSWindows.com. They offer multi-national VPS solutions with full-featured Linux and Windows operating systems, ensuring a smooth hosting experience.

Thank you for reading the article. Follow us for more tutorials on server optimization, web hosting, and Linux administration!