FedoraRHEL Based

How To Configure Nginx Server Blocks on Fedora 40

Configure Nginx Server Blocks on Fedora 40

In this tutorial, we will show you how to configure Nginx server blocks on Fedora 40. Nginx is a powerful, high-performance web server that has gained immense popularity among developers and system administrators. Known for its stability, speed, and low resource consumption, Nginx is an excellent choice for serving web content, acting as a reverse proxy, and handling load balancing.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step configuration of Nginx server blocks on Fedora 40.

Prerequisites

Before we dive into the installation process, ensure that you have the following prerequisites in place:

  • A server running one of the following operating systems: Fedora 40.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • You will need access to the terminal to execute commands. Fedora provides the Terminal application for this purpose. It can be found in your Applications menu.
  • A stable internet connection to download the necessary packages.
  • Familiarity with a text editor (e.g., nano, vim)
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Configure Nginx Server Blocks on Fedora 40

Step 1. Update Your System.

To ensure a smooth installation process and maintain the security of your Fedora 40 system, it is crucial to update all the installed packages to their latest versions. This step helps prevent potential compatibility issues and vulnerabilities. To update your system, open the terminal and run the following command:

sudo dnf clean all
sudo dnf update

Step 2. Installing Nginx on Fedora 40

The first step is to install Nginx on your Fedora 40 system. Nginx is available in the default Fedora repositories, making the installation process straightforward:

sudo dnf install nginx

Verify the installation by checking the Nginx version:

nginx -v

Start and enable the Nginx service:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3. Configure Nginx Server Blocks.

Nginx server blocks, also known as virtual hosts, allow you to host multiple websites on a single server. Each server block is a self-contained configuration that defines the settings for a specific website, such as the document root, server name, and location directives.

Server blocks offer several benefits, including:

    • Isolation of website configurations
    • Efficient resource utilization
    • Flexibility in managing multiple domains

By default, Nginx has a single server block configured, which serves as the default catch-all block for incoming requests.

Preparing the Directory Structure

To organize your website files effectively, create a directory structure that separates each website’s content.

sudo mkdir -p /var/www/example.com/html

Set the appropriate ownership and permissions:

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Create a sample index.html file:

nano /var/www/example.com/html/index.html

Add the following content:

<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>

Save and close the file.

Configuring the First Server Block

Now, let’s create and configure the server block for your first website.

sudo nano /etc/nginx/conf.d/example.com.conf

Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Test the Nginx configuration for syntax errors:

sudo nginx -t

If the configuration is valid, you should see a message indicating that the syntax is okay and the test is successful.

Setting Up Multiple Server Blocks

To host additional websites, you can create separate server block files for each domain.

sudo mkdir -p /var/www/test.com/html

Set the appropriate ownership and permissions:

sudo chown -R $USER:$USER /var/www/test.com/html
sudo chmod -R 755 /var/www/test.com

Create a sample index.html file for the second website:

nano /var/www/test.com/html/index.html

Add the following content:

<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body>
<h1>Success! The test.com server block is working!</h1>
</body>
</html>

Save and close the file, then create a new server block file for the second website:

sudo nano /etc/nginx/conf.d/test.com.conf

Add the following configuration:

server {
    listen 80;
    server_name test.com www.test.com;
    root /var/www/test.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Save the file then, Test the Nginx configuration and restart the service:

sudo nginx -t
sudo systemctl restart nginx

You can now access both websites using their respective domain names.

Step 4. Optimizing Nginx Performance.

To ensure optimal performance of your Nginx server, consider the following optimizations:

  • Enable Gzip compression to reduce the size of transferred data:
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  • Configure browser caching to leverage client-side caching:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 7d;
add_header Cache-Control "public";
}
  • Adjust worker processes and connections based on your server’s resources:
worker_processes auto;
worker_connections 1024;

Congratulations! You have successfully set up Nginx server blocks. Thanks for using this tutorial to configure Nginx server blocks on your Fedora 40 system. For additional or useful information, we recommend you check the official Nginx website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button