This guide explains what nginx blocks are, how they work in CentOS, and how to configure them for multiple websites on one server. You’ll learn the difference between server and location blocks, where the config files live, and how to test and reload changes safely.
What Are Nginx Blocks?
Nginx uses a block-based configuration system. A block is a named section enclosed in curly braces that contains directives. The two most important are the server block, which defines a virtual host (a website), and the location block, which controls how requests to specific URLs are handled. On CentOS, the main config file is /etc/nginx/nginx.conf, and it typically includes files from /etc/nginx/conf.d/ and /etc/nginx/default.d/. Each .conf file in those directories can hold one or more server blocks.
How Server Blocks Work
A server block listens on a port (usually 80 for HTTP, 443 for HTTPS) and matches requests based on the server_name directive. When a request arrives, nginx checks the Host header against all server blocks. If none match, it uses the default server block for that port. You can have multiple server blocks on the same port, each with a different domain name. For example, you can host example.com and example.org on one CentOS server by creating two separate files in /etc/nginx/conf.d/.
Location Blocks Explained
Location blocks sit inside a server block and define how to handle specific URIs. For instance, location /images/ would apply to any request starting with /images/. You can use prefix matching (as in the example), exact matching with =, regular expressions with ~ (case-sensitive) or ~* (case-insensitive), and the ^~ modifier to stop regex checks. A common use is to set caching headers for static files or to proxy requests to a backend application like PHP-FPM or Node.js.
How to Choose Between Server and Location
Use a server block when you need a completely separate website with its own domain, SSL certificate, or root directory. Use a location block when you need to apply rules to a subset of URLs within the same site. For example, you might have one server block for example.com and a location block for /api that proxies to a backend, while the rest of the site serves static files. As a rule of thumb, if the content is served from a different document root or uses a different domain, use a server block; if it’s just a different path on the same domain, use a location block.
Common Configuration Problems
One frequent issue is forgetting to reload nginx after editing config files. Changes only take effect after sudo systemctl reload nginx or sudo nginx -s reload. Another common mistake is having two server blocks with the same server_name on the same port; nginx will use the first one and log a warning. Also, check that the root directory exists and has the correct permissions. If you see a 403 Forbidden error, it’s often because the index file is missing or the directory lacks read permissions. Always run sudo nginx -t to test the config before reloading.
Comparing Nginx and Apache
If you’re coming from Apache, you’ll notice that nginx uses a different model. Apache’s .htaccess files allow per-directory configuration, but nginx does not support them; all configuration must be in the main config files. This is actually a performance advantage because nginx doesn’t have to scan directories for override files on every request. However, it means you need to be more careful when editing configs, as a syntax error will prevent nginx from starting. Also, nginx handles static files and concurrent connections more efficiently than Apache’s default prefork mode, but Apache’s mod_php is simpler for PHP apps, whereas nginx typically needs PHP-FPM.
Safety and Best Practices
Before making any changes, back up the current config: sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak. Always test with sudo nginx -t after editing. If the test fails, nginx will not reload, and your site stays up. Never edit the main config while nginx is running without testing first. Also, avoid using sudo systemctl restart nginx if a reload will do; restart causes a brief downtime, while reload is seamless. For security, don’t expose server version numbers in error pages (set server_tokens off), and use HTTPS with a valid certificate for any site that handles sensitive data.
Alternatives to Nginx Blocks
If you need a web server that supports .htaccess or you’re already familiar with Apache, you can use Apache’s VirtualHost feature instead. For a simpler setup, you could use a single server block with multiple location blocks, but that won’t give you separate domains. Another alternative is Caddy, which automatically obtains SSL certificates and has a simpler config syntax. However, nginx remains the most widely used and is a solid choice for high-traffic sites.
Frequently Asked Questions
What is the difference between a server block and a location block? A server block defines a virtual host (a website) with its own domain and port. A location block sits inside a server block and controls how requests to specific URLs are handled, such as serving static files or proxying to a backend.
Where are nginx server blocks stored on CentOS? The main config is /etc/nginx/nginx.conf. Server blocks are typically placed in separate files under /etc/nginx/conf.d/ or /etc/nginx/default.d/. Each file can contain one or more server blocks.
How do I test my nginx config before reloading? Run sudo nginx -t. If the syntax is correct, it will print “syntax is ok” and “test is successful”. Only then should you run sudo systemctl reload nginx.
Why does my nginx site show a 403 Forbidden error? This usually means the root directory doesn’t contain an index file (like index.html) or the directory permissions don’t allow nginx to read it. Check that the root path is correct and that the directory has execute permissions for the nginx user (often nginx or apache).
Can I have multiple SSL certificates on one IP? Yes, with Server Name Indication (SNI), nginx can serve different certificates for different domains on the same IP and port 443. Just create separate server blocks for each domain, each with its own ssl_certificate directive.
What does the server_name _; directive do? It’s a catch-all server block that matches any request that doesn’t match another server block. It’s often used as a default to block unknown Host headers or to redirect all unmatched requests to a main site.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Use Mac More Efficiently: 21 Proven Tips
- Best CPUs for Gaming and Streaming: Encoder Guide
- How to Compare AI Features and Performance in Phones
- Create Google Meet Account: Step-by-Step Guide
- AI Frameworks: How Developers Choose and Ship Them
- Nginx Virtual Hosts: Name-Based vs IP-Based