1. listen and server_name
1.1. listen
1.1.1. Directive
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]];
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]];
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]];
Default: listen *:80 | *:8080 ;
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:
listen 127.0.0.1:8080 ;
listen 127.0.0.1 ;
listen 8080 ;
listen *:8080 ;
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
Syntax: server_name name ... ;
Default: server_name "" ;
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:
- the exact name
- the longest wildcard name starting with an asterisk, e.g. “*.example.com”
- the longest wildcard name ending with an asterisk, e.g. “mail.*”
- 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:
server {
server_name ~^(www\.)?(.+)$;
location / {
root /sites/$2;
}
}
server {
server_name ~^(www\.)?(?<domain>.+)$;
location / {
root /sites/$domain;
}
}
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
# heyang.conf
server {
listen 80;
server_name www.heyang.com bbs.heyang.com ;
root /opt/website/web01 ;
# heyang2.conf
server {
listen 80;
server_name bbs.heyang.com;
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
nginx: [warn] conflicting server name "bbs.heyang.com" on 0.0.0.0:80, ignored
nginx: the configuration file /opt/apps/nginx/conf/nginx.conf syntax is ok
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
web01
2.2. Example 2
2.2.1. Nginx Configuration
[root@centos-81 conf.d]# head -4 heyang.conf heyang2.conf
# heyang.conf
server {
listen 80;
server_name www.heyang.com bbs.heyang.com ;
root /opt/website/web01 ;
# heyang2.conf
server {
listen 80;
server_name bbs.heyang.com;
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
nginx: [warn] conflicting server name "bbs.heyang.com" on 0.0.0.0:80, ignored
nginx: the configuration file /opt/apps/nginx/conf/nginx.conf syntax is ok
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
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
# heyang.conf
server {
listen 80;
server_name *.heyang.com;
root /opt/website/web01 ;
# heyang2.conf
server {
listen 80;
server_name bbs.heyang.com;
root /opt/website/web02 ;
3.1.2. Test
[root@centos-81 conf.d]# curl 192.168.1.81
web01
3.2. Example 2
3.2.1. Nginx Configuration
[root@centos-81 conf.d]# head -4 heyang.conf heyang2.conf
# heyang.conf
server {
listen 80;
server_name *.heyang.com;
root /opt/website/web01 ;
# heyang2.conf
server {
listen 80 default;
server_name bbs.heyang.com;
root /opt/website/web02 ;
3.2.2. Test
[root@centos-81 conf.d]# curl 192.168.1.81
web02