nginx 文件的默认配置文件位置 /usr/local/etc/nginx/nginx.conf
打开 /usr/local/etc/nginx/ 目录可以看到,里面有很多的配置文件,其中有nginx.conf和conf.d/default.conf
配置文件架构
// nginx全局块
...
// events块
events {
...
}
// http 块
http {
// http全局块
...
// server块
server {
...
}
// http全局块
...
}
配置文件加注释说明
# 配置nginx的用户组 默认为nobody
#user nobody;
# 配置nginx的主线程数量 nginx是一个主线程下面多个子线程
worker_processes 1;
# 配置nginx的错误日志 格式为 log路径 log级别
# error_log 的日志级别为: debug info notice warn error crit alert emerg 紧急由低到高
# error_log的默认日志级别为error,那么就只有紧急程度大于等于error的才会记录在日志
# error_log 的作用域为 main http mail stream server location
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定nginx进程运行文件存放地址
#pid logs/nginx.pid;
events {
# poll是多路复用IO中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
# use poll
# 设置网络的连接序列化 防止惊群现象发生 默认为 on
# accept_mutex on;
# 设置一个进程是否同时接受多个网络连接 默认为 off
# multi_accept off
# 最大连接数 默认为 512
worker_connections 1024;
}
http {
# 文件扩展名和文件类型映射表
include mime.types;
# 默认文件类型
default_type application/octet-stream;
# 日志格式
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 指定使用 sendfile 系统调用来传输文件。优点在于在两个文件描述符之间传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,效率高,称之为零拷贝,这个东西有点讲究,自行百度
# sendfile 作用域 location server http
sendfile on;
#tcp_nopush on;
# 链接超时时间 默认 75s 作用域 http server location
#keepalive_timeout 0;
keepalive_timeout 65;
# 开始gzip压缩
#gzip on;
server {
# 端口号
listen 8080;
# 域名或ip
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 对请求的路由进行过滤 正则匹配
location / {
root html;
index index.html index.htm;
}
...
}
include /usr/local/etc/nginx/conf.d/*;
}
其中log_format 取值
$remote_addr, $http_x_forwarded_for(反向) 记录客户端IP地址
$remote_user 记录客户端用户名称
$request 记录请求的URL和HTTP协议
$status 记录请求状态
$body_bytes_sent 发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
$bytes_sent 发送给客户端的总字节数。
$connection 连接的序列号。
$connection_requests 当前通过一个连接获得的请求数量。
$msec 日志写入时间。单位为秒,精度是毫秒。
$pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer 记录从哪个页面链接访问过来的
$http_user_agent 记录客户端浏览器相关信息
$request_length 请求的长度(包括请求行,请求头和请求正文)。
$request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_iso8601 ISO8601标准格式下的本地时间。
$time_local 通用日志格式下的本地时间。
反向代理
nginx使用proxy_pass属性来进行反向代理。
server {
# 监听 localhost:8080
listen 8080;
server_name localhost;
# / 表示匹配所有的请求,所有的请求都会经过这个过滤器
location / {
# 设定请求转发的地址
proxy_pass http://localhost:9000;
}
}
需要注意的是 proxy_pass 的写法,必须是http://或者https://开头的,http头是不能省的。
nginx跨域
nginx跨域需要在header里面加上允许跨域的源等信息
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
}
在正式环境中,origin最好不要设置为。*
nginx缓存
proxy_cache_path /tmp/cache/test levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
location ~ .*\.(jpg|png)$ {
proxy_pass http://127.0.0.1:9000;
proxy_cache my_cache;
proxy_cache_valid 200 304 1y;
proxy_cache_valid any 1m;
expires 1y;
}
- expires 缓存时间,默认为秒
- proxy_cache_path 执行缓存文件的目录,如果没有的话需要提前创建,不然nginx会报错
- levels 采用2级目录来存储
- key_zone 在共享内存中设置一块存储区域来存放缓存的key和metadata(类似使用次数),这样nginx可以快速判断一个request是否命中或者未命中缓存,1m可以存储8000个key,10m可以存储80000个key
- max_size 最大cache空间,如果不指定,会使用掉所有disk space,当达到配额后,会删除最少使用的cache文
- inactive 未被访问文件在缓存中保留时间,在指定时间内未被访问的文件会被删除
- use_temp_path 如果为off,则nginx会将缓存文件直接写入指定的cache文件中,而不是使用temp_path存储,official建议为off,避免文件在不同文件系统中不必要的拷贝
- proxy_cache 启用proxy cache,对应着配置的key_zone
- proxy_cache_valid 根据不同的状态码设置不同的缓存时间
upstream负载均衡
负载均衡是nginx的一大特点,可以配置多个服务器,将请求分发到最合适的那台服务器,避免某一台服务器请求太多而崩溃。使用upstream属性来配置
upstream project{
server 192.168.0.100:9000;
server 192.168.0.111:9000;
}
location / {
root html;
index index.html index.htm;
# 对应上方的 project
proxy_pass http://project;
}
weight权重
upstream project{
server 192.168.0.100:9000 weight=5;
server 192.168.0.111:9000 weight=10;
}
ip_hash
基于客户端的ip地址,采用hash算法计算下一个请求要选择哪一个服务器,这样固定的ip会访问同一个服务器,可以解决session问题
upstream project{
ip_hash;
server 192.168.0.100:9000;
server 192.168.0.111:9000;
}
least_conn最少链接
会将下一个请求分发到当前链接数最少的一台服务器
upstream project{
least_conn;
server 192.168.0.100:9000;
server 192.168.0.111:9000;
}
fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream project{
fair;
server 192.168.0.100:9000;
server 192.168.0.111:9000;
}
url_hash
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器
upstream backserver {
server 192.168.0.100:9000;
server 192.168.0.111:9000;
hash $request_uri;
hash_method crc32;
}