Nginx(发音同engine x)是一个异步框架的 Web服务器,也能够用做反向代理,负载平衡器 和 HTTP缓存。该软件由 Igor Sysoev 建立,并于2004年首次公开发布。同名公司成立于2011年,以提供支持。javascript
NGINX是免费,开源,高性能的HTTP和反向代理服务器,邮件代理服务器,通用TCP/UDP代理服务器php
Nginx在官方测试的结果中,可以支持五万个并行链接,而在实际的运做中,能够支持二万至四万个并行链接。css
Nginx 的编写有一个明确目标就是超越 Apache Web 服务器的性能。Nginx 提供开箱即用的静态文件,使用的内存比 Apache 少得多,每秒能够处理大约四倍于 Apache 的请求。低并发下性能与 Apache 至关,有时候还低于,可是在高并发下 Nginx 能保持低资源低消耗高性能。还有高度模块化的设计,模块编写简单。配置文件简洁。html
官网:http://nginx.org
java
文档:https://nginx.org/en/docs/
node
特性:python
功能:mysql
web服务功能:nginx
官方源:http://nginx.org/packages/centos/7/x86_64/
web
epel源:https://mirrors.aliyun.com/epel/7/x86_64/
安装:yum install nginx -y
默认主站点目录:/usr/share/nginx/html
主程序:/usr/sbin/nginx
# nginx 启动服务 # nginx -v|-V 查看版本 # nginx -t 检查配置文件 # nginx -c filename 指定配置文件(default: /etc/nginx/nginx.conf) # nginx -s signal 发送信号给master进程,signal:stop, quit, reopen, reload # nginx -g directives 在命令行中指明全局指令
主配文件:/etc/nginx/nginx.conf
子配文件:/etc/nginx/conf.d/*
配置文件格式:
main block:主配置段,即全局配置段,对http,mail都有效 event { ... } 事件驱动相关的配置 http { ... } http/https 协议相关配置段 mail { ... } mail 协议相关配置段 stream { ... } stream 服务器相关配置段
默认配置文件示例:
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; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
[root@centos7 ~]# yum install pcre-devel openssl-devel zlib-devel -y [root@centos7 ~]# useradd -r -s /sbin/nologin nginx [root@centos7 ~]# wget https://nginx.org/download/nginx-1.14.0.tar.gz [root@centos7 ~]# tar xf nginx-1.14.0.tar.gz [root@centos7 ~]# cd nginx-1.14.0/ [root@centos7 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx \ > --conf-path=/etc/nginx/nginx.conf \ > --error-log-path=/var/log/nginx/error.log \ > --http-log-path=/var/log/nginx/access.log \ > --pid-path=/var/run/nginx.pid \ > --lock-path=/var/run/nginx.lock \ > --user=nginx \ > --group=nginx \ > --with-http_ssl_module \ > --with-http_v2_module \ > --with-http_dav_module \ > --with-http_stub_status_module \ > --with-threads \ > --with-file-aio [root@centos7 nginx-1.14.0]# vim src/http/ngx_http_header_filter_module.c static u_char ngx_http_server_string[] = "Server: feijikesi" CRLF; [root@centos7 nginx-1.14.0]# vim src/core/nginx.h #define NGINX_VERSION "1.14.0" #define NGINX_VER "feijikesi/" NGINX_VERSION [root@centos7 nginx-1.14.0]# make && make install [root@centos7 ~]# echo 'PATH=/usr/local/nginx/sbin/:$PATH' > /etc/profile.d/nginx.sh [root@centos7 ~]# . /etc/profile.d/nginx.sh [root@centos7 ~]# nginx [root@centos7 ~]# curl -I 127.0.0.1 Server: feijikesi/1.14.0 [root@centos7 ~]# vim /etc/nginx/nginx.conf http { server_tokens off; } [root@centos7 ~]# nginx -s reload [root@centos7 ~]# curl -I 127.0.0.1 Server: feijikesi
帮助文档:http://nginx.org/en/docs/ngx_core_module.html
user:指定worker进程的运行身份,如组不指定,默认和用户名同名
pid /PATH/TO/PID_FILE:指定存储nginx主进程PID的文件路径
include file|mask:指明包含进来的其它配置文件片段
load_module file:
模块加载配置文件:/usr/share/nginx/modules/*.conf
指明要装载的动态模块路径:/usr/lib64/nginx/modules/*.so
worker_processes number | auto:worker进程的数量;一般应该为当前主机的cpu的物理核心数
worker_cpu_affinity cpumask ...:将worker进程绑定到指定CPU上,提升缓存命中率
cpumask: 00000001:0号CPU 00000010:1号CPU 10000000:8号CPU worker_cpu_affinity 0001 0010 0100 1000; 分别将worker进程绑定到1,2,3,4号CPU上
worker_priority number:指定worker进程的nice值,设定worker进程优先级:[-20-19]
worker_rlimit_nofile number:worker进程所可以打开的文件数量上限
events { worker_connections 1024; }
nginx高度模块化,但其模块早期不支持DSO机制;1.9.11版本支持动态装载和卸载
核心模块:core module
标准模块:
- HTTP 模块:ngx_http_*
- Mail 模块 ngx_mail_*
- Stream 模块 ngx_stream_*
第三方模块:
1)server { ... }:配置虚拟主机
# vim /etc/nginx/nginx.conf http { server { listen 80; server_name www.dongfei.tech; root /data/www/; } server { listen 80; server_name news.dongfei.tech; root /data/news/; } } # nginx 启动服务 # echo news site > /data/news/index.html # echo www site > /data/www/index.html
客户端测试:
# curl www.dongfei.tech www site # curl news.dongfei.tech news site
2)listen port|address[:port]|unix:/PATH/TO/SOCKET_FILE
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
listen PORT; 指令监听在不一样的端口,可实现基于端口的虚拟主机
listen IP:PORT; 监听 IP 地址不一样,实现基于IP的虚拟主机
3)server_name name ...
虚拟主机的主机名称后可跟多个由空白字符分隔的字符串
支持*通配任意长度的任意字符
server { listen 80; server_name *.dongfei.tech; root /data/default/; } # echo default > /data/default/index.html # nginx -s reload # curl xxx.dongfei.tech default
支持 ~ 起始的字符作正则表达式模式匹配,性能缘由慎用
server_name ~^www\d+\.dongfei\.tech$ #说明:\d 表示 [0-9]
匹配优先级机制从高到低:
(1) 首先是字符串精确匹配 如:www.dongfei.com
(2) 左侧*通配符 如:*.dongfei.tech
(3) 右侧*通配符 如:www.dongfei.*
(4) 正则表达式 如: ~^.*\.dongfei\.tech$
(5) default_server
4)tcp_nodelay on|off
在keepalived模式下的链接是否启用TCP_NODELAY选项
当为off时,延迟发送,合并多个请求后再发送
默认on时,不延迟发送
可用于:http, server, location
若是为了节约服务器性能能够打开,若是为了用户体验更好选择关闭
5)sendfile on|off
是否启用sendfile功能,在内核中封装报文直接发送,默认关闭
6)server_tokens on|off|build|string
是否在响应报文的Server首部显示nginx版本,建议关闭
7)root
设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用于http, server, location, if in location
8)location [ = | ~ | ~* | ^~ ] uri { ... }
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的全部location,并找出一个最佳匹配,然后应用其配置
# mkdir /data/www/blog/ # echo blog > /data/www/blog/index.html # vim /etc/nginx/nginx.conf server { listen 80; server_name www.dongfei.tech; root /data/www/; location /blog { root /data/www/; } } # curl http://www.dongfei.tech/blog/ #测试 blog
=:对URI作精确匹配
^~: 对URI的最左边部分作匹配检查,不区分字符大小写
~: 对URI作正则表达式模式匹配,区分字符大小写
~*: 对URI作正则表达式模式匹配,不区分字符大小写
不带符号:匹配起始于此uri的全部的uri
匹配优先级从高到低:=, ^~, ~/~*, 不带符号
例如:
location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] } The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.
9)alias
路径别名,文档映射的另外一种机制;仅能用于location上下文
server { listen 80; server_name www.dongfei.tech; root /data/www/; location /blog { alias /data/www/blog; #和root /data/www/;做用相同 } } # curl http://www.dongfei.tech/blog/ blog
注意:location中使用root指令和alias指令的意义不一样
(a) root,给定的路径对应于location中的/uri/左侧的/
(b) alias,给定的路径对应于location中的/uri/右侧的/
10)index file ...
指定默认网页文件,注意须要装载 ngx_http_index_module 模块
11)error_page code ... [=[response]] uri
定义错误页,以指定的响应状态码进行响应;可用位置:http, server, location, if in location
# echo "404 not found page" > /data/www/404.html server { listen 80; server_name www.dongfei.tech; root /data/www/; error_page 404 /404.html; } # curl http://www.dongfei.tech/notfound.html 404 not found page
server { listen 80; server_name www.dongfei.tech; root /data/www/; error_page 404 =200 /404.html; #将404返回码重定向成200访问码,防止浏览器劫持 } # curl -I http://www.dongfei.tech/notfound.html HTTP/1.1 200 OK #测试为200正确访问码
12)try_files file ... uri | =code
按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),若是全部的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数能够引发一个内部重定向,以前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,不然会出现内部500错误
# echo default page > /data/news/default.html server { listen 80; server_name news.dongfei.tech; root /data/news/; location / { try_files $uri /default.html; #若是用户访问的URI不存在则放回默认页面 } } # curl http://news.dongfei.tech/index.html news site # curl http://news.dongfei.tech/noindex.html default page
server { listen 80; server_name news.dongfei.tech; root /data/news/; location / { try_files $uri $uri/index.html $uri.html =404; } } # curl http://news.dongfei.tech/index.html news site # curl http://news.dongfei.tech/noindex.html 404 Not Found
13)keepalive_timeout timeout [header_timeout]
设定保持链接超时时长,0表示禁止长链接,默认为75s,可用于http, server, location
14)keepalive_requests number
在一次长链接上所容许请求的资源的最大数量,默认为100
15)keepalive_disable none | browser ...
对哪类型的浏览器禁用长链接
16)send_timeout time
向客户端发送响应报文的超时时长,此处是指两次写操做之间的间隔时长,而非整个响应过程的传输时长
17)client_body_buffer_size size
用于接收每一个客户端请求报文的body部分的缓冲区大小;默认为16k;
超出此大小时,其将被暂存到磁盘上的由下面 client_body_temp_path 指令所定义的位置
18)client_body_temp_path path [level1 [level2 [level3]]]
设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量
19)limit_rate rate
限制响应给客户端的传输速率,单位是bytes/second;默认值0表示无限制
20)limit_except method ... { ... }
限制客户端使用除了指定的请求方法以外的其它方法,仅用于location
method:GET(包括HEAD), HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH
location /upload { root /date/www/; limit_except GAT { allow 192.168.0.9/24; deny all; } } # 除了 GET和HEAD 以外其它方法仅容许192.168.0.9/24主机使用
21)aio on | off | threads[=pool]
是否启用aio功能
22)directio size | off
当文件大于等于给定大小时,例如directio 4m,同步到磁盘,而非写缓存,以防数据丢失
23)open_file_cache off | max=N [inactive=time]
24)open_file_cache_errors on | off
是否缓存查找时发生错误的文件一类的信息,默认值为off
25)open_file_cache_min_uses number
open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1
26)open_file_cache_valid time
缓存项有效性的检查频率,默认值为60s
基于ip的访问控制功能
server { listen 80 default_server; server_name www.dongfei.tech; root /data/www/; location / { allow 192.168.0.0/24; deny all; } }
自上而下检查,一旦匹配,将生效,条件严格的置前
实现基于用户的访问控制,使用basic机制进行用户认证
# mkdir /data/www/admin/ # echo admin area > /data/www/admin/index.html server { listen 80 default_server; server_name www.dongfei.tech; root /data/www/; location /admin { auth_basic "Admin Area"; auth_basic_user_file /etc/nginx/.ngxpasswd; } } # yum install httpd-tools -y # htpasswd -cm /etc/nginx/.ngxpasswd user1 # htpasswd -m /etc/nginx/.ngxpasswd user2 # nginx -s reload 浏览器访问:http://192.168.0.8/admin/ 测试
用于输出nginx的基本状态信息
server { listen 80 default_server; server_name www.dongfei.tech; root /data/www/; location /admin { auth_basic "Admin Area"; auth_basic_user_file /etc/nginx/.ngxpasswd; } location /status { stub_status; allow 192.168.0.0/24; deny all; } }
# curl http://192.168.0.8/status/ Active connections: 3 server accepts handled requests 35 35 34 Reading: 0 Writing: 1 Waiting: 2
Active connections:当前状态,活动状态的链接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的链接的链接数
Writing:当前状态,正在向客户端发送响应报文过程当中的链接数
Waiting:当前状态,正在等待客户端发出请求的空闲链接数
指定日志格式记录请求
1)log_format name string ...
string可使用nginx核心模块及其它模块内嵌的变量
2)access_log path [format [buffer=size][gzip[=level]][flush=time][if=condition]] 或 access_log off
http { log_format customlog '$remote_addr - $remote_user [$time_iso8601] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; server { listen 80 default_server; server_name www.dongfei.tech; root /data/www/; access_log /var/log/nginx/www.dongfei.tech-access.log customlog buffer=32k; } }
3)open_log_file_cache max=N [inactive=time][min_uses=N][valid=time]; 和 open_log_file_cache off;
缓存各日志文件相关的元数据信息
max:缓存的最大文件描述符数量
min_uses:在inactive指定的时长内访问大于等于此值方可被看成活动项
inactive:非活动时长
valid:验证缓存中各缓存项是否为活动项的时间间隔
用gzip方法压缩响应数据,节约带宽
1)gzip on | off; 启用或禁用gzip压缩
2)gzip_comp_level level; 压缩比由低到高:1 到 9 ,默认:1
3)gzip_disable regex ...; 匹配到客户端浏览器不执行压缩
4)gzip_min_length length; 启用压缩功能的响应报文大小阈值
5)gzip_http_version 1.0 | 1.1; 设定启用压缩功能时,协议的最小版本,默认:1.1
6)gzip_buffers number size; 支持实现压缩功能时缓冲区数量及每一个缓存区的大小,默认:32 4k 或 16 8k
7)gzip_types mime-type ...; 指明仅对哪些类型的资源执行压缩操做;即压缩过滤器,默认包含有text/html,不用显示指定,不然出错
8)gzip_vary on | off; 若是启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”
9)gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压缩功能
server { listen 80 default_server; server_name www.dongfei.tech; root /data/www/; gzip on; gzip_comp_level 6; gzip_min_length 64; gzip_proxied any; gzip_types text/xml text/css text/plain application/javascript; }
1)ssl on|off; 为指定虚拟机启用HTTPS protocol, 建议用listen指令代替
2)ssl_certificate file; 当前虚拟主机使用PEM格式的证书文件
3)ssl_certificate_key file; 当前虚拟主机上与其证书匹配的私钥文件
4)ssl_protocols [SSLv2][SSLv3][TLSv1][TLSv1.1][TLSv1.2]; 支持ssl协议版本,默认为后三个
5)ssl_session_cache off | none | [builtin[:size]][shared:name:size];
6)ssl_session_timeout time; 客户端链接能够复用ssl session cache中缓存的ssl参数的有效时长,默认5m
服务器配置示例:
[root@nginx ~]# cd /etc/pki/tls/certs/ [root@nginx certs]# vim Makefile %.key: umask 77 ; \ /usr/bin/openssl genrsa $(KEYLEN) > $@ [root@nginx certs]# make aa.crt Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:bj Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]:aa.com Organizational Unit Name (eg, section) []:opt Common Name (eg, your name or your server's hostname) []:www.aa.com [root@nginx certs]# make bb.crt Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:bj Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]:bb.com Organizational Unit Name (eg, section) []:opt Common Name (eg, your name or your server's hostname) []:www.bb.com [root@nginx certs]# mkdir /etc/nginx/conf.d/ssl/ [root@nginx certs]# mv aa.crt aa.key bb.crt bb.key /etc/nginx/conf.d/ssl/ [root@nginx ~]# vim /etc/nginx/conf.d/vhosts.conf server { listen 443 ssl; server_name www.aa.com; root /data/www/aa/; ssl_certificate /etc/nginx/conf.d/ssl/aa.crt; ssl_certificate_key /etc/nginx/conf.d/ssl/aa.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; } server { listen 443 ssl; server_name www.bb.com; root /data/www/bb/; ssl_certificate /etc/nginx/conf.d/ssl/bb.crt; ssl_certificate_key /etc/nginx/conf.d/ssl/bb.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; } [root@nginx ~]# mkdir -pv /data/www/{aa,bb} [root@nginx ~]# echo "aa test page" > /data/www/aa/index.html [root@nginx ~]# echo "bb test page" > /data/www/bb/index.html [root@nginx ~]# nginx
客户端测试:
[root@client ~]# vim /etc/hosts 192.168.0.8 www.aa.com www.bb.com [root@client ~]# curl -k https://www.aa.com/ aa test page [root@client ~]# curl -k https://www.bb.com/ bb test page
将用户请求的URI基于PCRE regex所描述的模式进行检查,然后完成重定向替换
1)rewrite regex replacement [flag]
将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI
若是在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会从新一轮的替换检查
隐含有循环机制,但不超过10次;若是超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制
若是replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端, 即永久重定向301
[flag]:
2)return
return code [text];
return code URL;
return URL;
中止处理,并返回给客户端指定的响应码
3)rewrite_log on | off;
是否开启重写日志, 发送至error_log(notice level)
4)set $variable value;
用户自定义变量;注意:变量定义和调用都要以$开头
5)if (condition) { ... }
条件知足时,执行配置块中的配置指令;server, location
condition:
实现http重定向到https
server { listen 80 default_server; listen 443 ssl; server_name www.aa.com; root /data/www/aa; ssl_certificate /etc/nginx/conf.d/ssl/aa.crt; ssl_certificate_key /etc/nginx/conf.d/ssl/aa.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; location / { if ( $scheme = http ) { rewrite / https://www.aa.com/ redirect; } } }
用来阻止Referer首部无有效值的请求访问,可防止盗链
valid_referers none|blocked|server_names|string ...; 定义referer首部的合法可用值,不能匹配的将是非法值
[root@nginx ~]# cp /usr/share/backgrounds/night.jpg /data/www/aa/ [root@nginx ~]# vim /data/www/bb/index.html bb test page <img src=http://www.aa.com/night.jpg> #bb.com网站盗链aa.com网站图片 [root@nginx ~]# vim /etc/nginx/conf.d/vhosts.conf server { listen 80; server_name www.aa.com; root /data/www/aa; valid_referers none block server_names *.aa.com ~\.aa\.; #只有从aa.com访问的才能够浏览 if ($invalid_referer) { return 403 http://www.aa.com; } } server { listen 80; server_name www.bb.com; root /data/www/bb/; } 如今访问www.bb.com没法获取aa.com的图片了
反向代理:转发请求至另外一台主机
1)proxy_pass URL; proxy_pass后面路径不带uri时,会将location的uri传递(附加)给后端主机
server { listen 80; server_name www.aa.com; location /admin { proxy_pass http://192.168.0.9/; #带" / " } } 访问http://www.aa.com时至关于访问 http://192.168.0.9/
server { listen 80; server_name www.aa.com; location /admin { proxy_pass http://192.168.0.9; #不带" / " } } 访问http://www.aa.com时至关于访问 http://192.168.0.9/admin
若是location定义其uri时使用了正则表达式的模式,则proxy_pass以后必须不能使用uri; 用户请求时传递的uri将直接附加至后端服务器以后
2)proxy_set_header field value; 设定发日后端主机的请求报文的请求首部的值
proxy_set_header X-Real-IP $remote_addr; #$remote_addr客户端IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #$proxy_add_x_forwarded_for代理IP
配合日志记录实现后端web服务器记录真正的客户端地址:
[root@nginx ~]# vim /etc/nginx/conf.d/vhosts.conf server { listen 80 default_server; server_name www.aa.com; location / { proxy_pass http://192.168.0.9/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
[root@nginx-1 ~]# vim /etc/nginx/nginx.conf listen 80; listen [::]:80; log_format mylogformat '$http_x_forwarded_for - $remote_user [$time_iso8601] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; [root@nginx-1 ~]# vim /etc/nginx/conf.d/vhosts.conf server { listen 80 default_server; server_name www.aa.com; root /data/www/aa/; access_log /var/log/nginx/www.aa.com-access.log mylogformat; } root@nginx-1 ~]# mkdir -pv /data/www/aa/ [root@nginx-1 ~]# echo www.aa.com test page on nginx-1 > /data/www/aa/index.html
[root@nginx-2 ~]# curl www.aa.com www.aa.com test page on nginx-1 [root@nginx-1 ~]# tail -f /var/log/nginx/www.aa.com-access.log 192.168.0.10 - - [2018-07-07T14:34:07+08:00] "GET / HTTP/1.0" 200 32 "-" "curl/7.29.0" "192.168.0.10" #显示的是真正的客户端的IP地址
3)proxy_cache_path; 定义可用于proxy功能的缓存,在http中定义
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
配置代理缓存示例:在http配置定义缓存信息
[root@nginx ~]# vim /etc/nginx/nginx.conf proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g; [root@nginx ~]# mkdir /var/cache/nginx/
proxycache:20m 指内存中缓存的大小,主要用于存放key和metadata
max_size=1g 指磁盘存入文件内容的缓存空间最大值
[root@nginx ~]# vim /etc/nginx/conf.d/vhosts.conf server { listen 80 default_server; server_name www.aa.com; location / { proxy_pass http://192.168.0.9/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; } } [root@nginx ~]# tree /var/cache/nginx/proxy_cache/ /var/cache/nginx/proxy_cache/ └── 9 └── d └── 7 └── 6666cd76f96956469e7be39d750cc7d9
4)proxy_cache zone|off; 默认off ,指明调用的缓存,或关闭缓存机制
5)proxy_cache_key string; 缓存中用于“键”的内容
默认值:proxy_cache_key $scheme$proxy_host$request_uri;
6)proxy_cache_valid [code ...] time; 定义对特定响应码的响应内容的缓存时长,定义在http{...}中
7)proxy_cache_use_stale; 在被代理的后端服务器出现哪一种状况下,能够真接使用过时的缓存响应客户端
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...
8)proxy_cache_methods GET | HEAD | POST ...; 对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法老是被缓存
9)proxy_hide_header field; 默认nginx在响应报文不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel-等,用于隐藏后端服务器特定的响应首部
10)proxy_connect_timeout time; 定义与后端服务器创建链接的超时时长,如超时会出现502错误,默认为60s,通常不建议超出75s
11)proxy_send_timeout time; 将请求发送给后端服务器的超时时长;默认为60s
12)proxy_read_timeout time; 等待后端服务器发送响应报文的超时时长,默认为60s
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值
1)add_header name value [always]; 添加自定义首部
add_header X-Via $server_addr; add_header X-Cache $upstream_cache_status; add_header X-Accel $server_name;
2)add_trailer name value [always]; 添加自定义响应信息的尾部
转发请求到FastCGI服务器,不支持php模块方式
1)fastcgi_pass address; address为后端的fastcgi server的地址;可用位置:location, if in location
2)fastcgi_index name; fastcgi默认的主页资源;fastcgi_index index.php;
3)fastcgi_param parameter value [if_not_empty]; 设置传递给 FastCGI服务器的参数值,能够是文本,变量或组合
4)fastcgi_cache_path path ... 定义fastcgi的缓存:
fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time][purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
path:缓存位置为磁盘上的文件系统
max_size=size:磁盘path路径中用于缓存数据的缓存空间上限
levels=levels:缓存目录的层级数量,以及每一级的目录数量
levels=ONE:TWO:THREE:示例:leves=1:2:2
keys_zone=name:size:k/v映射的内存空间的名称及大小
inactive=time:非活动时长
5)fastcgi_cache zone|off; 调用指定的缓存空间来缓存数据,可用位置:http, server, location
6)fastcgi_cache_key string; 定义用做缓存项的key的字符串,示例:fastcgi_cache_key $request_rui;
7)fastcgi_cache_methods GET | HEAD | POST ...; 为哪些请求方法使用缓存
8)fastcgi_cache_min_uses number; 缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认做活动项
9)fastcgi_keep_conn on | off; 收到后端服务器响应后,fastcgi服务器是否关闭链接,建议启用长链接
10)fastcgi_cache_valid [code ...] time; 不一样的响应码各自的缓存时长
示例:配置 lnmp
[root@lnmp ~]# yum install nginx php-fpm php-mysql mariadb -y [root@lnmp ~]# cp /etc/nginx/nginx.conf{,.bak} [root@lnmp ~]# vim /etc/nginx/conf.d/lnmp.conf server { listen 80 default_server; server_name www.dongfei.tech; root /data/www; location / { root /data/www; index index.php index.html; } location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www/$fastcgi_script_name; include fastcgi_params; } location ~* ^/(status|ping)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; include fastcgi_params; } } [root@lnmp ~]# vim /etc/php-fpm.d/www.conf pm.status_path = /status ping.response = pong ping.path = /ping [root@lnmp ~]# systemctl enable nginx.service php-fpm.service [root@lnmp ~]# systemctl start nginx.service php-fpm.service [root@lnmp ~]# mkdir /var/cache/nginx/ [root@lnmp ~]# mkdir -pv /data/www/ [root@lnmp ~]# vim /data/www/index.php <?php phpinfo(); ?>
测试: # curl 192.168.0.8/status #查看PHP的工做状态 pool: www process manager: dynamic start time: 07/Jul/2018:16:27:07 +0800 start since: 1463 accepted conn: 9 listen queue: 0 max listen queue: 0 listen queue len: 128 idle processes: 4 active processes: 1 total processes: 5 max active processes: 1 max children reached: 0 slow requests: 0 # curl 192.168.0.8/ping #测试PHP进程是否工做 pong 访问 http://192.168.0.8/
用于将多个服务器定义成服务器组,而由proxy_pass, fastcgi_pass等指令进行引用,实现健康检查,负载均衡的功能
1)upstream name { ... } 定义后端服务器组,会引入一个新的上下文,默认调度算法是wrr,在http中定义
2)server address [parameters]; 在upstream上下文中server成员,以及相关的参数
address:
parameters:
3)ip_hash 源地址hash调度方法
4)least_conn 最少链接调度算法,当server拥有不一样的权重时其为wlc,当全部后端主机链接数相同时,则使用wrr,适用于长链接
5)hash key [consistent]
基于指定的key的hash表来实现对请求的调度,此处的key能够直接文本、变量或两者组合
将请求分类,同一类请求将发往同一个upstream server,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用
hash $request_uri consistent; hash $remote_addr;
6)keepalive 链接数N:为每一个worker进程保留的空闲的长链接数量,可节约nginx端口,并减小链接管理的消耗
7)health_check [parameters]; 健康状态检测机制;只能用于location上下文,仅对nginx plus有效
8)match name { ... } 对backend server作健康状态检测时,定义其结果判断机制;只能用于http上下文,仅对nginx plus有效
示例:实现反向代理的负载均衡功能
web1:
[root@web1 ~]# yum install httpd -y [root@web1 ~]# echo web1 test page. > /var/www/html/index.html [root@web1 ~]# vim /etc/httpd/conf/httpd.conf LogFormat "%{client_ip}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined [root@web1 ~]# systemctl start httpd [root@web1 ~]# systemctl enable httpd
web2:
[root@web2 ~]# yum install httpd -y [root@web2 ~]# echo web2 test page. > /var/www/html/index.html [root@web2 ~]# vim /etc/httpd/conf/httpd.conf LogFormat "%{client_ip}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined [root@web2 ~]# systemctl start httpd [root@web2 ~]# systemctl enable httpd
proxy:
[root@proxy ~]# curl http://192.168.0.9/ web1 test page. [root@proxy ~]# curl http://192.168.0.10/ web2 test page. [root@proxy ~]# yum install nginx -y [root@proxy ~]# vim /etc/nginx/nginx.conf #在http中加入如下内容 http { upstream web { least_conn; server 192.168.0.9:80; server 192.168.0.10:80 weight=3; } proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g; server { location / { proxy_pass http://web; proxy_set_header client_ip $proxy_add_x_forwarded_for; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; } } } [root@proxy ~]# mkdir /var/cache/nginx/ [root@proxy ~]# nginx
在客户端测试时不会看到调度的效果,想要看到调度的效果须要把缓存关闭
感谢阅读!