Welcome to your first foray into Nginx! In this section, we'll build a foundational Nginx configuration that allows you to serve a simple static website. Think of this as the digital equivalent of setting up your storefront. We'll start with the absolute essentials and gradually introduce key concepts.
Every Nginx installation comes with a default configuration file, often located at /etc/nginx/nginx.conf. However, for managing individual sites, it's best practice to create separate configuration files within a dedicated directory, typically /etc/nginx/sites-available/ and then symlink them to /etc/nginx/sites-enabled/. This keeps things organized and makes it easy to enable or disable sites.
Let's create our first configuration file. We'll name it mysite.conf.
sudo nano /etc/nginx/sites-available/mysite.confNow, paste the following basic configuration into this file. We'll break down each part.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/mysite;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}Let's dissect this configuration block by block:
server { ... }: This is the fundamental block in Nginx. It defines a virtual server and is responsible for handling requests for a specific domain or IP address.
listen 80;: This directive tells Nginx to listen for incoming connections on port 80, which is the standard port for HTTP traffic. If you were setting up HTTPS, you'd also seelisten 443 ssl;here.
server_name example.com www.example.com;: This directive specifies the domain names that thisserverblock should respond to. When a client requestsexample.comorwww.example.com, Nginx will use this configuration to handle the request. You should replaceexample.comwith your actual domain name.
root /var/www/mysite;: Therootdirective defines the document root for requests. This is the directory where Nginx will look for your website's files. In this case, we're assuming your website files will be located in/var/www/mysiteon your server. You'll need to create this directory and place yourindex.htmlfile inside it.
index index.html index.htm;: Theindexdirective specifies the default file to serve when a directory is requested. If a user navigates tohttp://example.com/(without a specific file), Nginx will first try to findindex.htmlin therootdirectory, and if not found, it will then look forindex.htm.
location / { ... }: Thelocationblock defines how Nginx should handle requests for specific URI patterns. The/pattern matches all requests. Inside this block, we have:
try_files $uri $uri/ =404;: This is a crucial directive for static sites. It tells Nginx to try serving the requested URI as a file ($uri), then as a directory ($uri/), and if neither exists, return a 404 Not Found error. This is a common and robust way to handle static file serving.
Now that we have our configuration file, we need to enable it. We do this by creating a symbolic link from sites-available to sites-enabled.
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/Before we restart Nginx, it's always a good idea to test your configuration for syntax errors.
sudo nginx -tIf the test is successful, you'll see output indicating that the syntax is ok and the test is successful. If there are errors, Nginx will point you to the line causing the problem.
Finally, restart Nginx to apply the new configuration.
sudo systemctl restart nginxYou've now set up your first Nginx configuration for a static website! If you've pointed your domain's DNS to your server and created the /var/www/mysite/index.html file, you should be able to visit http://example.com in your browser and see your content.
graph TD
A[User Request] --> B{Nginx Server}
B --> C{Server Block Match}
C --> D{Root Directory}
D --> E{Index File Check}
E -- Found --> F[Serve Content]
E -- Not Found --> G[404 Error]
F --> H[Response to User]
G --> H