nginx / Web Service

15.5 nginx 主配置文件(ngx_http_core_module:server段配置)

 

与套接字相关的配置:

server段配置(这里主要指WEB配置):
=======================================

1> listen子段:

Syntax: listen port | address[:port] | unix:/PATH/TO/SOCKIT_FILE
        listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
Default:listen *:80 | *:8000;
Context: server

default_server: 设定为默认虚拟主机;
ssl: 限制仅能通过ssl连接所提供的服务;
backlog=number :后援队列长度;
rcvbuf=size :接收缓冲区大小;
sndbuf=size :发送缓冲区大小;

2> server_name子段:

Syntax:	  server_name name ...;
Default:	server_name "";
Context:	server

指明虚拟主机的主机名称,后面可跟多个有空白字符分隔的字符串;
主机名支持*,通配任意长度的任意字符;比如,server_name   *.kou.cn www.kou.*
主机名支持~起始的字符做正则表达式模式匹配;比如,server_name  ~^www\d+\.kou\.cn$

匹配机制(匹配主机名的读取顺序):
1>首先是字符串精确匹配;
2>其次是左侧*通配符;
3>再次是右侧*通配符;
4>最后是正则表达式;

3>root子段:<定义路径相关的配置1>

Syntax:		root path;
Default:	root html;
Context:	http, server, location, if in location

如果server段(server内部,但在location之外)中定义了root,与location中定义的root有冲突,则location中定义的root生效;
如果location没有定义root,则继承location之外的server内部的root;

4>location子段:<定义路径相关的配置2>

Syntax:		location [ = | ~ | ~* | ^~ ] uri { ... }
      location @name { ... }
Default:	—
Context:	server, location

Sets  configuration  depending  on  a  request  URI, 根据请求URI设置配置;

在一个server的location配置段可存在多个,用于实现从uri到文件系统的路径映射;
nginx会根据用户请求的uri来检查定义的所有location,并找出一个最佳匹配,而后应用其配置;

= :对URI做精确匹配;比如,http://www.a.com/(能匹配),http://www.a.com/index.html(不能匹配);
~ :对URI做正则表达式匹配,区分字符大小写;
~* :对URI做正则表达式匹配,不区分字符大小写;
^~ :对URI的左半部分做匹配检查,不区分字符大小写;
不带符号:匹配起始于此URI的所有RUL;

匹配优先级:=,^~,~/~*,不带符号;

5>alias子段:<定义路径相关的配置3>

Syntax:		alias path;
Default:	—
Context:	location

定义路径别名,文档映射的另一种机制,仅用于location上下文中;

注意,location中使用root指令和alias指令的意义不同:
location中使用root,root给定的路径对应于location中的/uri/左侧的/;
location中使用alias,alias给定的路径对应于location中的/uri/右侧的/;

举例:
  location ^~ /images/ {
    root /data/pictures/;
  }
如果访问的URI为/images/a.jpg,则服务器端的资源路径为/data/pictures/images/a.jpg;

  location ^~ /images/ {
    alias /data/pictures/;
  }
如果访问的URI为/images/a.jpg,则服务器端的资源路径为/data/pictures/a.jpg;

6>index:

Syntax:		index file ...;
Default:	index index.html;
Context:	http, server, location

7>error_log:

Syntax:		error_log file [level];
Default:	error_log logs/error.log error;
Context:	main, http, mail, stream, server, location

8>error_page:

Syntax:		error_page code ... [=[response]] uri;
Default:	—
Context:	http, server, location, if in location

举例:
error_page  404   /404.html;
error_page  500  502  503  504   /50x.html;

error_page  404  =200  /empty.gif;

9>server_tokens:

Syntax:		server_tokens on | off | build | string;
Default:	server_tokens on;
Context:	http, server, location

隐藏nginx版本号;默认是开启显示版本号;

Leave a Reply

Your email address will not be published. Required fields are marked *