1. Nginx 服务概述html
1.1 Nginx 服务简介前端
Nginx 是一个开源的,支持高性能、高并发的 WWW 服务和代理服务软件,它是由俄罗斯人 Igor Sysoev 开发的。Nginx 因具备高并发(特别是静态资源)、占用系统资源少等特性,且功能丰富而逐渐流行起来。linux
1.2 Nginx HTTP 服务器特点及优势nginx
① 支持高并发:能支持几万并发链接(特别是静态小文件业务环境);vim
② 资源消耗少:在 3 万并发链接下,开启 10 个 Nginx 线程消耗的内存不到 200M ;windows
③ 能够作 HTTP 反向代理,即负载均衡功能,内置对 RS 节点服务器健康检查功能,这至关于专业的 Haproxy 软件或 LVS 功能;后端
④ 具有 Squid 等专业缓存软件等的缓存功能;浏览器
⑤ 支持异步网络 I/O 事件模型 epoll 。缓存
1.3 Nginx 软件的主要企业功能应用安全
① 做为 Web 服务软件;
② 反向代理或负载均衡服务;
③ 前端业务数据缓存服务;
提示:Nginx 的以上三大功能是国内使用 Nginx 的主要场景,特别是前两个。
1.4 Nginx Web 服务介绍
Nginx 安装简单,配置文件简洁,并且配置灵活。近两年来,Nginx 在国内互联网领域的使用日趋火热,愈来愈多的网络开始使用,如淘宝、阿里、京东、小米、网易、新浪、赶集网等。
1.5 Nginx 做为 Web 服务器的主要应用场景
① 使用 Nginx 运行 HTML、JS、CSS、小图片等静态数据(此功能相似 Lighttpd 软件);
② Nginx 结合 FastCGI 运行 PHP 等动态程序(例如使用 fastcgi_pass 方式);
③ Nginx 结合 Tomcat/Resin 等支持 Java 动态程序(经常使用 proxy_pass 方式)。
1.6 Nginx 软件的特色
① 基于异步网络 I/O 模型(epoll、kqueue);
② 具有支持高性能,高并发的特性,并发链接可达数万;
③ 对小文件(小于 1M 的静态文件)高并发支持很好,性能很高;
④ 不支持相似 Apache 的 DSO 模式,扩展库必须编译进主程序(缺点);
⑤ 进程占用资源比较低;
⑥ 支持 Web 、反向 Proxy、Cache 三大重点功能,而且都很优秀;
⑦ 市场份额在逐年快速增加。
1.7 Lighttpd 的特色
① 基于异步网络 I/O 模型,性能、并发都与 Nginx 相近;
② 扩展库是 SO 模式,比 Nginx 灵活;
③ 目前国内的使用率比较低,安全性没有 Apache 和 Nginx 好;
④ 经过插件(mod_secdownload)可实现文件 URL 地址加密(优势);
⑤ 社区不灵活,市场份额较低。
1.8 为何 Nginx 整体性能比 Apache 高
Nginx 使用最新的 epoll (Linux 2.6 内核)和 kqueue (freebsd)异步网络 I/O 模型;Apache 使用的是传统的 select 模型。
提示:目前 Linux 下可以承受高并发访问的 Squid、Memcached 软件采用的都是 epoll 模型。
1.9 Apache select 和 Nginx epoll 的技术对比以下表
指标 |
select |
epoll |
性能 |
随链接数增长性能急剧降低 |
链接数增长性能不降 |
链接数 |
限制链接数不超过1024,不然要改配置。 |
链接数无限制 |
内在处理机制 |
线性轮询 |
回调 callback |
开发复杂性 |
低 |
中 |
2. 企业中如何正确选择 Web 服务
① 静态业务:如果高并发场景,尽可能采用 Nginx 或 Lighttpd ,两者首选 Nginx ;
② 动态业务:理论上采用 Nginx 和 Apache 都可,建议选择 Nginx ,为了不相同业务的服务软件多样化,增长额外维护成本。动态业务能够由 Nginx 兼作前端代理,再根据页面元素的类型或目录,转发到后端相应的服务器进行处理。
③ 既有静态业务又有动态业务:采用 Nginx 。
切记:在知足需求的前提下选择本身最擅长的软件避免给企业带来灾难性的损失。
3. 基于 CentOS 6.8 安装 Nginx 服务
3.1 检查并安装 Nginx 基础依赖包
[root@lnmp02 ~]# rpm -qa pcre pcre-devel
pcre-7.8-7.el6.x86_64 # pcre 包已经存在。
[root@lnmp02 ~]# yum install pcre-devel -y # 安装 pcre-devel 包。
[root@lnmp02 ~]# yum install openssl-devel -y # 安装 openssl-devel 包。
[root@lnmp02 tools]# rpm -qa pcre pcre-devel openssl openssl-devel # 检查依赖包。
openssl-devel-1.0.1e-57.el6.x86_64
pcre-7.8-7.el6.x86_64
openssl-1.0.1e-57.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
3.2 建立 Nginx 服务的用户
[root@lnmp02 nginx-1.6.3]# useradd nginx -s /sbin/nologin -M
[root@lnmp02 nginx-1.6.3]# id nginx
uid=501(nginx) gid=501(nginx) groups=501(nginx)
3.3 安装 Nginx 服务
[root@lnmp02 ~]# rpm -qa nginx # 查看是否已经安装。
[root@lnmp02 ~]# mkdir -p /home/oldboy/tools # 建立安装目录。
[root@lnmp02 ~]# cd /home/oldboy/tools
[root@lnmp02 tools]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz # 下载软件包。
[root@lnmp02 tools]# ls nginx-1.6.3.tar.gz # 查看下载的软件包。
nginx-1.6.3.tar.gz
[root@lnmp02 tools]# ls nginx-1.6.3.tar.gz -lk # 查看下载的软件包(787 K)。
-rw-r--r-- 1 root root 787 Apr 8 2015 nginx-1.6.3.tar.gz
[root@lnmp02 tools]# tar xf nginx-1.6.3.tar.gz # 解压下载的软件包。
[root@lnmp02 tools]# cd nginx-1.6.3
[root@lnmp02 nginx-1.6.3]# ls # 查看该软件内容。
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@lnmp02 nginx-1.6.3]# tree|wc -l # 一共 404 行。
404
[root@lnmp02 nginx-1.6.3]# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
# 编译软件(可以使用 ./configure --help 查看相关帮助信息)。
[root@lnmp02 nginx-1.6.3]# echo $?
0
[root@lnmp02 nginx-1.6.3]# make
[root@lnmp02 nginx-1.6.3]# make install # 安装。
[root@lnmp02 nginx-1.6.3]# cd ..
[root@lnmp02 tools]# ll -ld /application/nginx-1.6.3/
drwxr-xr-x 6 root root 4096 Dec 18 22:40 /application/nginx-1.6.3/
[root@lnmp02 tools]# ln -s /application/nginx-1.6.3/ /application/nginx
# 建立软连接(方便使用、更新时只需重建软连接、引用该软连接的地方无需改动)。
[root@lnmp02 tools]# ls -l /application/
total 4
lrwxrwxrwx 1 root root 25 Dec 18 22:44 nginx -> /application/nginx-1.6.3/
drwxr-xr-x 6 root root 4096 Dec 18 22:40 nginx-1.6.3
[root@lnmp02 tools]# /application/nginx/sbin/nginx # 启动 Nginx 服务。
[root@lnmp02 tools]# ss -lntup|grep nginx # 查看 Nginx 服务的端口。
tcp LISTEN 0 128 *:80 *:* users:(("nginx",4154,6),("nginx",4155,6))
3.4 启动命令详解
3.4.1 参数说明
[root@lnmp02 conf]# /application/nginx/sbin/nginx -h
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit # 查看编译的参数。
-t : test configuration and exit # 检查语法。
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload # 指定信号处理。
-p prefix : set prefix path (default: /application/nginx-1.6.3/)
-c filename : set configuration file (default: conf/nginx.conf) # 指定配置文件。
-g directives : set global directives out of configuration file
3.4.2 启动(或重启)Nginx 前要检查语法:
提示:避免配置文件出错,网站打不开的状况,工做中会写脚本,reload 后调用脚本批量检查语法,不正常就自动还原(发布代码相关)。
[root@lnmp02 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
3.5 启动 Nginx 并访问测试
经过 widows 浏览器访问 Ngnix 服务器的 IP:192.168.136.143 ,结果以下图所示:
提示:若是不成功,到 widows 做以下操做:
① ping 192.168.136.143
② telnet 192.168.136.143 80 # 若是此路不通,关闭 Ngnix 服务器防火墙。
3.6 查看 Nginx 编译的参数
[root@lnmp02 tools]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
3.7 Nginx 故障排查:
① Selinux 是否关闭;
② 防火墙是否关闭;
③ 经过查看日志排错:
cat /var/log/message
cat /application/nginx/logs/error.log
4. 了解 Nginx 服务
4.1 Nginx 安装目录下文件详情
[root@lnmp02 tools]# cd /application/nginx
[root@lnmp02 nginx]# ll
total 36
drwx------ 2 nginx root 4096 Dec 18 22:59 client_body_temp
drwxr-xr-x 2 root root 4096 Dec 18 22:40 conf
drwx------ 2 nginx root 4096 Dec 18 22:59 fastcgi_temp
drwxr-xr-x 2 root root 4096 Dec 18 22:40 html
drwxr-xr-x 2 root root 4096 Dec 18 22:59 logs
drwx------ 2 nginx root 4096 Dec 18 22:59 proxy_temp
drwxr-xr-x 2 root root 4096 Dec 18 22:40 sbin
drwx------ 2 nginx root 4096 Dec 18 22:59 scgi_temp
drwx------ 2 nginx root 4096 Dec 18 22:59 uwsgi_temp
[root@lnmp02 nginx]# ls -l|grep -v temp # 排除带有 temp 的临时文件。
total 36
drwxr-xr-x 2 root root 4096 Dec 18 22:40 conf # 配置文件。
drwxr-xr-x 2 root root 4096 Dec 18 22:40 html # Nginx 的默认网站(站点)目录。
drwxr-xr-x 2 root root 4096 Dec 18 22:59 logs # 访问日志、错误日志、进程号(pid)等。
drwxr-xr-x 2 root root 4096 Dec 18 22:40 sbin # 该目录下是启动命令。
4.2 Nginx 默认站点目录
[root@lnmp02 nginx]# cd html
[root@lnmp02 html]# ls -l
total 8
-rw-r--r-- 1 root root 537 Dec 18 22:40 50x.html
-rw-r--r-- 1 root root 612 Dec 18 22:40 index.html # 站点的首页文件(Welcoe to Nginx !)
4.3 Nginx 的配置文件
[root@lnmp02 nginx]# cd conf/
[root@lnmp02 conf]# ls -l nginx.conf # Nginx 的核心配置文件。
-rw-r--r-- 1 root root 2656 Dec 18 22:40 nginx.conf
4.4 Nginx 经常使用模块介绍
可经过官方地址 http://nginx.org/en/docs/ 查看
① Nginx 核心功能模块:【Core functionality】
Nginx 核心功能模块负责 Nginx 的全局应用,主要对应主配置文件的 Main 区块和 Events 区块区域,这里有不少 Nginx 必须的全局参数配置。
② 标准的 HTTP 功能模块集合:
这些标准的 HTTP 功能模块,虽然不是 Nginx 软件所必须的,但都是很经常使用的,所以绝大部分默认状况都会自动安装到 Nginx 软件中(见 P157 表 5-2),在生产环境中,配置、调整以及优化 Nginx 软件,主要就是根据这些模块的功能修改相应的参数来实现的。
4.5 Nginx 的目录结构说明
[root@lnmp02 ~]# tree /application/nginx
/application/nginx
├── client_body_temp
├── conf # Nginx 全部配置文件的目录。(重要)
│ ├── fastcgi.conf # fastcgi 相关参数的配置文件。
│ ├── fastcgi.conf.default # 上面的原始备份。
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types # 媒体类型。
│ ├── mime.types.default
│ ├── nginx.conf # Nginx 默认的主配置文件。
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp # fastcgi 临时数据目录。
├── html # 编译安装时 Nginx 的默认站点目录。
│ ├── 50x.html # 错误页面优雅替代显示文件。
│ └── index.html # 默认的首页文件。,首页文件名字是在 nginx.conf 中事先定义好的。(在实际工做中习惯用 index.html index.pgp index.jsp 来作网站的首页文件)
├── logs # Nginx 默认的日志路径。
│ ├── access.log # Nginx 默认访问日志文件(可经过命令:tail -f access.log 实时观看网站用户访问状况信息)
│ └── error.log # Nginx 的错诶日志文件,出现启动故障等问题查看该错误日志。
│ └── nginx.pid # Nginx 的进程号。
├── proxy_temp # 临时目录。
├── sbin # Nginx 命令的目录,如启动命令:nginx 。
│ └── nginx # Nginx 的启动命令:nginx 。
├── scgi_temp # 临时目录。
└── uwsgi_temp # 临时目录。
9 directories, 20 files
提示:全部结尾为 default 的文件都是备份文件。
4.6 Nginx 配置文件模板详解
[root@lnmp02 ~]# cd /application/nginx/conf/
[root@lnmp02 conf]# egrep -v "#|^$" nginx.conf.default # 过滤掉注释和空行查看文件。
worker_processes 1; # worker 进程的数量。(通常认为和 CPU 核数相等)
events { # 事件区块开始。
worker_connections 1024; # 每一个 worker 进程支持的最大链接数。
} # 事件区块结束。
http { # http 区块开始。
include mime.types; # Nginx 支持的媒体类型库文件。
default_type application/octet-stream; # 默认的媒体类型。
sendfile on; # 开启高效传输模式。
keepalive_timeout 65; # 链接超时,
server { # 第一个 server 区块开始。
listen 80; # 提供服务的端口,默认为 80 。
server_name localhost; # 提供服务的域名主机名。
location / { # 第一个 location 区块开始。
root html; # 站点的根目录。
index index.html index.htm; # 默认的首页文件,多个用空格隔开。
} # 第一个 location 区块结束。
error_page 500 502 503 504 /50x.html;# 出现对应的 http 状态码时,使用 50x.html 回应客户。
location = /50x.html { # location 区块开始,访问 50x.html 。
root html; # 指定对应的站点目录为 html 。
}
}
} # http 区块结束。
5. Nginx 虚拟主机
5.1 虚拟主机概念
所谓虚拟主机,在 Web 服务里就是一个独立的网站站点,这个站点对应独立的域名(也但是 IP 或端口)具备独立的程序及资源目录,能够独立地对外提供服务供用户访问。这个独立的站点在配置里是由必定格式的标签段标记的。Nginx 软件使用一个 server{} 标签来表示一个虚拟主机。一个 Web 服务里能够有多个虚拟主机标签对,便可以同时支持多个虚拟主机站点。
5.2 虚拟主机类型
① 基于域名的虚拟主机;★★★★★
② 基于端口的虚拟主机;★★
③ 基于 I P 的虚拟主机。
5.3 搭建两个基于域名的虚拟主机
[root@lnmp02 conf]# /application/nginx/sbin/nginx # 启动 Nginx 服务。
[root@lnmp02 conf]# egrep -v "#|^$" nginx.conf.default >nginx.con # 最小化编辑 conf 文件。
[root@lnmp02 conf]# vim nginx.conf
[root@lnmp02 conf]# cat 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.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
[root@lnmp02 conf]# mkdir ../html/{www,bbs} -p # 建立两个站点目录。
[root@lnmp02 conf]# tree ../html/
../html/
├── 50x.html
├── bbs
├── index.html
└── www
[root@lnmp02 conf]# echo "www.etiantian.org" > ../html/www/index.html
[root@lnmp02 conf]# echo "bbs.etiantian.org" > ../html/bbs/index.html
# 上面两行意为在首页文件配置以上内容便于访问测试时分辨。
[root@lnmp02 conf]# cat ../html/{www,bbs}/index.html # 查看设置的访问首页内容。
www.etiantian.org
bbs.etiantian.org
[root@lnmp02 conf]# /application/nginx/sbin/nginx -t # 检查语法。
[root@lnmp02 conf]# /application/nginx/sbin/nginx -s reload # 从新加载让配置生效。
[root@lnmp02 conf]# vi /etc/hosts # 编辑 hosts 作 DNS 解析。
192.168.136.143 www.etiantian.org bbs.etiantian.org
5.4 Linux 本地作 DNS 解析并访问
在 Linux 客户端本地经过编辑 hosts 作 DNS 解析:
工做中在购买的 DNS 解析页面以 A 记录的形式解析到公网 IP 便可。
在 Linux 客户端本地访问结果:
[root@lnmp02 conf]# ping www.etiantian.org
[root@lnmp02 conf]# ping bbs.etiantian.org
[root@lnmp02 conf]# curl www.etiantian.org
[root@lnmp02 conf]# curl bbs.etiantian.org
bbs.etiantian.org
提示:能 ping 通而且 curl 返回设置的首页内容表示配置成功!
5.5 在 Windows 作 DNS 解析后访问
在 Windows 的 /etc/hosts 下经过编辑 hosts 作 DNS 解析:
徽标键+R 调出运行窗口输入 drivers:
把内容:192.168.136.143 www.etiantian.org bbs.etiantian.org 拖入hosts 文件。
提示:若是不能修改 hosts 文件就让记事本以管理员身份运行再修改 hosts 文件。
在 Windows 访问配置的基于域名的虚拟主机结果:
在 windows 访问上面两个域名可看到访问首页内容(返回设置的 index.html)。【略】
若是在 windows 访问该服务器的 IP 地址,会返回 www.etiantian.org (第一个)。
5.6 Nginx 配置虚拟主机步骤小结
① 增长一个完整的 server 标签段到结尾处(注意要放在 http 的结束大括号前,也就是将 server 标签段放入 http 标签);
② 更改 server_name 及对应网页的 root 根目录,若是须要其它参数,能够增长或修改;
③ 建立 server_name 域名对应网页的根目录,而且创建测试文件,若是没有 index.html 首页,访问会出现 403 错误;
④ 检查 Nginx 配置文件语法,平滑重启 Nginx 服务,快速检查启动结果;
⑤ 在客户端对 server_name 处配置的域名作 host 解析或 DNS 配置,并检查(ping、telenet);
⑥ 在 Windows 浏览器中输入地址访问,或在 Linux 客户端作 hosts 解析,用 wget 或 curl 接配置的域名作访问测试是否成功。
6. 利用 include 功能优化 Nginx 的配置文件
[root@lnmp02 ~]# /application/nginx/sbin/nginx # 启动 Nginx 服务。
[root@lnmp02 ~]# cd /application/nginx/conf/
[root@lnmp02 ~]# cp nginx.conf nginx.conf.base_name # 备份配置文件。
[root@lnmp02 conf]# vim nginx.conf # 编辑配置文件。
[root@lnmp02 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# nginx vhosts config
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
[root@lnmp02 conf]# mkdir extra # 建立目录extre 。
[root@lnmp02 conf]# cat -n nginx.conf.base_name # 带行号输出域名虚拟主机的备份文件。
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 server {
11 listen 80;
12 server_name www.etiantian.org;
13 location / {
14 root html/www;
15 index index.html index.htm;
16 }
17 }
18 server {
19 listen 80;
20 server_name bbs.etiantian.org;
21 location / {
22 root html/bbs;
23 index index.html index.htm;
24 }
25 }
26 server {
27 listen 80;
28 server_name blog.etiantian.org;
29 location / {
30 root html/blog;
31 index index.html index.htm;
32 }
33 }
34 }
[root@lnmp02 conf]# sed -n '10,17p' nginx.conf.base_name # 取出第一个 server 标签。
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@lnmp02 conf]# sed -n '10,17p' nginx.conf.base_name >extra/www.conf # 放入指定目录 extra
[root@lnmp02 conf]# sed -n '18,25p' nginx.conf.base_name # 取出第二个 server 标签。
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@lnmp02 conf]# sed -n '18,25p' nginx.conf.base_name >extra/bbs.conf # 放入指目录 extra。
[root@lnmp02 conf]# sed -n '26,33p' nginx.conf.base_name # 取出第三个 server 标签。
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@lnmp02 conf]# sed -n '26,33p' nginx.conf.base_name >extra/blog.conf # 放入指定目录 extra
[root@lnmp02 conf]# ls -l extra/ # 三个文件每一个文件包含本身的虚拟主机。
total 12
-rw-r--r-- 1 root root 185 Dec 20 23:17 bbs.conf
-rw-r--r-- 1 root root 187 Dec 20 23:17 blog.conf
-rw-r--r-- 1 root root 185 Dec 20 23:16 www.conf
[root@lnmp02 conf]# /application/nginx/sbin/nginx -t # 检查语法。
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@lnmp02 conf]# /application/nginx/sbin/nginx -s reload # 平滑加载使配置生效。
[root@lnmp02 conf]# cat /etc/hosts # hosts 文件有域名对应 IP 的解析。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.136.143 www.etiantian.org bbs.etiantian.org blog.etiantian.org
[root@lnmp02 conf]# curl www.etiantian.org # 经过 curl 命令测试是否配置成功(成功)。
www.etiantian.org
[root@lnmp02 conf]# curl bbs.etiantian.org # 经过 curl 命令测试是否配置成功(成功)。
bbs.etiantian.org
[root@lnmp02 conf]# curl blog.etiantian.org # 经过 curl 命令测试是否配置成功(成功)。
blog.etiantian.org