Nginx介绍及安装
Nginx 简述
Nginx 是一个开源的 高性能的 可靠地HTTP中间件 代理服务.
Nginx 优点
1.内核模型
IO多路复用的epoll模型
1. 单线程 多进程
多线程共享内存,有一个线程有问题会影响其他线程,多进程是 独立应用同一个线程内存的
2. IO 多路复用模型
在一个线程内复用的完成任务,这里的复用指的是同一个线程
并行IO处理,就是一个master管理一群worker 进程,请求来了 每个空闲worker接收请求,处理请求,处理完毕告诉master我 处理完了,可以接收处理下一个
- Epoll 模型
Select
线性遍历队列中的空闲处理
最大的请求数为1024个
Epoll
2.轻量级
功能模块少
代码模块化
3.CPU亲和(affinity)
是一种吧CPU核心和Nginx工作进程绑定的方式,把每个worker进程固定在一个CPU上执行,减少切换CPU的cache miss,获得更好的性能
4.Sendfile
一般的是 用户空间 内核空间 到socket
Sendfile
直接操作内核空间 到达socket,减少用户空间操作
Nginx 安装
Yum 安装
设置yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
#gpgkey=https://nginx.org/keys/nginx_signing.key
yum list |grep nginx
yum install nginx
nginx -v
安装完成
安装目录
Rpm -ql nginx
/etc/logrotate.d/nginx
配置文件
Nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx
目录 配置文件 Nginx主配置文件
/etc/nginx/nginx.conf 主配置文件,启动加载文件
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf server配置文件
/etc/nginx/fastcgi_params
/etc/nginx/uwsgi_params
/etc/nginx/scgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-utf
/etc/nginx/win-utf
/etc/nginx/mime.types
/etc/nginx/modules/
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.14.2/
/usr/share/doc/nginx-1.14.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/var/cache/nginx
/var/log/nginx
安装编译参数
--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/pid/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-fastcig-temp-path=/var/cache/nginx/fastcig_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
用户和用户组
--user=nginx Nginx进程启动的用户和组用户
--group=nginx
--with-cc-opt=parameters 设置额外的参数被添加到CFLAGS
变量
--with-ld-opt=paraments 设置附加的参数 链接系统库
Nginx-debug 使用
安装时候安装此选项
./configure --with-debug --with-cc-opt='-g -O0'
设置配置文件
error_log logs/error.log debug;
代码调试
ngx_log_debug(NGX_LOG_DEBUG, log, 0,"[jsh] 11111");
HTTP请求
Nginx 日志
Nginx变量
HTTP请求变量 -arg_PARAMETER htttp_HEADER set_http_HEADER
内置变量 Nginx内置的
自定义变量 自己定义的
日志
error.log access.log
log_format
log-format name [escape=dafult|json] string ...;
context : http
例子 :
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer"
' '"$http_user_agent" "$http_x_forwarded_for"';
Main 名字
remote_addr:对应客户端的地址
remote_user:是请求客户端请求认证的用户名,如果没有开启认证模块的话是值为空。
time_local:表示nginx服务器时间
request:表示request请求头的行
status:表示response的返回状态
body_bytes_sent:表示从服务端返回给客户端的body数据大小
http_referer:表示请求的上一级页面
http_user_agent:表示agent信息
http_x_forwarded_for:会记录每一级请求中信息
Nginx 模块
官方模块
第三方模块
http_stub_status_module
监控nginx 运行状态
--with-http_sub_status_module Nginx的客户端状态
Syntax : stub_status;
Default:----
Context: server,location
实例:
- 配置文件设置
location /mystatus {
stub_status;
}
Active connections: 1
活跃链接数
server accepts handled requests
6 6 12
Nginx的握手数 nginx的连接数 Nginx的请求数
Reading: 0 Writing: 1 Waiting: 0
读 写 等待数
random_index_module
随机返回网站首页
不会加载以.开头的文件
Syntax : random_index on|off;
Default: random_index off;
Context: location
实例:
- 配置文件
location / {
#stub_status;
root /opt/code/;
random_index on;
}
- 创建 /opt/code/下 的 1.html 2.html 3. html
- 访问 http://192.168.153.139/
http_sub_module
--with-http_sub_module
HTTP内容替换
Syntax: sub-filter string(要被替换内容) replacement(替换后的内容)
Default: ---
Context; http server location
实例:
- 配置文件
location / {
root /opt/code/;
sub_filter lisi zhangsan;
}
- 在/opt/code 下 创建sub_filter.html
<h1>
lisi</br>
zhangsan</br>
lisi</br>
wangwu</br>
</h1>
- 访问
第一个被替换
判断Nginx发送给客户端的内容是否有变化
Syntax: sub_filter_last_modified on|off;
Default: sub_filter_last_modified off;
Context; http server location
实例:
在以上基础
Sub_filter指令执行一次,还是重复执行
Syntax: sub_filter_once on|off;
Default: sub_filter_once on;
Context; http server location
实例:
配置文件
location / {
root /opt/code/;
sub_filter lisi zhangsan;
sub_filter_once off;
}
在以上基础上替换全部
指定sub_filter 的应用类型
Syntax: sub_filter_types text/html;
Default:sub_filter_types text/html;
Context; http server location