一.nginx的目录结构

[root@node nginx_116]# ls

client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp

conf 存放nginx所有配置文件的目录,主要nginx.conf

html 存放nginx默认站点的目录,如index.html、error.html等

logs 存放nginx默认日志的目录,如error.log access.log

sbin 存放nginx主命令的目录,sbin/nginx

二.nginx主配置文件解析

CoreModule核心模块

user www;                       #Nginx进程所使用的用户
worker_processes ; #Nginx运行的work进程数量(建议与CPU数量一致或auto)
error_log /log/nginx/error.log #Nginx错误日志存放路径
pid /var/run/nginx.pid #Nginx服务运行后产生的pid进程号

events事件模块

events {

    worker_connections  //每个worker进程支持的最大连接数

    use epool;          //事件驱动模型, epoll默认

}

http内核模块

//公共的配置定义在http{}
http { //http层开始
...
//使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
'server' {
listen ; //监听端口, 默认80
server_name localhost; //提供服务的域名或主机名
access_log host.access.log //访问日志
//控制网站访问路径
'location' / {
root /usr/share/nginx/html; //存放网站代码路径
index index.html index.htm; //服务器返回的默认页面文件
}
//指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
error_page /50x.html;
}
...
//第二个虚拟主机配置
'server' {
...
} include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件 } //http层结束

nginx核心配置

#定义nginx工作进程数
worker_processes ;#错误日志
#error_log logs/error.log;
#http定义代码主区域http {
include mime.types;
default_type application/octet-stream;
#定义nginx的访问日志功能
#nginx会有一个accses.log功能,查看用户访问的记录
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;
keepalive_timeout ;
#开启gzip压缩传输 gzip on;
#虚拟主机1 定义一个 斗鱼网站 server {
#定义nginx的访问入口端口,访问地址是 192.168.11.37:
listen ;
#定义网站的域名www.woshidouyu.tv
#如果没有域名,就填写服务器的ip地址 192.168.11.37 server_name www.woshidouyu.tv;
#nginx的url域名匹配
#只要请求来自于www.woshidouyu.tv/
#只要请求来自于www.woshidouyu.tv/qweqwewqe
#最低级的匹配,只要来自于www.woshidouyu.tv这个域名,都会走到这个location
location / {
#这个root参数,也是关键字,定义网页的根目录
#以nginx安装的目录为相对路径 /opt/nginx112/html
#可以自由修改这个root定义的网页根目录 root html;
#index参数定义网站的首页文件名,默认的文件名 index index.html index.htm;
}
#错误页面的优化(只要是遇到前面4系列的错误,就会直接跳转到相对目录下的40x.html页面)
error_page /40x.html;
}
}
05-08 08:35