2-1
中间件是什么?
NGINX是一个开源且高性能,可靠的HTTP中间件,代理服务。
2-2
常见的HTTP服务:HTTPD,IIS,GWS
2-3/4/5/6
为什么选择nginx?
IO多路复用epoll:(多个描述符的I/O一个线程里并发交替的完成),实现并发和吞吐轻量级:功能模块少(出于性能考虑,源代码只保留核心代码),代码模块化(易读,可以二次改进,比如阿里的tenginx)CPU亲和:一个worker进程在一个CPU上进行sendfile:静态文件不需要过多的用户空间逻辑处理,可以直接内核空间进行传输。2-7
http://nginx.org下载安装包
2-8
安装目录的讲解
|-- client_body_temp |-- conf #这是Nginx所有配置文件的目录,极其重要 | |-- fastcgi.conf #fastcgi相关参数的配置文件 | |-- fastcgi.conf.default #fastcgi.conf的原始备份 | |-- fastcgi_params #fastcgi的参数文件 | |-- fastcgi_params.default | |-- koi-utf # 编码转换映射转化文件(很少用到) | |-- koi-win #同上 | |-- mime.types #媒体类型, | |-- mime.types.default | |-- nginx.conf #这是Nginx默认的主配置文件 | |-- nginx.conf.default | |-- scgi_params #scgi相关参数文件,一般用不到 | |-- scgi_params.default | |-- uwsgi_params #uwsgi相关参数文件,一般用不到 | |-- uwsgi_params.default | |-- win-utf # 编码转换映射转化文件(很少用到) |-- fastcgi_temp #fastcgi临时数据目录 |-- html #这是编译安装时Nginx的默认站点目录,类似 Apache的默认站点htdocs目录 | |--50x.html # 错误页面优雅替代显示文件,例如:出现502错误时会调用此页面 # error_page 500502503504 /50x.html; | |-- index.html # 默认的首页文件,首页文件名字是在nginx.conf中事先定义好的。 |-- logs #这是Nginx默认的日志路径,包括错误日志及访问日志 | |-- access.log # 这是Nginx的默认访问日志文件,使用tail -f access.log,可以实时观看网站用户访问情况信息 | |-- error.log # 这是Nginx的错误日志文件,如果Nginx出现启动故障等问题,一定要看看这个错误日志 | |-- nginx.pid # Nginx的pid文件,Nginx进程启动后,会把所有进程的ID号写到此文件 |-- proxy_temp #临时目录 |-- sbin #这是Nginx命令的目录,如Nginx的启动命令nginx | |-- nginx #Nginx的启动命令nginx |-- scgi_temp #临时目录 |-- uwsgi_temp #临时目录2-9
编译配置参数的讲解
nginx -V :会出现如下信息
-prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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
------------------------------------------------------------------------------------------------------------------------------
--http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 执行对应模块时,Nginx所保留的临时性文件 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp ------------------------------------------------------------------------------------------------------------------------------
--user=nginx
--group=nginx 设定Nginx的进程启动的用户和组用户
------------------------------------------------------------------------------------------------------------------------------
--with-cc-opt C语言编译 ------------------------------------------------------------------------------------------------------------------------------
--with-ld-opt= 设置附加的参数,链接系统库
2-10
默认配置基本语法
参考nginx配置文件nginx.conf超详细讲解
查看CPU核数:cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
子配置文件:
从上到下依次:
列 作用
listen 80; 监听的端口 server_name localhost; 用域名方式访问的地址
------------------------------------------------------------------------------------------------------------------------------
location / { 一个server里可以有多个location,当这是/的时候, root /usr/share/nginx/html; root是存放首页的路径 index index.html index.htm; 访问的页面,访问index.html } ------------------------------------------------------------------------------------------------------------------------------
error_page 500 502 503 504 404 /50x.html; 当访问错误的时候,会显示的页面 location = /50x.html { root /usr/share/nginx/html; root是存放页面的路径 }
2-11
默认配置和默认站点启动
2-12
HTTP 请求
安装curl:
yum install curl
curl -v www.baidu.com
2-13
日志:
access log 记录用户,页面以及用户浏览器,ip和其他的访问信息
85.14.245.124 - - [15/May/2019:12:14:02 +0000] "\x03\x00\x00/*\xE0\x00\x00\x00\x00\x0 0Cookie: mstshash=Administr" 400 157 "-" "-" 104.152.52.68 - - [15/May/2019:12:28:28 +0000] "GET / HTTP/1.0" 200 612 "-" "-" 187.11.247.100 - - [15/May/2019:15:19:10 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla /5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Vers ion/9.1.2 Safari/601.7.7" 193.188.22.116 - - [15/May/2019:15:29:09 +0000] "\x03\x00\x00/*\xE0\x00\x00\x00\x00\x 00Cookie: mstshash=Administr" 400 157 "-" "-"error log 记录服务器错误日志
2019/05/15 20:26:45 [error] 102452#0: *22 open() "/usr/local/nginx/html/cache/global/ img/gs.gif" failed (2: No such file or directory), client: 80.82.70.187, server: loca lhost, request: "GET http://www.baidu.com/cache/global/img/gs.gif HTTP/1.1", host: "w ww.baidu.com" 2019/05/15 21:29:41 [error] 102452#0: *24 open() "/usr/local/nginx/html/robots.txt" f ailed (2: No such file or directory), client: 124.43.17.15, server: localhost, reques t: "GET /robots.txt HTTP/1.1", host: "42.159.195.188" 2019/05/15 21:29:41 [error] 102452#0: *24 open() "/usr/local/nginx/html/Appbcc39f2a.p hp" failed (2: No such file or directory), client: 124.43.17.15, server: localhost, r equest: "POST /Appbcc39f2a.php HTTP/1.1", host: "42.159.195.188"2-14
vim nginx.conf
access日志格式配置:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '$upstream_addr $upstream_response_time $request_time '; access_log logs/access.log main;
#配置access log日志的存储位置及文件,注意:access.log文件是可以按日期进行分割的,方便查看及处理
access_log /usr/local/nginx/log/access.log main;相关说明解释 1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区; 4.$request : 用来记录请求的url与http协议; 5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小; 7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;
2-15
模块讲解:
官方模块
第三方模块
2-16
sub_status:NGINX的客户端状态