Nginx web server provides a lot of different configuration about the web service. Some of the configurations are used very often. server_name
is one of them which can be miss interpreted.
Single Domain Name
In the old days internet was so little. There was less site and domain names than today. System administrators were prefer to assign a single domain to a single web server like Apache etc.
Multiple Domain Names
In today there are a lot of web sites and domains. In a regular IT infrastructure we generally need to server multiple domain names in a single web server instance. Say we have sites like site1.com
, site2.com
and site3.com
. Should we start separate instance for each of them. But what about the port 80 or 433 , how will we set multiple web sites or domain names in a single port. Here is the solution server_name
Server Name
We will create separate server configurations like below. Each server configuration is dedicated for a separate domain name. So single nginx instance and port will listen and serve for multiple domain names. The domain name separation will be handled by nginx
according to our configuration.
server { listen 80; server_name site1.com; ... } server { listen 80; server_name site2.com; ... } server { listen 80; server_name site3.com; ... }
Using Wildcards
server_name configuration provides us some useful features. We can use wildcards in order to specify any character and length for a given sub domain. In this example we want to specify sub domain which ends with user
.
site_name "*user.site.com";
Using Regular Expression
If wildcards is not enough to express domain or sub domain name we can use more complex structure like regular expression. In this example we will accept sub domains which starts with user
and ends with a number which is minimum 1 maximum 2 digits.
server_name "user\d{1,2}";
Using Multiple Domain Names
We have ability to specify multiple domain names for a single server configuration. We will just provide domain names by delimiting with space. Also using double quote will be a good practice and error prevention technique.
server_name "site1.com" "mysite.com" "yoursite.com";