主配置文件组织结构
Nginx的配置文件是进行分块的,对于每个块可以设定指令以及变量;指令分为两种,第一种是全局指令(放置于全局配置段中,即文档根),第二种模块指令(是由模块引入,其指令也只能放置在相应的配置段中),一个简单的指令由名称和空格分隔参数,并以分号结束(;)。而变量一般也是由模块提供,但变量可以自定义设置,如(set variable value;),调用变量($variable;)等。
主配置文件主要分为以下几个段:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  | 
						main block  #全局配置段,设置的指令对全局生效,且配置指令不用放在{}中,除了全局配置段之外,往下的配置段都需要编译过此模块,其对应的指令才能生效; event {     #event配置段,用来设置事件驱动模块是如何工作的,只有编译过event模块,此段设置的指令才能生效,对http和mail段都生效,主要是面对用户并发连接时的组织配置机制;   ... } http {      #http配置段,所有跟web服务相关的都必须且只能放在http配置段中(除了http就是mail段了,知道就行,不用深入了解),http配置段一般用于设置多个虚拟主机公用指令;    ...    upstream { #反向代理配置段,对于一个web服务来说不是必须的,只有在用到反向代理时才需要upstream段,这里先略过,后面说;      ...    }    server {   #http配置段中的server配置段,用来设置虚拟主机,在http配置段中可以有多个server配置段,每个server配置段就是一个虚拟主机,然后针对此虚拟主机可以设置指令;      listen  80;                     #此虚拟主机监听的端口;      servername www.ywnds.com;       #每个虚拟主机中可以定义主机名、根目录、路径别名等;      root  /usr/local/nginx/html;    #网站根目录;      location /uri {                 #在每个server段中还可以定义location配置段,可以对此虚拟主机下的资源进行映射到别处去,有点类似alias,但比alias强大很多;        ...      }            ...    }    server {   #在http配置段中,对于server段是采用include包含一个配置文件,这个配置文件就是一个server,一个虚拟主机,多个虚拟主机就提供多个配置文件;       ...    } }  | 
					
Nginx的主配置文件
下面先来看看主配置文件参数,然后参考上面介绍的主配置文件结构来看。
$ cat /etc/nginx/nginx.conf
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  | 
						#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events {  worker_connections 1024; } http {  include mime.types;  default_type application/octet-stream;  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '  # '$status $body_bytes_sent "$http_referer" '  # '"$http_user_agent" "$http_x_forwarded_for"';  #access_log logs/access.log main;  sendfile on;  #tcp_nopush on;  #keepalive_timeout 0;  keepalive_timeout 65;  #gzip on;  include  /etc/nginx/conf.d/*.conf; }  | 
					
从这个配置文件可以看出,主配置文件中有这么三个配置端,全局配置段、event配置段、http配置段;如果你是编译安装的nginx的话,那么在http配置段中应该还有server配置段和location配置段。但是这里我没有这么贴,我是把server虚拟主机段给放在了另外的目录下(/etc/nginx/conf.d),然后使用include在主配置文件中包含进来了。如果你使用安装包安装过nginx,应该就会发现nginx配置文件的组织就是这么弄的,建议这么使用。Nginx的主配置文件中主要用于设定一些全局的指令,具体指令后面介绍,这种配置文件规划是非常合理的,易于管理,当有修改操作时只需要去修改对应的配置文件即可。
另外注意,在配置配置文件中以#符号开头的被认为是一个注释行。
虚拟主机配置文件
大概说完了Nginx主配置文件的组织结构,有了一个清晰的认识后,下面看一下Nginx的虚拟主机配置文件样例,就是把server段独立成一个配置文件包含进nginx.conf主配置文件中了。
| 
					 1 2  | 
						$ mkdir /etc/nginx/conf.d/ $ cat /etc/nginx/conf.d/server.conf  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  | 
						server {  listen 80;  server_name localhost;  #charset koi8-r;  #access_log logs/host.access.log main;  location / {  root html;  index index.html index.htm;  }  #error_page 404 /404.html;  # redirect server error pages to the static page /50x.html  #  error_page 500 502 503 504 /50x.html;  location = /50x.html {  root html;  }  # proxy the PHP scripts to Apache listening on 127.0.0.1:80  #  #location ~ \.php$ {  # proxy_pass http://127.0.0.1;  #}  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  #  #location ~ \.php$ {  # root html;  # fastcgi_pass 127.0.0.1:9000;  # fastcgi_index index.php;  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  # include fastcgi_params;  #}  # deny access to .htaccess files, if Apache's document root  # concurs with nginx's one  #  #location ~ /\.ht {  # deny all;  #} }  | 
					
对Nginx配置文件的组织结构有了一个清晰的认识后,接下来就可以说一说每个配置段所支持的常用指令有哪些了,以及指令的含义,以及server段的配置。
