Linux 下部署并配置 -Nginx
一 、Nginx 简介 ;
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个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 特点和优势 ===============================================================
① ,支持高并发 :能支持几万的并发连接 (特别是静态的小文件)
② , 资源消耗少: 在 3 万并发连接下,开启 10 个Nginx 线程消耗的内存不到 200MB
③ ,可以做HTTP 反向代理及加速缓存 ,即负载均衡功能,内置对 RS 节点服务器器健康检查功能相当于专业的haproxy 软件或者 LVS 功能 。
④ , 具备squid 等专业的缓存软件等功能 。
⑤ ,支持异步网络 I/O 时间模型 epoll。
nginx软件的特点或优势
· 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
· 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB
· 可以做HTTP反向代理及加速缓存、即负载均衡功能,
内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能。
· 具备Squid等专业缓存软件等的缓存功能。
· 支持异步网络I/O事件模型epoll(Linux 2.6+)(绘图说明同步和异步)
基础课程知识: 大并发写操作;先写内存,再将内存数据存储到硬盘中---保证访问效率
大并发读操作;先写磁盘,再将磁盘数据存储到内存中---保证数据安全
总结:高并发-->资源消耗少-->功能多样(web服务/负载均衡/支持缓存)-->通讯模型先进(epoll)
三、企业里 Nginx 的常见应用 =====================================================
① , 作为 web 服务软件
nginx 是一个支持高性能 、 高并发 的web 服务软件 ,它具有很多优秀的性能,作为 web 服务器于Apache 相比,Nginx 能够支持更多的 并发访问,但是占用的资源却很少,效率更高 ,在功能上也强大了很多不逊色Apache 。
② ,反向代理或者是负载均衡服务
在反向代理或者均在均衡方面,nginx 可以作为 web 服务器 、PHP 等动态的服务以及 Memcached缓存的代理服务器,它具有类似专业 的 反向代理如那件(如 haprxy) 的功能 ,同时也是一个优秀的邮件代理服务器,但是 nginx 的代理功能相对来说还是简单了一些 ,
③ ,前端也无缓存服务
在web 缓存服务方面, nginx 可以通过自身的 proxy_cache 模块来实现类似 squid 等专业的缓存软件功能 。
nginx 的三大功能 (Web 服务 、 反向代理或者是负载均衡服务 、 前端业务缓存服务 )
三 、Nginx 主要和Apache 的对比 ;
Nginx 和 Apache 主要是 模型上不一样 , Nginx 用的是最新的 epoll 模型 , Apache 用的是 select 模型
==============================================================================
企业中如何正确的选择 Web 服务 :
虽然国内很多人都在使用nginx, 但是Apache 、Lighttpd 这两个web 服务器同样也非常强大,
尤其是Apache 到现在为止仍是全球使用率最广泛的 web 服务软件
在实际中作中我们需要根据业务需求来选择合适的业务服务软件 ,有关web服务的建议如下:
① , 静态业务 : 若是高并发场景,尽量采用 nginx 或者是 lighttpd ,二者首选是 nginx
② , 动态业务 : 理论上采用 nginx 和 Apache 均可,建议nginx 为了避免相同业务软件的多样化。
增加额外的维护成本,动态业务可以由 nginx 兼做前端代理 ,再根据页面的元素的类型或者是目录,转发到后端相应的服务器处理进程 。
==============================================================================
四 、 安装Nginx ;
4.1 、下载软件 ;
[[email protected] ~]# mkdir -p /home/oldboy/tools --->> 创建统一的目录
[[email protected] ~]# cd /home/oldboy/tools --->> 进入到目录下
[[email protected] tools]# wget -q http://nginx.org/download/nginx-1.10.3.tar.gz --->> 下载软件
-bash: wget: command not found
问题1 : 没有 wget 这个命令 yum 安装一下即可
[[email protected] tools]# yum install wget -y --->> yum 下在 wget 命令
已加载插件:fastestmirror
设置安装进程
已安装:
wget.x86_64 0:1.12-10.el6
完毕!
[[email protected] tools]# wget -q http://nginx.org/download/nginx-1.10.3.tar.gz --->> 下载软件
[[email protected] tools]# ls --->> ls 查看一下
nginx-1.10.3.tar.gz
4.2 、 编译安装软件步骤 ;
nginx的依赖包(pcre-devel openssl-devel) ---->> 需要安装 Nginx 的依赖包
[[email protected] tools]# yum install -y pcre-devel openssl-devel
[[email protected] tools]# rpm -qa |egrep "pcre|openssl"
pcre-7.8-7.el6.x86_64
openssl-1.0.1e-57.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64
a. 解压要编译安装的软件 (解压软件---配置(./configure)-(编译 make)-(安装 make install))
[[email protected] tools]# ls
nginx-1.10.3.tar.gz
[[email protected] tools]# tar xf nginx-1.10.3.tar.gz --->> 解压 nginx
[[email protected] tools]# ls
nginx-1.10.3 nginx-1.10.3.tar.gz
[[email protected] tools]# cd nginx-1.10.3 ---->> 进入到nginx 这个目录下
[[email protected] nginx-1.10.3]#
[[email protected] nginx-1.10.3]# ls --->> 里面的内容就是源代码 ---默认编译会安装到/usr/local目录)
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[[email protected] tools]# useradd -s /sbin/nologin -M www ---->> 创建 web 服务程序 www 用户
[[email protected] nginx-1.10.3]# ./configure --prefix=/application/nginx-1.10.3 --user=www --group=www --with-http_stub_status_module --with- http_ssl_module
编译参数说明 :
#--prefix=/application/nginx-1.10.3 --->> 表示安装软件的目录,如果这个目录没有nginx 会自动创建的
# --user=www --group=www --->> 用户和组
# --with-http_stub_status_module --->> nginx 的状态模块
# --with- http_ssl_module --->> nginx 支持https 的模块
[[email protected] nginx-1.10.3]# echo $? --->> ./configure 执行完成后可以检查一下是否有错误 ,如果返回值是 0 说明正确 ;
0
[[email protected] nginx-1.10.3]# make && make install --->> 编译安装软件
[[email protected] nginx-1.10.3]# echo $? --->> 检查一下是否有错误
0
[[email protected] application]# ln -s /application/nginx-1.10.3/ /application/nginx ---->> 安装完成后创建一个软连接,方便以后管理
[[email protected] application]# /application/nginx/sbin/nginx ---->> 启动nginx
[[email protected] application]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 5268 root 6u IPv4 18383 0t0 TCP *:http (LISTEN)
nginx 5269 www 6u IPv4 18383 0t0 TCP *:http (LISTEN)
[[email protected] application]#
问题1 ;
[[email protected] application]# lsof -i:80 --->> 这个问题是和Apache 冲突了 停掉 Apache或者改端口 即可
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1643 root 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1696 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1697 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1698 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1699 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1700 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1701 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1702 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
httpd 1703 apache 4u IPv6 11535 0t0 TCP *:http (LISTEN)
============================== 至此软件安装完毕 ==================================
五 、 Nginx 软件的常见错误说明
①. nginx软件安装过程中遇到的问题
· 软件依赖包未正确安装问题---PCRE依赖包没有安装
---------------------------------------------------------------------------------------------
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
---------------------------------------------------------------------------------------------
解决方法:yum install pcre pcre-devel -y
软件依赖包未正确安装问题---OPENSSL依赖包没有安装
②. nginx软件启动过程中遇到的问题
· nginx软件重复启动产生的错误信息
---------------------------------------------------------------------------------------------
[[email protected] nginx-1.10.2]# /application/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
---------------------------------------------------------------------------------------------
解决方法:nginx软件已经启动无需反复启动,如果需要重新启动需要停止nginx进程或者用reload方式进行重启
③ . nginx软件排查问题三部曲说明
a 在客户端上ping服务器端IP,检查链路是否通畅
b 在客户端上telnet服务器端IP、端口,检查链路访问是否通畅
c 在客户端上wget检测模拟页面访问是否正常
curl -v/wget --debug
[[email protected] nginx]# ll ---->>> nginx 常用的目录结构说明
总用量 36
drwx------ 2 www root 4096 8月 25 04:24 client_body_temp
drwxr-xr-x 2 root root 4096 8月 25 04:16 conf --->> 保存nginx服务配置文件信息
drwx------ 2 www root 4096 8月 25 04:24 fastcgi_temp
drwxr-xr-x 2 root root 4096 8月 25 04:16 html ----->> web服务的站点目录
drwxr-xr-x 2 root root 4096 8月 25 04:33 logs ----->> nginx服务相关日志文件保存的目录
drwx------ 2 www root 4096 8月 25 04:24 proxy_temp
drwxr-xr-x 2 root root 4096 8月 25 04:16 sbin --->> nginx服务相关命令保存目录
drwx------ 2 www root 4096 8月 25 04:24 scgi_temp
drwx------ 2 www root 4096 8月 25 04:24 uwsgi_temp
[[email protected] nginx]# tree
.
├── client_body_temp
├── conf ---- >> Nginx 配置文件
│ ├── fastcgi.conf ---- >> nginx软件与其他动态软件配合文件
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types ---- >> 媒体类型 http 协议中的文件类型
│ ├── mime.types.default
│ ├── nginx.conf ---- >> nginx 主配置文件
│ ├── nginx.conf.default ---- >> 备份
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
├── html ---- >> nginx 默认的网站目录,也是站点目录
│ ├── 50x.html
│ └── index.html
├── logs
│ ├── access.log ---- >> nginx 访问日志
│ ├── error.log
│ └── nginx.pid
├── proxy_temp
├── sbin ---- >> nginx 管理命令
│ └── nginx
├── scgi_temp
└── uwsgi_temp
403错误出现情况:
01. 客户端访问受限
02. 默认的首页文件不存在时
nginx软件使用过程中深入说明
①. nginx软件语法检查方法:
nginx -t
②. nginx软件访问测试过程:
curl -v www.baidu.com
=========================================================================================
扩展说明:
a. 304状态码的意义说明
304状态码是浏览器缓存造成的,可以利用装包工具进行查看获取(抓包工具进行理解http访问过程)
取消缓存的方式为:在浏览器设置中进行清除缓存;或者采用浏览器强制刷新功能进行浏览器缓存的刷新
b. wireshark抓包软件使用说明
启动软件---选择需要进行转包的网卡---开始进行抓包
=========================================================================================
六 、 解析 Nginx 的配置文件 ;
[[email protected] conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf ---->> 把主配置文件精简化, 空行 、注释 等都去掉
[[email protected] 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 localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[email protected] conf]#
6.1 、 主配置文件注释 ;
[[email protected] conf]# /application/nginx/sbin/nginx -t ------>> Nginx 检查配置文件的语法是否有问题
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[[email protected] conf]#
[[email protected] conf]# /application/nginx/sbin/nginx -s reload ----->> 平滑重启 Nginx
再次在浏览器中输入 IP地址可以访问到内容 ;
七 、配置基于域名的虚拟主机 ;
[[email protected] conf]# mkdir -p /application/nginx/html/{www.bbs,blog} ---->> 创建所需要的目录
[[email protected] nginx]# for name in www bbs blog ;do echo $name `hostname` >/application/nginx/html/$name/index.html;done
[[email protected] nginx]# for name in www bbs blog ;do cat /application/nginx/html/$name/index.html;done
www m01
bbs m01
blog m01
[[email protected] nginx]#
【虚拟主机知识说明】
即一个server就是一个虚拟主机
[[email protected] ~]# cat /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;
server {
listen 80;
server_name www.etiantian.org; ----->> 修改虚拟主机域名名称
location / {
root html/www; ----->> 修改虚拟主机站点目录
index index.html index.htm;
}
}
}
[[email protected] conf]# /application/nginx/sbin/nginx -t ------>> Nginx 检查配置文件的语法是否有问题
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
配置完成后,浏览器访问测试 ;
配置多个虚拟主机 ;
[[email protected] ~]# cat /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;
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;
}
}
server {
listen 80;
server_name blog.etiantian.org; ---->> 修改虚拟主机域名名称
location / {
root html/blog; ---->> 修改虚拟主机站点目录
index index.html index.htm;
}
}
}
说明:利用vim复制命令,复制创建多个server主机标签;如 10,17copy17 既把 10 - 17行内容 复制到 17行下边
nginx配置文件每一行参数结尾都需要输入分号;
浏览器测试 ;
[[email protected] ~]# curl www.etiantian.org ---->> 也可以利用curl命令本地检测nginx配置是否成功;
[[email protected] ~]# curl bbs.etiantian.org
[[email protected] ~]# curl blog.etiantian.org
说明:利用curl命令进行测试三个虚拟主机是否已经可以正确访问;需要注意hosts文件中已经对三个域名进行了统一解析
总结:基于域名多虚拟主机配置
1. 需要创建相应的多个网站的目录 html/{www,bbs,blog}
2. 配置文件 域名配置(server_name) 网站的站点目录配置(root)
配置基于端口配置虚拟主机;
【虚拟主机配置说明---基于端口配置】
01. 基于端口虚拟主机主要用于内部网络人员访问虚拟主机使用,不想提供直接对外访问的虚拟主机
[[email protected] 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 8123; ---->> 由80改为基于端口的 8123 即可
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
说明:通过修改不同server标签中的监听端口信息,实现基于端口的方式访问不同虚拟主机;
nginx配置文件实践配置说明---基于IP配置虚拟主机
【虚拟主机配置说明---基于ip配置】
[[email protected] 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 10.0.0.8:80;
server_name www.etiantian.org; ----->> 此处也可以改成对应IP 10.0.0.8
location / {
root html/www;
index index.html index.htm;
}
}
}
说明:通过修改不同server标签中的监听ip信息,实现基于ip的方式访问不同虚拟主机;
=============================================================================
扩展知识说明: vim编辑器使用
ctrl+v 进入可视化模块模式,实现批量编辑操作
按方向键, 上下选择多行内容
shift+i 编辑选中的第一行
esc 退出编辑,完成多行内容统一设置
r 直接替换字符信息
x 删除当前光标所在位置字符信息
=============================================================================
nginx常用功能说明;nginx 配置文件精简化 ;
①. 多个主机不同配置文件进行管理(规范化配置文件)
实现一个网站对应一个配置文件,而不是将多个网站都放在一个nginx.conf配置文件中
提取主配置文件内容追加到相应虚拟主机配置文件中:
sed -n ‘10,17p’ nginx.conf >extra/www.conf ---->> extra 是在在 nginx conf 下自己创建的一个目录,需自定义
[[email protected] extra]# ls
bbs.conf blog.conf www.conf
[[email protected] extra]# cd ..
[[email protected] conf]# cat extra/*
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[[email protected] conf]#
[[email protected] 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;
include extra/www.conf; ---->> 需要把虚拟主机配置文件增加到 nginx 配置文件中就需要 “ include ” 这个参数 ;
include extra/bbs.conf;
include extra/blog.conf;
}
nginx 别名配置 ;
[[email protected] extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org; ---->> 在添加一个短的域名
location / {
root html/www;
index index.html index.htm;
}
}
[[email protected] extra]#
[[email protected] extra]# nginx -s reload
主要功能:便于定位识别相应的nginx服务器集群节点
nginx 状态模块功能说明
nginx -V ---查看编译参数中,是否加载了状态模块信息(--with-http_stub_status_module )
配置nginx服务的状态模块信息
cat >>/application/nginx/conf/extra/status.conf<<EOF
##status
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on; --->> 开启nginx 的状态信息
access_log off; --->> 不记录访问日志
}
}
EOF
[[email protected] extra]# ss -ant | grep -ic estab --->> 查看系统内的并发数量
2
[[email protected] extra]#
nginx日志功能说明
①. 错误日志信息说明
error_log的默认值为:
#default:error_log logs/error.log error;
可以放置的标签段为:
#context:main,http,server,location
参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log 。
说明:nginx官方文档查询信息如何使用,如何根据配置信息获取所在模块目录
②. 访问日志信息说明
Nginx日志格式中默认的参数配置如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
Nginx记录日志的默认参数配置如下:
access_log logs/access.log main;
日志参数详解 :
$remote_addr 10.0.0.253 ---->> 客户端IP地址信息
$remote_user - ---->> 远程的用户
[16/Sep/2017:14:15:29 +0800] [$time_local] ---->> 用户访问时间
"$request" "GET / HTTP/1.1" ---->> http请求报文中的请求行信息
$status 200 ---->> 服务端响应状态码信息
$body_bytes_sent 13 ---->> 服务端返回给客户端的资源大小信息
"$http_referer" - ---->> 记录推荐链接过来的服务器地址信息(暂时忽略)
"$http_user_agent" Chrome/61.0.3163.79 Safari/537.36 ---->> 客户端访问网站方式,利用的软件
"$http_x_forwarded_for" "-" ---->> 忽略掉
nginx日志轮询切割的一个小脚本
#!/bin/bash
/bin/mv /application/nginx/logs/access.log /application/nginx/logs/access_$(date +%F).log
/application/nginx/sbin/nginx -s reload
编写脚本,利用定时任务进行执行,实现日志切割的目的
nginx 自带的日志切割工具 logrotate 工具
nginx优化
①. nginx配置文件分开了
②. nginx加上了访问日志
③. nginx状态+zabbix(监控)
Nginx 状态码
Nginx 中的 location 模块 ; 根据 URI 找相应的位置 ;
[[email protected] extra]# vim www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / { ---->> / 表示默认情况下
return 401; ---->> 返回一个 错误信息 401
}
location /documents/ { ---->> 包含 documents 返回一个 403
return 403;
}
location ^~ /images/ { ---->> 包含 images 返回一个 404
return 404;
}
location ~* \.(gif|jpg|jpeg)$ { ---->> 不区分大小写只要包含这几个 (gif|jpg|jpeg)结尾的 返回一个 500
return 500;
}
Nginx Rewrite ;
网站更换域名的时候会用到rewrite ,还有使用使域名变得规范一点,
rewrite跳转第一种配置方式: =====================================
server {
listen 80;
server_name www.etiantian.org t.org;
if ($host ~* "^etiantian.org$") {
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
location / {
root html/www;
index index.html index.htm;
}
}
第二种配置方式 ;
curl 命令参数 ;