1. listen and server_name

1.1. listen

1.1.1. Directive

  1. Syntax: listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
  2. listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
  3. listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
  4. Default: listen *:80 | *:8080 ;
  5. Context: server

1.1.2. Instroduction

Sets the address and port for IP, or the path for a UNIX-domain socket on which the server will accept requests. Both address and port, or only address or only port can be specified. An address may also be a hostname:

  1. listen 127.0.0.1:8080 ;
  2. listen 127.0.0.1 ;
  3. listen 8080 ;
  4. listen *:8080 ;
  5. listen localhost:8080 ;

Parameters:

  • default_server

Specify default server,before 0.8.21 the parameter is named default.

  • ssl

Sets server work on ssl modole.

  • http2

Sets server work protocol is http2,in this case it should be work on ssl modole.

  • backlog=num

sets the backlog parameter in the listen() call that limits the maximum length for the queue of pending connections. By default, backlog is set to -1 on FreeBSD, DragonFly BSD, and macOS, and to 511 on other platforms.

1.2. server_name

1.2.1. Directive

  1. Syntax: server_name name ... ;
  2. Default: server_name "" ;
  3. Context: server

1.2.2. Instroduction

During searching for a virtual server by name, if the name matches more than one of the specified variants, (e.g. both a wildcard name and regular expression match), the first matching variant will be chosen, in the following order of priority:

  1. the exact name
  2. the longest wildcard name starting with an asterisk, e.g. “*.example.com”
  3. the longest wildcard name ending with an asterisk, e.g. “mail.*”
  4. the first matching regular expression (in order of appearance in the configuration file)

Named captures in regular expressions create variables (0.8.25) that can later be used in other directives:

  1. server {
  2. server_name ~^(www\.)?(.+)$;
  3. location / {
  4. root /sites/$2;
  5. }
  6. }
  7. server {
  8. server_name ~^(www\.)?(?<domain>.+)$;
  9. location / {
  10. root /sites/$domain;
  11. }
  12. }

2. Based On Server_name

If different nginx configuration file has same server names,the first loaded server name will become defaut server name .
If one server name includes *.xxx,others has specific server name like bbs.xxx act.xxx,the specific server name has higher priority.

2.1. Example 1

2.1.1. Nginx Configuration

[root@centos-81 conf.d]# head -4 heyang.conf heyang2.conf

  1. # heyang.conf
  2. server {
  3. listen 80;
  4. server_name www.heyang.com bbs.heyang.com ;
  5. root /opt/website/web01 ;
  6. # heyang2.conf
  7. server {
  8. listen 80;
  9. server_name bbs.heyang.com;
  10. root /opt/website/web02 ;

2.1.2. Test

[root@centos-81 conf.d]# /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf -t ## One warn,no error

  1. nginx: [warn] conflicting server name "bbs.heyang.com" on 0.0.0.0:80, ignored
  2. nginx: the configuration file /opt/apps/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /opt/apps/nginx/conf/nginx.conf test is successful

[root@centos-81 conf.d]# /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf -s reload
[root@centos-81 conf.d]# curl bbs.heyang.com

  1. web01

2.2. Example 2

2.2.1. Nginx Configuration

[root@centos-81 conf.d]# head -4 heyang.conf heyang2.conf

  1. # heyang.conf
  2. server {
  3. listen 80;
  4. server_name www.heyang.com bbs.heyang.com ;
  5. root /opt/website/web01 ;
  6. # heyang2.conf
  7. server {
  8. listen 80;
  9. server_name bbs.heyang.com;
  10. root /opt/website/web02 ;

2.2.2. Test

[root@centos-81 conf.d]# /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf -t ## One warn,no error

  1. nginx: [warn] conflicting server name "bbs.heyang.com" on 0.0.0.0:80, ignored
  2. nginx: the configuration file /opt/apps/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /opt/apps/nginx/conf/nginx.conf test is successful

[root@centos-81 conf.d]# /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf -s reload
[root@centos-81 conf.d]# curl bbs.heyang.com

  1. web01

3. Base on IP or port

If different nginx configuration file has same IP address and port,the first loaded address and port will become defaut virtual host.
Listen directive can speicify default virtual host by default option.

3.1. Example 1

3.1.1. Nginx Configuration

[root@centos-81 conf.d]# head -4 heyang.conf heyang2.conf

  1. # heyang.conf
  2. server {
  3. listen 80;
  4. server_name *.heyang.com;
  5. root /opt/website/web01 ;
  6. # heyang2.conf
  7. server {
  8. listen 80;
  9. server_name bbs.heyang.com;
  10. root /opt/website/web02 ;

3.1.2. Test

[root@centos-81 conf.d]# curl 192.168.1.81

  1. web01

3.2. Example 2

3.2.1. Nginx Configuration

[root@centos-81 conf.d]# head -4 heyang.conf heyang2.conf

  1. # heyang.conf
  2. server {
  3. listen 80;
  4. server_name *.heyang.com;
  5. root /opt/website/web01 ;
  6. # heyang2.conf
  7. server {
  8. listen 80 default;
  9. server_name bbs.heyang.com;
  10. root /opt/website/web02 ;

3.2.2. Test

[root@centos-81 conf.d]# curl 192.168.1.81

  1. web02