0.参考链接
- 官网
http://nginx.org/
1. Nginx安装
1.1 在MacOS下进行安装
-
当前版本
nginx version: nginx/1.15.8
-
通过homebrew安装
brew install nginx
-
查看nginx的配置文件路径等信息
brew info nginx
-
默认文件路径
Docroot is: /usr/local/var/www
nginx.conf: /usr/local/etc/nginx/nginx.conf
nginx will load all files in /usr/local/etc/nginx/servers/.
1.2 其他环境安装待补充…
2. Nginx的常用命令
-
语法
nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
-
查看帮助
nginx -h
-
显示版本
nginx -v
-
显示版本和配置项
nginx -V
-
检测配置文件是否成功配置
nginx -t
-
检测配置文件是否成功配置,并进行快照
nginx -T
-
在配置测试期间禁止非错误消息
nginx -q
-
启动,停止f和重新加载配置
- 启动
sudo nginx
- 快速关机
nginx -s stop
- 优雅关机
nginx -s quit
- 重新加载配置文件
nginx -s reload
- 重新打开日志文件
nginx -s reopen
- 启动
-
设置前置路径
nginx -p prefix
默认路径: /usr/local/Cellar/nginx/1.15.8/
-
设置配置文件
nginx -c filename
默认路径: /usr/local/etc/nginx/nginx.conf
-
从配置文件中设置全局指令
nginx -g directives
3. Nginx的配置文件
3.1 配置文件的结构
简单指令由名称、参数构成,以空格分隔,以分号结尾.
例如: daemon on;
块指令是由大括号({})包围的一组简单指令
上下文:如果块指令在大括号内可以有其他指令,则称为上下文
例如:events,http,server,location
主上下文(main):在上下文之外的指令
- 配置文件结构
#一些指令...
events {
#一些指令...
}
http {
#一些指令...
server {
#一些指令...
location {
#一些指令...
}
}
server {
}
}
3.2 简单的配置文件描述
#定义Nginx运行的用户和用户组,指定NginxWorker进程运行用户及用户组,默认由nobody账号运行
#user nobody;
#Nginx进程数量
worker_processes 2;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ],其中debug输出日志最为最详细,而crit输出日志最少
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程文件,用来指定进程id的存储文件位置
#pid logs/nginx.pid;
#是否以后台模式启动,默认on
#daemon on;
#事件
events {
#定义Nginx每个进程的最大连接数,默认1024
worker_connections 1024;
#网络连接序列化,默认on
accept_mutex on;
#一个进程是否同时接受多个网络连接,默认为off
multi_accept off;
#事件驱动模型
#use epoll;
}
#设定http服务器
http {
#设定文件的mimie类型,类型在配置文件目录页下的mime.types文件中定义,用于Nginx识别文件类型
include mime.types;
#默认文件类型
default_type application/octet-stream;
#设置日志格式,设置为main,用access_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指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off
sendfile on;
#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
sendfile_max_chunk 0;
#防止网络阻塞
#tcp_nopush on;
#长连接超时时间(秒)
keepalive_timeout 75;
#====================== gzip 模块 Begin ======================
#开启gzip压缩传输
gzip on;
#最小压缩文件大小
gzip_min_length 1k;
#压缩缓冲区
gzip_buffers 4 16k;
#压缩版本
gzip_http_version 1.1;
#压缩等级
gzip_comp_level 2;
#压缩类型
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#====================== gzip 模块 End ======================
#====================== 虚拟主机 模块 Begin ======================
server {
#监听端口
listen 8080;
#域名,可以多个,空格分隔
server_name localhost;
#charset utf-8;
#access_log logs/host.access.log main;
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#====================== 虚拟主机 模块 End ======================
#导入其他配置文件
include servers/*;
}
3.3 配置文件测量单位
单位 | 描述对象 | 实例 |
---|---|---|
字节 | 大小 | 1024 |
千字节(k或者K) | 大小 | 1K |
兆字节 (m或M) | 大小 | 2M |
毫秒(ms) | 时间间隔 | 1000ms |
秒(s) | 时间间隔 | 1s |
分钟(m) | 时间间隔 | 1m |
小时(h) | 时间间隔 | 12h |
天(d) | 时间间隔 | 1d |
月(M) | 时间间隔 | 12M |
年(y) | 时间间隔 | 1y |
4. Nginx如何处理请求
4.1 基于名称的虚拟服务器
Nginx决定哪个服务器处理请求
假设有三个虚拟服务器都在端口80上监听,配置如下:
server {
listen 80;
server_name example.org www.example.org;
...
}
server {
listen 80;
server_name example.net www.example.net;
...
}
server {
listen 80;
server_name example.com www.example.com;
...
}
待续…