Serving Static Content with Nginx
Configure Nginx to efficiently serve static files and assets, optimizing performance for web applications.
What is Static Content?
When you visit a website, you often see a mix of content. Some of it changes frequently (like news feeds), and some stays the same.
Static content refers to files that are delivered to the user exactly as they are stored. They don't change based on who is viewing them or when.
- HTML files: The structure of your web pages.
- CSS files: Styles and visual presentation.
- JavaScript files: Interactive elements and client-side logic.
- Images: JPG, PNG, GIF, SVG.
- Fonts: Web fonts like TTF, WOFF.
Nginx is incredibly efficient at serving these static files.
Setting the Root Directory
To tell Nginx where to find your static files, you use the root directive. This specifies the base directory for your website's content.
When Nginx receives a request, it appends the requested URI to the root path to locate the file.
http {
server {
listen 80;
server_name example.com;
root /var/www/mywebsite; # Nginx looks for files here
}
}All lessons in this course
- Nginx Installation & Setup
- Basic Nginx Configuration
- Serving Static Content with Nginx
- Nginx Location Blocks & Request Matching