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.
Nginx Web服务器提供了有关Web服务的许多不同配置。 一些配置经常使用。 server_name
是其中之一,可能会错过解释。
单一域名 (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.
在过去,互联网很少。 网站和域名比今天少。 系统管理员倾向于将单个域分配给单个Web服务器(例如Apache等)。
多个域名 (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
如今,有许多网站和域。 在常规的IT基础架构中,我们通常需要在单个Web服务器实例中为多个域名提供服务器。 假设我们有site1.com
, site2.com
和site3.com
。 我们应该为它们中的每一个启动单独的实例。 但是端口80或433呢,我们将如何在单个端口中设置多个网站或域名。 这是解决方案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.
我们将创建单独的服务器配置,如下所示。 每个服务器配置专用于一个单独的域名。 因此,单个nginx实例和端口将侦听并服务于多个域名。 域名分离将由nginx
根据我们的配置进行处理。
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
.
server_name配置为我们提供了一些有用的功能。 我们可以使用通配符来指定给定子域的任何字符和长度。 在此示例中,我们要指定以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.
如果通配符不足以表达域名或子域名,我们可以使用更复杂的结构,如正则表达式。 在此示例中,我们将接受子域,该子域以user
开头,并以至少1个最大2个数字的数字结束。
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";
翻译自: https://www.poftut.com/what-is-server_name-in-nginx-web-server-configuration/