Blogs

Nginx: What It Is, Its Benefits, and How You Use It! 🚀✨


What is Nginx? 🚀

Nginx (pronounced "engine-x") is a powerful web server known for its speed and ability to handle many users at once. It's great for serving static content, like images and stylesheets, and it can help manage traffic to your website by distributing requests across multiple servers.

One of Nginx's standout features is its ability to host multiple websites on a single server. This is done through a feature called "server blocks" (or virtual hosts), which lets you configure different sites to respond to different domain names. So, whether you're running a personal blog, an e-commerce site, or a large-scale platform, Nginx can efficiently manage all of them without needing separate servers.


Many developers love Nginx's efficiency and scalability, making it a top choice for busy websites and applications. With Nginx, you can keep things running smoothly, no matter how many sites you host!


Hosting a site on a server with Nginx 🌟

To host a site on a server with Nginx, here's what we need to do:

  1. Install Nginx on the server.
  2. Deploy our site files on the server.
  3. Create a configs file in the sites-available folder.
  4. Create a symlink of the site to the sites-enabled folder.
  5. Reload the nginx service.
sites-available

This directory contains configuration files for all server blocks (virtual hosts) that you can set up in Nginx. It holds the complete list of potential sites that can be configured on the server, allowing you to create, edit, or archive configurations without affecting the live setup.

sites-enabled

This directory contains symbolic links to the configuration files you want Nginx to actively use in sites-available. Only the configurations listed here are loaded by Nginx when it starts. This setup allows you to easily enable or disable sites by managing the symlinks, making organizing and controlling your server's configurations straightforward.


Detailed steps of how to serve a website using Nginx 📚

  • Install Nginx on your server.
    1. sudo apt install nginx
    2. sudo systemctl start nginx
    3. sudo systemctl enable nginx
  • Deploy your website code on the server.
    1. Create a folder with your site name under /var/www; it should look like /var/www/{your-domain}
    2. Copy your site code to the folder created in the earlier step.
  • Create a Nginx config file.
    1. Create a Nginx config file with your site name under /etc/nginx/sites-available; it should look like /etc/nginx/sites-available/{your-domain}
    2. Write your site's configs in the file created in the step earlier; here's an example of what it should look like:
      server {
      listen 80;
      server_name _;

      root /var/www/{your-domain.com};
      index index.html;

      location / {
      try_files $uri $uri/ =404;
      }
      }
  • Create a symlink of that config file to let Nginx know that your site should be available
    sudo ln -s /etc/nginx/sites-available/{your-domain.com} /etc/nginx/sites-enabled/
  • Reload the nginx service
    sudo systemctl reload nginx

Summary ✅

So, in this blog, we quickly looked at Nginx and how to set it up on our server.
That's it for now; you find it helpful!