nginx简介
Nginx 的官网 http://nginx.org/en/
基本特性
可针对静态资源高速高并发访问及缓存。
可使用反向代理加速,并且可进行数据缓存。
具有简单负载均衡、节点健康检查和容错功能。
支持远程 FastCGI 服务的缓存加速。
支持 FastCGI 、Uwsgi 、SCGI 、 Memcached Servers 的加速和缓存。
支持 SSL 、TLS 、SNI。
具有模块化的架构:过滤器包括 gzip 压缩 、ranges 支持、chunked 响应 、 XSLT、SSI 及图像缩放等功能。在 SSI 过滤器中,一个包含多个 SSI 的页面,如果经由FastCGI 或反向代理处理,可被并行处理。
具备 WWW 服务特性
支持基于名字、端口及 IP 的多虚拟主机站点。
支持 Keep-alive 和 pipelined 连接。
可进行简单 、方便、灵活的配置和管理。
支持修改 Nginx 配置,并且在代码上线时,可平滑重启,不中断业务访问。
可自定义访问日志格式,临时缓冲写日志操作,快速日志轮询及通过 rsyslog 处理日志。
可利用信号控制 Nginx 进程。
支持 3xx-5xx HTTP 状态码重定向。
支持 rewrite 模块,支持 URI 重写及正则表达式E配。
支持基于客户端 IP 地址和 HTTP 基本认证的访问控制。
支持 PUT 、 DELETE 、 MKCOL 、 COPY 及 MOVE 等较特殊的 HTTP 请求方法。
支持 FLV 流和 MP4 流技术产品应用。
支持 HTTP 响应速率限制。
支持同一 IP 地址的并发连接或请求数限制。
支持邮件服务代理。
nginx安装
RPM包获取:http://nginx.org/packages/
源码包获取:http://nginx.org/download/
Tengine
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
Nginx http 功能模块说明
ngx_http_core_module 包括一些核心的 http 参数配置, 对应 Nginx 的配置为 HTTP区块部分
ngx_http_access_module 访问控制模块, 用来控制网站用户对 Nginx 的访问
ngx_http_gzip_module 压缩模块, 对 Nginx 返回的数据压缩, 属于性能优化模块
ngx_http_fastcgi_module FastCGI 模块, 和动态应用相关的模块, 例如 PHP
ngx_http_proxy_module proxy 代理模块
ngx_http_upstream_module 负载均衡模块, 可以实现网站的负载均衡功能及节点的健康检查
ngx_http_rewrite_module URL 地址重写模块
ngx_http_limit_conn_module 限制用户并发连接数及请求数模块
ngx_http_limit_req_module 根据定义的 key 限制 Nginx 清求过程的速率
ngx_http_log_module 访问日志模块, 以指定的格式记录 Nginx 客户访问日志等信息
ngx_http_auth_basic_module Web 认证模块, 设置 Web 用户通过账号、 密码访问 Nginx
ngx_http_ssl_module ssl 用于加密的 http 连接, 如 https
ngx_http_stub_status_module 记录 Nginx 基本访问状态信息等的模块
1、 Tengine安装
官网:http://tengine.taobao.org/
1、配置扩展源
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2、下载tengine源码包
[root@node1 ~]# wget -c http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
3、下载编译工具包
[root@node1 ~]# yum install gcc gcc-c++ make pcre-devel openssl-devel -y
4、将源码包解压到/usr/local/src/目录下
[root@node1 ~]# tar xf tengine-2.3.2.tar.gz -C /usr/local/src/
[root@node1 ~]# cd /usr/local/src/tengine-2.3.2/
5、tengine帮助命令
[root@node1 tengine-2.3.2]# ./configure --help
6、基于nginx各模块对tengine初始化设置安装
[root@node1 tengine-2.3.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/tengine --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_perl_module
安装出现以下错误:perl版本过低,缺少模块
+ perl version: This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.
./configure: error: perl module ExtUtils::Embed is required
解决:安装该模块
[root@node1 tengine-2.3.2]# yum list | grep -i perl-ExtUtils-E
perl-ExtUtils-Embed.noarch 1.30-299.el7_9 updates
[root@node1 tengine-2.3.2]# yum install perl-ExtUtils-Embed.noarch -y
7、进行编译
[root@node1 tengine-2.3.2]# make
[root@node1 tengine-2.3.2]# make install
8、完成安装,tengine目录下文件说明
[root@node1 ~]# tree /usr/local/tengine/
/usr/local/tengine/
├── client_body_temp
├── conf
│ ├── 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
│ ├── 50x.html # 错误页面
│ └── index.html # 默认的首页文件
├── logs
│ ├── access.log #默认访问曰志文件
│ ├── error.log #Nginx 的错误日志文件
│ └── nginx.pid # Nginx 的 pid 文件,
├── proxy_temp # 临时目录
├── sbin
│ └── nginx # Nginx 的启动命令
├── scgi_temp # 临时目录
└── uwsgi_temp
2、 Tengine脚本编辑
将目录下的命令文件链接到/usr/sbin/目录下,可直接使用nginx命令
[root@node1 tengine-2.3.2]# ln -sv /usr/local/tengine/sbin/nginx /usr/sbin/
"/usr/sbin/nginx" -> "/usr/local/tengine/sbin/nginx"
查看nginx版本
[root@node1 tengine-2.3.2]# nginx -v
Tengine version: Tengine/2.3.2
编辑tengine脚本文件
[root@node1 ~]# vim /usr/lib/systemd/system/tengine.service
[Unit]
Description=tengine - high performance web server
Documentation=http://tengine.taobao.org/documentation_cn.html
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/tengine.pid
ExecStartPre=/usr/local/tengine/sbin/nginx -t -c /usr/local/tengine/conf/nginx.conf
ExecStart=/usr/local/tengine/sbin/nginx -c /usr/local/tengine/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl start tengine
tengine配置文件
worker_processes 1; #worker进程数量
events {
#事件区块
worker_connections 1024; #每个worker进程可以处理的连接数
}
http {
#http区块
include mime.types; #支持的媒体类型
default_type application/octet-stream; #默认的媒体类型
sendfile on; #高效传输模式
keepalive_timeout 65; #超时时间
server {
#server区块
listen 80; #监听端口
server_name localhost; #域名
location / {
#第一个location区块
root html; #站点目录
index index.html index.htm; #首页文件
} #第一个location区块结束
error_page 500 502 503 504 /50x.html; #错误页面配置
location = /50x.html {
#错误页面文件位置
root html; #路径
} #server区块结束
} #http区块结束
服务器停止防火墙后,浏览器访问服务器
nginx-web应用
1、虚拟主机
1)基于域名的虚拟主机
通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站使用的都是基于域名的虚拟主机
2)基于端口的虚拟主机
通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部网,访问基于端口的虚拟主机,地址里要带有端口
3)基于IP的虚拟主机
通过不同的IP区分不同的虚拟主机
示例:基于域名的虚拟主机
1、该配置添加到http区块中
[root@node1 ~]# vim /usr/local/tengine/conf/nginx.conf
server {
listen 80;
server_name www.xx.com;
location / {
root /usr/local/tengine/html/xx;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.test.com;
location / {
root /usr/local/tengine/html/test;
index index.html index.htm;
}
}
2、语法检测
[root@node1 ~]# nginx -t
nginx: the configuration file /usr/local/tengine/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/tengine/conf/nginx.conf test is successful
3、创建个虚拟机的主页目录
[root@node1 ~]# mkdir -p /usr/local/tengine/html/xx /usr/local/tengine/html/test
4、编辑个虚拟机的主页内容
[root@node1 ~]# echo "xx test page" > /usr/local/tengine/html/xx/index.html
[root@node1 ~]# echo "test page" > /usr/local/tengine/html/test/index.html
5、重启服务
[root@node1 ~]# systemctl restart tengine
6、编辑hosts文件实现域名解析
[root@node1 ~]# echo 192.168.16.11 www.xx.com www.test.com >> /etc/hosts
7、访问测试:
[root@node1 ~]# curl www.xx.com
xx test page
[root@node1 ~]# curl www.test.com
test page
2、日志
两种日志种类:
错误日志:记录nginx运行错误情况信息
访问日志:记录用户访问日志信息
1>配置错误日志
系统默认配置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
2>配置访问日志
系统默认配置
#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;
$remote_addr: 客户端地址
$remote_user: 远程访问用户
[$time_local]: 访问时间
$request: 请求行信息
$status: 状态码