The server_name
directive in Nginx is used to define which domain(s) or subdomain(s) a server block should respond to. You can configure Nginx to handle specific domain names, wildcard domains, or even catch-all configurations. Here are several examples to help illustrate how server_name
works in different scenarios.
This is the most common use case, where Nginx responds to a specific domain name.
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name example.com;
: Nginx will handle requests forexample.com
.- Requests for
http://example.com
will serve files from/var/www/example
.
You can set up Nginx to handle multiple domain names for a single server block.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name example.com www.example.com;
: Nginx will handle requests for bothexample.com
andwww.example.com
.- This is useful when you want the same content served for both the base domain and its
www
version.
You can use a wildcard *
to match all subdomains of a domain.
server {
listen 80;
server_name *.example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name *.example.com;
: Nginx will handle any subdomain ofexample.com
(e.g.,app.example.com
,blog.example.com
,dev.example.com
).- Requests for
subdomain.example.com
will be served from the same root directory.
If you want to strictly match a single domain without allowing subdomains, use =
before the domain name.
server {
listen 80;
server_name =example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name =example.com;
: Nginx will handle requests only forexample.com
and not for any subdomains likewww.example.com
orapp.example.com
.
You can use an underscore (_
) as a wildcard to match any domain name that doesn't match any other server_name
directive.
server {
listen 80 default_server;
server_name _;
root /var/www/default;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name _;
: This acts as a catch-all for any requests that don’t match any otherserver_name
configurations. It will catch requests for unknown or mismatched domain names.- This is useful as a fallback when you want to serve a default page or handle requests to any undefined domains.
You can have different server blocks for handling various domains with different configurations.
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name api.example.com;
root /var/www/api;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name example.com;
: This block handles requests forexample.com
.server_name api.example.com;
: This block handles requests forapi.example.com
.- Each domain can serve different content from different root directories (
/var/www/example
and/var/www/api
).
You can use regular expressions (regex) to match more complex domain patterns. Note that you need to prefix the server_name
with ~
for regex.
server {
listen 80;
server_name ~^www\.(.+)\.com$;
root /var/www/regex;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server_name ~^www\.(.+)\.com$;
: Nginx will match any domain that starts withwww.
and ends with.com
, such aswww.example.com
orwww.test.com
.- This can be useful for complex domain matching scenarios.
You can use server_name
in combination with a redirect to ensure that requests to www.example.com
are redirected to example.com
(non-www
version).
server {
listen 80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
- The first server block redirects
www.example.com
toexample.com
. - The second server block serves the actual content for
example.com
.
If you want to redirect all HTTP traffic to HTTPS for a specific domain, you can use the following configuration:
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
- This block ensures that all HTTP requests are redirected to the HTTPS version of
example.com
.
server_name
defines which domains a particular server block will handle.- You can specify one or more domains, use wildcards, regular expressions, or set up catch-all blocks.
- This allows you to configure different behaviors for different domains or subdomains on the same Nginx server.
These examples provide flexibility to manage multiple domains and requests efficiently within your Nginx configuration.