一 nginx介绍
nginx是一个开源且高性能、牢靠的HTTP中间件和代理服务器
劣势:
- IO多路复用 多个描述符的IO操作都能在一个线程里并发交替程序实现,复用线程
nginx 通过 多过程 + io多路复用(epoll) 实现了高并发。采纳多个worker 过程实现对 多cpu 的利用。通过eopll 对多个文件描述符事件回调机制和就绪描述符的解决,实现单线程io复用,从而实现高并发 - CPU亲和 一种把CPU外围和Nginx工作过程绑定形式,把每个worker过程固定在一个CPU上执行,缩小切换CPU和提交缓存命中率,取得更好的性能。
- sendfile 零拷贝传输模式 ![usercore]
二 学习环境
1 操作系统
CENTOS>=7.0,位数 X64 CENTOS 7.2
2 环境确认
2.1 敞开 iptables
iptables命令是Linux上罕用的防火墙软件
- 进行防火墙 systemctl stop firewalld.service
- 永恒敞开防火墙 systemctl disable firewalld.service
2.2.2 确认停用 selinux
- 平安增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个平安子系统。
- SELinux 次要作用就是最大限度地减小零碎中服务过程可拜访的资源(最小权限准则)。
性能 | 命令 |
---|
查看状态 | getenforce |
查看状态 | /usr/sbin/sestatus -v |
长期敞开 | setenforce 0 |
永恒敞开 | /etc/selinux/config SELINUX=enforcing改为SELINUX=disabled |
2.2.3 装置依赖模块
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automakeyum -y install wget httpd-tools vim
三 nginx装置
1 版本分类
- Mainline version 开发版
- Stable version 稳定版
- Legacy versions 历史版本
2 下载地址
3 CentOS下YUM装置
/etc/yum.repos.d/nginx.repo
[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1
yum install nginx -ynginx -vnginx -V
四 配置文件
- /etc/nginx/nginx.conf
- /etc/nginx/conf.d/*.conf /etc/nginx/conf.d/default.conf
1 全局配置
分类 | 配置项 | 作用 |
---|
全局 | user | 设置nginx服务的零碎应用用户工作过程数,个别和CPU数量雷同 |
全局 | worker_processes | 工作过程数,个别和CPU数量雷同 |
全局 | error_log | nginx的谬误日志 |
全局 | pid | nginx服务启动时的pid |
2 服务配置
分类 | 配置项 | 作用 | events |
---|
events | worker_connections | 每个过程容许的最大连接数 10000 |
events | use | 指定应用哪种模型(select/poll/epoll),倡议让nginx主动抉择,linux内核2.6以上个别能应用epoll,进步性能。 |
/etc/nginx/nginx.conf
user nginx; 设置nginx服务的零碎应用用户 worker_processes 1; 工作过程数,个别和CPU数量雷同 error_log /var/log/nginx/error.log warn; nginx的谬误日志 pid /var/run/nginx.pid; nginx服务启动时的pidevents { worker_connections 1024;每个过程容许的最大连接数 10000}http { include /etc/nginx/mime.types;//文件后缀和类型类型的对应关系 default_type application/octet-stream;//默认content-type 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;//启用sendfile #tcp_nopush on;//懒发送 keepalive_timeout 65;//超时工夫是65秒 #gzip on;gzip压缩 include /etc/nginx/conf.d/*.conf;//蕴含的子配置文件}
default.conf
server { listen 80; //监听的端口号 server_name localhost; //用域名形式拜访的地址 #charset koi8-r; //编码 #access_log /var/log/nginx/host.access.log main; //拜访日志文件和名称 location / { root /usr/share/nginx/html; //动态文件根目录 index index.html index.htm; //首页的索引文件 } #error_page 404 /404.html; //指定谬误页面 # redirect server error pages to the static page /50x.html # 把后盾谬误重定向到动态的50x.html页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # 代理PHP脚本到80端口上的apache服务器 #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 把PHP脚本9000端口上监听的FastCGI服务 #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # 不容许拜访.htaccess文件 #location ~ /\.ht { # deny all; #}}
五 启动和从新加载
systemctl restart nginx.servicesystemctl reload nginx.servicenginx -s reload
六 日志
1 日志类型
- access_.log 拜访日志
- error.log 谬误日志
2 log_format
类型 | 用法 |
---|
语法 | log_format name [escape=default[json] string] |
默认 | log_format combined ... |
context | http |
案例
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; log_format zfpx '$arg_name $http_referer sent_http_date"'; access_log /var/log/nginx/access.log main; 221.216.143.110 - - [09/Jun/2018:22:41:18 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-"
3 HTTP申请变量
名称 | 含意 | 例子 |
---|
arg_PARAMETER | 申请参数 | $arg_name |
http_HEADER | 申请头 | $http_referer |
sent_http_HEADER | 响应头 | sent_http_cookie |
4内置变量
名称 | 含意 |
---|
$remote_addr | 客户端地址 |
$remote_user | 客户端用户名称 |
$time_local | 拜访工夫和时区 |
$request | 申请的URI和HTTP协定 |
$http_host | 申请地址,即浏览器中你输出的地址(IP或域名) |
$status | HTTP申请状态 |
$body_bytes_sent | 发送给客户端文件内容大小 |
七 nginx实战
5动态资源Web服务
- 动态资源:个别客户端发送申请到web服务器,web服务器从内存在取到相应的文件,返回给客户端,客户端解析并渲染显示进去。
- 动静资源:个别客户端申请的动静资源,先将申请交于web容器,web容器连贯数据库,数据库解决数据之后,将内容交给web服务器,web服务器返回给客户端解析渲染解决。
类型 | 品种 |
---|
浏览器渲染 | HTML、CSS、JS |
图片 | JPEG、GIF、PNG |
视频 | FLV、MPEG |
下载文件 | Word、Excel |
6 配置语法
1 属性
名称 | 语法 | 默认 | 上下文 | 形容 |
---|
sendfile | sendfile on / off | sendfile off; | http,server,location,if in location | 不通过用户内核发送文件 | |
tcp_nopush | tcp_nopush on / off | tcp_nopush off; | http,server,location | 在sendfile开启的状况 下,进步网络包的传输效率 |
tcp_nodelay | tcp_nodelay on / off | tcp_nodelay on; | http,server,location | 在keepalive连贯下,进步网络包的传输实时性 |
gzip | gzip on / off | gzip off; | http,server,location | 压缩文件能够节约带宽和进步网络传输效率 |
tcp_nodelay | tcp_nodelay on / off | tcp_nodelay on; | http,server,location | 在keepalive连贯下,进步网络包的传输实时性 |
gzip | gzip on / off | gzip off; | http,server,location | 压缩文件能够节约带宽和进步网络传输效率 |
gzip_comp_level | gzip_comp_level level | gzip_comp_level 1; | http,server,location | 压缩比率越高,文件被压缩的体积越小 |
gzip_http_version | gzip_http_version 1.0/1.1 | | gzip_http_version 1.1; | http,server,location |
压缩HTTP版本 |
http_gzip-static_module | gzip_static on/off | gzip_static off; | http,server,location | 先找磁盘上找同名的.gz 这个文件是否存在,节约CPU的压缩工夫和性能损耗 |
示例
location ~ .*\.(jpg|png|gif)$ { gzip off; gzip_http_version 1.1; gzip_comp_level 3; gzip_types image/jpeg image/png image/gif; root /data/images; } location ~ .*\.(html|js|css)$ { gzip on; gzip_min_length 1k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/css application/javascript; root /data/html; } location ~ ^/download { gzip_static on; tcp_nopush on; root /data/download; }
7 浏览器缓存
校验本地缓存是否过期
类型 | 品种 |
---|
测验是否过期 | Expires、Cache-Control(max-age) |
Etag | Etag |
Last-Modified | Last-Modified |
1 expires
增加Cache-Control、Expires头
类型 | 品种 |
---|
语法 | expires time |
默认 | expires off |
上下文 | http server location |
location ~ .*\.(jpg|png|gif)$ { expires 24h;}
2 跨域
类型 | 品种 |
---|
语法 | dd_header name value |
默认 | add_header --; |
上下文 | http server location |
location ~ .*\.json$ { add_header Access-Control-Allow-Origin http://localhost:3000; add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; root /data/json; }
8 代理服务
1 反向代理
反向代理的对象是服务端
resolver 8.8.8.8;location ~ ^/api { proxy_pass http://127.0.0.1:3000;}
2 正向代理
正向代理的对象是客户端
location / { proxy_pass http://$http_host$request_uri; }
9 负载平衡
- 应用集群是网站解决高并发、海量数据问题的罕用伎俩。
- 当一台服务器的解决能力、存储空间有余时,不要希图去换更弱小的服务器,对大型网站而言,不论如许弱小的服务器,都满足不了网站持续增长的业务需要。
- 这种状况下,更失当的做法是减少一台服务器分担原有服务器的拜访及存储压力。通过负载平衡调度服务器,将来自浏览器的拜访申请散发到应用服务器集群中的任何一台服务器上,如果有更多的用户,就在集群中退出更多的应用服务器,使应用服务器的负载压力不再成为整个网站的瓶颈。
1 upstream
upstream zfpx { ip_hash; server localhost:3000; server localhost:4000; server localhost:5000;}server { location / { proxy_pass http://zfpx; }
2 后端服务器调试状态
状态 | 形容 |
---|
down | 不参加负载平衡 |
backup | 备份的服务器 |
max_fails | 容许申请失败的次数 |
fail_timeout | 通过max_fails失败后,服务暂停的工夫 |
max_conns | 限度最大的接管的连接数 |
upstream zfpx { server localhost:3000 down; server localhost:4000 backup; server localhost:5000 max_fails=1 fail_timeout=10s;}
3 调配形式
类型 | 品种 |
---|
轮询(默认) | 每个申请按工夫程序逐个调配到不同的后端服务器,如果后端服务器down掉,能主动剔除。 |
weight(加权轮询) | 指定轮询几率,weight和拜访比率成正比,用于后端服务器性能不均的状况。 |
ip_hash | 每个申请按拜访ip的hash后果调配,这样每个访客固定拜访一个后端服务器,能够解决session的问题。 |
url_hash(第三方) | 按拜访的URL地址来调配 申请,每个URL都定向到同一个后端 服务器上(缓存) |
fair(第三方) | 按后端服务器的响应工夫来调配申请,响应工夫短的优先调配。 |
least_conn | 最小连接数,哪个连贯少就分给谁 | |
自定义hash | hash自定义key |
10缓存
http{ proxy_cache_path /data/nginx/tmp-test levels=1:2 keys_zone=tmp-test:100m inactive=7d max_size=1000g; }
- proxy_cache_path 缓存文件门路
- levels 设置缓存文件目录档次;levels=1:2 示意两级目录
- keys_zone 设置缓存名字和共享内存大小
- inactive 在指定工夫内没人拜访则被删除
- max_size 最大缓存空间,如果缓存空间满,默认笼罩掉缓存工夫最长的资源。
location /tmp-test/ { proxy_cache tmp-test; proxy_cache_valid 200 206 304 301 302 10d; proxy_cache_key $uri; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8081/media_store.php/tmp-test/; }
- proxy_cache tmp-test 应用名为tmp-test的对应缓存配置
- proxy_cache_valid 200 206 304 301 302 10d; 对httpcode为200…的缓存10天
- proxy_cache_key $uri 定义缓存惟一key,通过惟一key来进行hash存取
- proxy_set_header 自定义http header头,用于发送给后端实在服务器。
- proxy_pass 指代理后转发的门路,留神是否须要最初的/