目录结构
进入Nginx的主目录我们可以看到这些文件夹
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
其中这几个文件夹在刚安装后是没有的,主要用来存放运行过程中的临时文件
client_body_temp fastcgi_temp proxy_temp scgi_temp
conf:用来存放配置文件相关
html:用来存放静态文件的默认目录 html、css等
sbin:nginx的主程序
基本运行原理
Nginx通过主程序文件启动主进程,主进程加载nginx.conf配置文件,启动子进程处理网络请求。用户发起网络请求,子进程接收网络请求,将请求解析处理,返回给用户处理结果响应
Nginx配置
最小配置
worker_processes
worker_processes 1; 默认为1,表示开启一个业务进程
worker_connections
worker_connections 1024; 单个业务进程可接受连接数
include mime.types
include mime.types; 引入http mime类型,nginx服务器指定响应的类型
default_type application/octet-stream
default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输
keepalive_timeout 65;
keepalive_timeout 65;超时连接时长
sendfile on
sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝
未开启sendfile,nginx接受到网络请求,将请求的文件进行读取,写入到应用程序内存,然后读取应用程序内存的文件写入到网络接口缓存,然后通过网络接口响应给用户
开启sendfile,nginx接受到网络请求,发送信号给linux,linux网络接口缓存读取文件,响应给用户
虚拟主机配置
server {
listen 80; 监听端口号
server_name localhost; 主机名
location / { 匹配路径
root html; 文件根目录
index index.html index.htm; 默认页名称
}
error_page 500 502 503 504 /50x.html; 报错编码对应页面
location = /50x.html {
root html;
}
}