文章目录
1. nginx软件的使用
1.1 nginx web服务应用
1.1.1 nginx介绍
-
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
-
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
-
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
-
特点:
-
解释同步与异步(nginx基于异步,Apache基于同步)
-
作为web服务软件
- 反向代理或负载均衡服务
-
1.1.1.1 为什么nginx总体性能比Apache高?
- nginx使用最新的epoll(linux2.6内核)和kqueue(freebsd)异步网络I/O模型,而Apache则使用传统的select模型。目前Linux下能够承受高并发访问的squid、memcached软件都采用的是epoll模型。
- 处理大量连接的读写,Apache所采用的select网络I/O模型比较低效。下面用两个通俗的比喻来解释Apache采用的select模型和nginx采用的epoll模型之间的区别。
-
第一个比喻:
- 第二个比喻:
-
Apache select 和nginx epoll区别技术对比
-
如何正确选择web服务器?
- 统一
- 批量管理----环境统一—符合一个标准----标准化----自动化维护 运维
- 工作中多用的是nginx
-
开始安装nginx
-
下载:nginx.org官网下载
- 如果nginx版本的第二数是奇数就是测试版本如:nginx-1.13.2
- 如果nginx版本的第二数是偶数就是测试版本如:nginx-1.12.0
- wget下载
-
环境准备
下载包放入特定的下载目录 mkdir /Script cd /Script #目录是不确定的
-
-
编译安装nginx
-
依赖包 需要配合 pcre(perl语言兼容的正则表达式 rewrite) openssl(https)
yum install pcre-devel openssl-devel -y rpm -qa | egrep 'pcre|openssl' #检查
-
解压编译安装
tar xf nginx-1.10.3.tar.gz cd nginx-1.10.3
-
添加nginx运行用户 www(用户按照实际情况创建,如果不执行默认nginx用户运行)
useradd -s /sbin/nologin -M www
-
编译
###./configure(配置 编译参数) make(编译) make install(复制 创建目录 文件) ##--prefix指定软件安装的位置(默认安装到/usr/local) ##--with-http_stub_status_module nginx的状态模块 ##--with-http_ssl_module nginx支持HTTPS的模块 #没有error报错,就没报错或者echo $?为0就没报错 ./configure --user=www --group=www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module echo $? make echo $? make install make clean #如果需要重新编译,需要先清除 ln -s /application/nginx-1.8.1/ /application/nginx #做软连接
/application/nginx/sbin/nginx #启动nginx lsof -i:80 #检查是否启动
-
- 回顾浏览器打开
- 把本机IP加入真实电脑本地hosts,然后用浏览器打开本机IP
2.nginx启动的疑难杂症汇总:
-
问题1:启动nginx报错nginx:[emerg] getpwnam(“nginx/www”) failed
##用户不存在 添加用户 useradd -s /sbin/nologin -M www
-
问题2:编译安装pcre编译软件gcc不全导致报错(这里使用yum安装不存在此问题)
yum install pcre-devel openssl-devel -y yum -y install gcc
-
问题3:如何查看nginx编译时的参数。
-
问题4:浏览器、wget或者curl等软件访问不了nginx页面
是否将本机IP加入真实本地hosts中
- web 排错三部曲下面介绍客户端排查的思路。
3.nginx技术深入剖析
-
nginx的目录结构说明
tree nginx/ conf ###nginx配置文件 nginx.conf ###nginx.conf nginx主要配置文件 nginx.conf ###备份 mime.types ###媒体类型 http协议中的文件类型 fastcgi.conf ###fastcgi的配置文件 nginx软件与其他动态软件配合,搭建网站博客会用到 html ##nginx默认的网站目录 站点目录 网站的根目录 www.oldboyedu.com/index.html log ###nginx的日志目录 access.log ##nginx的访问日志 sbin ##nginx的管理命令目录
-
nginx主配置文件nginx.conf
egrep -v "#|^$" nginx.conf.default >nginx.conf
##修改配置文件 serve_name www.etiantian.org;
/application/nginx/sbin/nginx -t #检查语法 /application/nginx/sbin/nginx -s reload #重启nginx
- nginx其他的配置文件
4. nginx虚拟主机配置实战
-
虚拟主机概念和类型介绍
-
配置第一个小网站
1. 修改nginx.conf配置文件 nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.yihutu.com; location / { root html; index index.html index.htm; } } curl IP/index.html #访问
修改站点文件 /application/nginx/html/index.html #修改网站需要重启,修改站点目录文件不需要重启
-
curl IP
curl -v IP [root@moban ~]# curl -v 192.168.152.150 * About to connect() to 192.168.152.150 port 80 (#0) * Trying 192.168.152.150... connected * Connected to 192.168.152.150 (192.168.152.150) port 80 (#0) > GET / HTTP/1.1 #>是http的请求,请求版本是HTTP/1.1协议 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 #值curl命令 > Host: 192.168.152.150 #要找的主机 > Accept: */* > < HTTP/1.1 200 OK #<是响应,状态是200 < Server: nginx/1.10.3 #nginx软件版本 < Date: Thu, 23 Nov 2017 03:21:28 GMT < Content-Type: text/html < Content-Length: 612 < Last-Modified: Mon, 20 Nov 2017 21:26:37 GMT < Connection: keep-alive < ETag: "5a13488d-264" < Accept-Ranges: bytes <
-
问题
- 尽量不要用win10默认浏览器
- 注意浏览器缓存
- 忘记配置hosts解析
-
nginx命令语法
1. -t 检查语法 2. -s reload 优雅的重启 3. -s stop 关闭 4. /sbin/nginx 启动 检查 ps -ef | grep '80' lsof -i:80
-
总结
1.基础 pv uv ip 静态页面 动态页面 nginx与Apache的区别 2. nginx 安装 配置文件 listen serve_name root index
-
预习
1. nginx 多个网站 多个虚拟主机 2. nginx配置 3. location 4. rewrite 5. 查看nginx状态
-
- 虚拟主机—一个网站—一个server标签{}
-
基于域名的虚拟主机配置实战
多个server标签、 1.网站目录 www.etiantian.org #站点html/www blog.etiantian.org #站点html/blog bbs.etiantian.org #站点html/bbs mkdir /application/nginx/html{www,blog,bbs} -p 2. 首页文件 index.html for name in www bbs blog;do echo $name $hostname>/application/nginx/html/$name/index.html;done for name in www bbs blog;do cat $hostname>/application/nginx/html/$name/index.html;done
修改nginx配置文件 cp nginx.conf{,.ori.www.only} vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.yihutu.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name blog.yihutu.com; #修改域名 location / { root html/blog; #修改站点目录 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name bbs.yihutu.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name blog.yihutu.com; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
1. 检查语法 2. 重启nginx 3.检查结果 修改Windows解析,然后浏览器打开测试
- 总结:多个虚拟主机 多个网站
- 多个网站的目录 html/{www,bbs,blog}
- 配置文件 域名 server_name root网站的根目录
- 总结:多个虚拟主机 多个网站
-
基于端口的虚拟主机配置实战
#一般给自己人用,不开放到外界 # 不要和其他端口冲突 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80:80; #修改端口 server_name www.yihutu.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
nginx www.yihutu.com 访问过程(重要) 1. nginx会看端口 2. 你要的域名 看看是否有你要的域名 3. 如果没有就会找这个端口的第一个
-
基于IP的虚拟主机配置实战
-
nginx配置虚拟主机的步骤
-
企业场景中重启nginx后的检测策略
5. nginx常用功能配置实战
-
规范化nginx配置文件
# 多个网站的情况,单独放server标签 mkdir extra #创建存放标签的目录 vim /extra/www ##放入/extra/www.conf server { listen 80; server_name www.yihutu.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } vim /extra/blog.conf ##放入/extra/blog.conf server { listen 80; server_name blog.yihutu.com; location / { root html/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } vim /extra/bbs.conf ##放入/extra/bbs.conf server { listen 80; server_name bbs.yihutu.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
修改nginx.conf cp nginx.conf{,.bak} worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; #尽量www放在第一个 include extra/blog.conf; include extra/bbs.conf; }
1. 检查语法 2. 重启
-
nginx虚拟主机的别名配置
www.baidu.com 别名baidu.com vim /extra/bbs.conf server { listen 80; server_name bbs.yihutu.com yihutu.com; #yihutu.com就是别名 location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 注意:Windows解析 hosts也要加上yihutu.com
-
nginx状态信息功能实战
vim /extra/status.conf server { listen 80; server_name status.yihutu.com; #监控状态 location / { stub_status on; ###开启nginx状态信息 access_log off; ###不记录访问日志 } } sed -i.bak.status '/include.*blog/a include extra/status.conf;' /application/nginx/conf/nginx.conf ----------------------------------------- worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/blog.conf; include extra/bbs.conf; include extra/status.conf; } ---------------------------------------------- 在Windows上添加hosts解析 status.yihutu.com
-
如何查看nginx的并发请求
1. nginx 状态模块 Active connections:4 2. ss -ant| grep -ic estab #查看已经建立的连接
-
问题
设置别名的时候不要配置重复的域名,会造成域名冲突
-
-
为nginx增加错误日志(error_log)配置/nginx访问日志(access_log)
-
规定 定义与使用
默认nginx.conf配置文件就有,用的时候在配置文件上加上 规定一个日志格式 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;
##nginx.conf主配置文件 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #直接在这里加上 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; include extra/www.conf; include extra/blog.conf; include extra/bbs.conf; include extra/status.conf; } --------------------------------------------------------------- server标签配置文件 server { listen 80; server_name bbs.yihutu.com yihutu.com; #yihutu.com就是别名 location / { root html/bbs; index index.html index.htm; } access_log logs/access_www.log main; access_log logs/access_www.log main gzip puffer=32k flush=5s; ##gzip压缩 puffer=32k放入内存32k flush=5s每隔5秒刷新 }
1. 检查语法 2. 重启 3. tailf /nginx/log/access.log
-
nginx日志 每一列的含义
$remote_addr ##远端地址 客户端IP地址 用户的ip地址(可以统计用的IP地址) $remote_user ##远端的用户 基本为空 $time_local ##当前服务器的时间 $request ##请求的起始行 GET / HTTP/1.1 ------------------------- GET / HTTP/1.1 请求方法 uri http协议版本 uri 和 url uri 统一资源标识符号 每个资源 文件的位置 url 统一资源定位符号 uri 范围很大 包含url nginx web服务器中 url:http://www.yihutu.com/upload/1.zip uri:/upload/1.zip ##在nginx服务器中uri表示网址后面第一个/到结尾的所有 ---------------------------- $status ##状态码 服务端http响应里面的状态码 $body_bytes_sent ##服务端给客户端发送的数据的大小 $http_referer ##记录着用户通过谁找到我的网站 ----------------------------------------------- www.baidu.com 先去百度 搜索老男孩教育 ##通过百度 找到我们的网站 www.oldboyedu.com ------------------------------------------------ $http_user_agent ##客户使用的浏览器 访问你网站的工具 $http_x_forwarded_for ##待补
curl -I 10.0.0.8 ##显示响应头的信息
-
状态码
304 not modified 没有修改 读取的是浏览器的缓存
-
日志的切割或轮询
##脚本每天定时执行 mv access.log access.log.$(date +%F) nginx -s reload ##logrotate切割轮询 系统自带的命令 (待补) cat /etc/logrotate.d/syslog
-
注意
rm -f access.log #硬链接数为零 进程调用数不为零 lsof | grep delete nginx -s reload ##解决 lsof | grep delete
1. location rewrite(etiantian.org ---->www.etiantian.org) 2. 搭建一个网站 博客网站 WordPress nginx 1.10.3 mysql 5.6.34 php 5.5.32 lnmp环境 3. 把数据库迁移到db01 4. 把用户上传的文件挂载到nfs01 需要:web01 db01 nfs01
-