Navigating Nginx: A Beginner's Guide to Finding and Creating a Basic Web Server
Embark on Your Web Hosting Journey with Nginx: Learn How to Set Up Your First Server Configuration
What is NGINX?
Nginx (pronounced "engine-x") is a popular open-source web server, load balancer and reverse proxy server software. It is designed to handle high-traffic websites efficiently, providing excellent performance, scalability, and reliability. Created by Igor Sysoev in 2004, Nginx has gained widespread adoption due to its lightweight, event-driven architecture, making it an ideal choice for modern web applications and content delivery.
What makes it so fast?
Nginx uses an event-driven, asynchronous architecture. It employs a single master process that manages multiple worker processes. Each worker can handle thousands of connections simultaneously, making Nginx highly efficient and capable of handling a large number of concurrent connections with minimal resource usage.
Learn it on Linux
For a hands-on installation guide on virtualizing Ubuntu Server using VirtualBox, check out this article: "Virtualize Your Ubuntu Server Experience: A Hands-On Installation Guide" by Me. You can find it at ritwikmath.hashnode.dev/virtualize-your-ubu...
Why should you learn Nginx on Linux?
Learning NGINX in Linux offers several compelling points to consider
Nginx was originally developed for Linux, and it is primarily designed to run on Unix-based systems. As a result, most of the online resources, tutorials, and documentation focus on Nginx's implementation on Linux.
In the real world, Nginx is commonly used on Linux servers to power websites, web applications, and other online services.
Learning Nginx on Linux can be a gateway to understanding server administration on Unix-based systems.
Linux offers a powerful and versatile command-line interface, which is the preferred environment for configuring and managing Nginx.
Nginx typically performs better on Linux due to its event-driven, non-blocking architecture, which is optimized for Unix-based systems.
Linux has robust security features and a well-established permission system that ensures better security for your Nginx configurations and files.
Learning Nginx on Linux exposes you to the world of server automation and scripting using tools like Bash, Python, and other scripting languages.
Important files and directories
/etc/nginx
The NGINX server's default configuration root is located at /etc/nginx/
. In this directory, you can find essential configuration files that guide NGINX on its behavior and settings.
/etc/nginx/nginx.conf
nginx.conf is the main configuration file for Nginx. It is located in the default configuration root of Nginx, which is typically /etc/nginx/
. The nginx.conf file contains global settings, worker processes, event handling, and references to other configuration files, including server block configurations.
/etc/nginx/conf.d/
The /conf.d/
directory is a common location for additional configuration files in Nginx. It is typically found within the main configuration root of Nginx, which is often located at /etc/nginx/
.
The purpose of the /conf.d/
directory is to organize Nginx configuration files that correspond to specific components or functionalities of the web server. Each file in this directory usually represents a separate server block, which defines how Nginx handles requests for a particular website or application.
/etc/nginx/sites-enabled
The directory "/etc/nginx/sites-enabled" is commonly found on systems using the Nginx web server. In Nginx, the configuration files for individual websites or web applications are usually stored in the "/etc/nginx/sites-available" directory. These configuration files define how Nginx should handle incoming requests for specific domains or subdomains.
The configuration files (usually ending with the .conf extension) present in the "sites-enabled" directory are symbolic links that point to the corresponding configuration files in the "sites-available" directory. This setup allows Nginx to selectively enable or disable specific websites or applications by simply managing the symbolic links in the "sites-enabled" folder. Any changes made to the configuration files in either directory affect the same underlying file since they are linked together.
/etc/nginx/mime.types
mime.types is a significant configuration file in Nginx that maps file extensions to their corresponding MIME types. It is used by the web server to identify the type of content being served and send the appropriate HTTP Content-Type
header in the response.
Useful Nginx commands
# Display nginx help menu
nginx -h
# Display nginx version only
nginx -v
# The following command provides information about the NGINX version,
# build details, and configuration arguments,
# which reveal the modules built into the NGINX binary:
nginx -V
# Tests the NGINX configuration
nginx -t
# Tests the NGINX configuration and
# prints the validated configuration to the screen
nginx -T
Configure your first NGINX server
Create/Clone a static website
First, create a static website in /var/www
directory. You can also clone my repository https://github.com/ritwikmathlearner/camptrue.git.
git clone https://github.com/ritwikmathlearner/camptrue.git
Rest of the article will be written considering this website
Create a configuration file
Create a file name my-site.conf (except .conf extension you can use any name) in /etc/nginx/sites-available
directory. Edit the file to write server block configuration.
server {
listen 80 default_server;
server_name www.my-site.com;
location / {
root /var/www/camptrue/public;
index index.html index.htm;
}
}
Now delete the default configuration file in the sites-enabled directory.
sudo rm -rf /etc/nginx/sites-enabled/default
Create a symbolic link named "my-site.conf" in the "sites-enabled" directory, pointing to the original configuration file located in the "sites-available" directory
sudo ln -s /etc/nginx/sites-available/my-site.conf /etc/nginx/sites-enabled/
Now test configuration
sudo nginx -t
Reload Nginx to reflect the changes in configuration
sudo systemctl reload nginx.service
Open the IP Address in the browser to check if the Camptrue website is visible or not. You can also create other HTML files in /var/www/camptrue/public
directory to test further.
Do not use a relative path to create a symbolic link. The issue you will face: https://stackoverflow.com/questions/12715871/nginx-not-picking-up-site-in-sites-enabled
Conclusion
In conclusion, the article has achieved the following objectives in guiding you through the process of configuring their first NGINX server. You have gained a clear understanding of how to install NGINX on their server and manage the NGINX service using system management commands. The article has explained the creation of server block configuration files in the "sites-available" directory and demonstrated how to enable websites by creating symbolic links in the "sites-enabled" directory. You have learned how to test the NGINX configuration for syntax errors using "nginx -t" and have been equipped with troubleshooting techniques to address common configuration issues. The article has successfully guided you on how to serve web content by specifying the appropriate "root" and "index" directives within the server block.