Nginx 服务启动时会读入配置文件,后续的行为则按照配置文件中的指令进行。Nginx 的配置文件是纯文本文件,默认安装 Nginx 后,其配置文件均在 /usr/local/nginx/conf/ 目录下。其中,nginx.conf 为主配置文件。配置文件中以 # 开始的行,或者是前面有若干空格或者 TAB 键,然后再跟 # 的行,都被认为是注释。这里只是了解主配置文件的结构。
Nginx 配置文件是以 block(块)形式组织,每个 block 都是以 “{}” 表示,block 分为几个层级,整个配置文件为 main 层级,即最大的层级;在 main 层级下可以有 event、http 、mail 等层级,而 http 中又会有 server block,server block中可以包含 location block。
每个层级可以有自己的指令(Directive),例如 worker_processes 是一个main层级指令,它指定 Nginx 服务的 Worker 进程数量。有的指令只能在一个层级中配置,如worker_processes 只能存在于 main 中,而有的指令可以存在于多个层级,在这种情况下,子 block 会继承 父 block 的配置,同时如果子block配置了与父block不同的指令,则会覆盖掉父 block 的配置。指令的格式是“指令名 参数1 参数2 … 参数N;”,注意参数间可用任意数量空格分隔,最后要加分号。
下图是 Nginx 配置文件通常结构图示。
以下是在 Ubuntu 12.04 系统成功安装 Nginx 之后的主配置文件:
#Nginx服务器正常启动时会读取该配置文件,以下的值都是默认的,若需要可自行修改;
#以下是配置选项
#Nginx worker进程运行的用户以及用户组
#语法格式:user username[groupname]
#user nobody;
#Nginx worker 进程个数
worker_processes 1;
#error 日志设置
#语法格式:error /path/file level
#其中/path/file是一个具体文件;level是日志的输出级别,其取值如下:
#debug info notice warn error crit alert emerg,从左至右级别增大;
#若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#保存master进程ID的pid文件存放路径
#语法格式:pid path/file
#pid logs/nginx.pid;
#事件类配置项
#一般有以下几种配置:
#1、是否打开accept锁
# 语法格式:accept_mutex [on | off];
#2、lock文件的路径
# 语法格式:lock_file path/file;
#3、使用accept锁后到真正建立连接之间的延迟时间
# 语法格式:accept_mutex_delay Nms;
#4、批量建立新连接
# 语法格式:multi_accept [on | off];
#5、选择事件模型
# 语法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];
#6、每个worker进行的最大连接数
# 语法格式:worker_connections number;
events {
worker_connections 1024;
}
#以下是http模块
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;
#server块
# 每个server块就是一个虚拟主机,按照server_name来区分
server {
#监听端口
listen 80;
#主机名称
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location语法:location[= | ~ | ~* | ^~ | @] /uri/ {}
#location尝试根据用户请求中的URI来匹配 /uri表达式,若匹配成功,则执行{}里面的配置来处理用户请求
#以下是location的一般配置项
#1、以root方式设置资源路径
# 语法格式:root path;
#2、以alias方式设置资源路径
# 语法格式:alias path;
#3、访问首页
# 语法格式:index file...;
#4、根据HTTP返回码重定向页面
# 语法格式:error_page code [code...] [= | =answer-code] uri | @named_location;
#5、是否允许递归使用error_page
# 语法格式:recursive_error_pages [on | off];
#6、try_files
# 语法格式:try_files path1 [path2] uri;
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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
参考资料:
《深入理解Nginx》
原文地址:http://blog.csdn.net/chenhanzhun/article/details/42363661