1.配置文件语法格式
# 属性
worker_processes 1;
# 配置块
events {
# 属性
worker_connections 1024;
}
# 配置块
http {
# 属性
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#配置快
server {
# 属性
listen 80;
server_name localhost;
# 配置块
location / {
# 属性
root html;
index index.html index.htm;
}
# 配置块 参数
location /nginx_status {
# 属性
stub_status on;
access_log off;
}
}
}
(1)配置块: 名称开头用大口号包裹其对应属性。上述例子中events、http、server、location。
(2)属性:基于空格切分属性名与属性值,属性值可能有多个项 都以空格进行切分。如access_log logs/host.access.log main。
(3)属性值:其配置在块名称与大括号间,其值如果有多个也是通过空格进行拆分。如上述例子中nginx_status 。也可以使用正则表达式匹配。
2.配置一个静态WEB服务
# 当前nginx服务启动几个worker进程,建议是与cpu核心数相同
worker_processes 1;
# 事件模块配置
events {
# 一个worker进程最多处理多少连接
worker_connections 1024;
}
# http服务根路径
http {
# 引入外部文件,如下一个代码块
include mime.types;
# 默认请求类型
default_type application/octet-stream;
# 允许发送文件
sendfile on;
# 长连接超时时间,65s
keepalive_timeout 65;
# 服务模块,可以是多个,如果是多个listen和server_name不能同时一样
server {
# 监听端口
listen 80;
# 服务名称,配置域名,可以有多个使用空格分隔
server_name localhost
# 映射路径配置,以“/”开头的请求,都会由此location处理
location / {
# 当前站点的根目录,html是相对路径
root html;
# 访问引导页
index index.html index.htm;
}