Nginx: What It Is, Its Benefits, and How You Use It! 🚀✨
Get ready to explore Nginx and enhance your web projects! 🌐✨
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:
- Install Nginx on the server.
- Deploy our site files on the server.
-
Create a configs file in the
sites-available
folder. -
Create a symlink of the site to the
sites-enabled
folder. - Reload the nginx service.
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.
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.
-
sudo apt install nginx
-
sudo systemctl start nginx
-
sudo systemctl enable nginx
-
-
Deploy your website code on the server.
-
Create a folder with your site name under
/var/www
; it should look like/var/www/{your-domain}
- Copy your site code to the folder created in the earlier step.
-
Create a folder with your site name under
-
Create a Nginx config file.
-
Create a Nginx config file with your site name under
/etc/nginx/sites-available
; it should look like/etc/nginx/sites-available/{your-domain}
-
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 Nginx config file with your site name under
-
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!