主配置文件是 /etc/nginx/nginx.conf 是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{} 来表示开始与结束。
1。Main 位于nginx.conf 配置文件的最高层
2.Main 层下可以有Event、HTTP层
3.HTTP层下面有允许有多个Server层,用于对不同的网站做不同的配置。
4.Server层也允许有多个Location ,用于对不同的路径进行不同模块的配置。
修改完配置文件使用如下命令进行核对并重新加载:
nginx -t -c /etc/nginx/nginx.conf
nginx -s reload
//nginx 默认配置语法
user nginx;
worker_processes 1;//工作进程,配置和CPU个数保持一致
pid // nginx 服务启动的pid
error_log /var/log/nginx/error.log warn; //错误日志,后接路径
pid /var/run/nginx.pid;
//events 事件模块
events { //事件模块
worker_connections 1024; //每个worker进程支持的最大连接数
use epoll; //内核模型,select ,poll ,epoll
}
//非虚拟主机的配置或公共配置定义在http{} 段内,server
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
// 日志格式
// remote_addr //表示客户端地址
//remote_user //http客户端请求nginx认证用户名
//time_local //Nginx的时间
//request //Request 请求行,GET等方法,http协议
//status // response 返回状态码
//body_bytes_send //从服务端响应给客户端body信息
// http_referer //http上一级页面,防盗链,用户行
//http_user_agent //http头部信息,客户端访问设备
// http_x_forwarded-for //http请求携带的http信息
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 /var/log/nginx/access.log main;
server {
listen 80://监听端口, 默认80
server_name localhost; //提供服务的域名或主机名
//控制网站访问路径
location / {
root /usr/share/nginx/html; //存放网站路径
index index.html index.htm; // 默认访问首页文件
}
//错误页面,统一定义错误页面
//定义请求错误,指定错误代码
error-page 500 502 503 504 /50x.html
//错误代码重定向到新的Location
location = /50x.html {
root html;
}
}
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}