目录
学习环境准备
centos7.2
1、确认网络可用 ping www.baidu.com
2、确认yum可用 yum list|grep gcc
3、确认关闭iptables规则 iptables -L #查看规则 optables -F#关闭 iptables -t nat -F
4、确认停用selinux ,getenforce #显示Disabled即可,setenforce 0 #设置selinux关闭
两项安装
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install httpd-tools
一次初始化
cd /opt;mkdir app download logs work backup
Nginx快速安装
Mainline version -开发版
Stable version -稳定版
Legacy vesion -历史版本
官网下载
nginx.org->downlowd->点击底部
Stable version连接->拷贝yum配置如下
[nginx]name=nginx repogpgcheck=0enabled=1
vi /etc/yum.repos.d/nginx.repo #添加如下内容,替换os及版本
[nginx]name=nginx repogpgcheck=0enabled=1
测试
yum list|grep nginx #可以查看到最新的nginx版本1.1.13
安装
yum install nginx
nginx -v #查看版本
nginx -V #查看编译的参数
Nginx安装目录
安装目录
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
/etc/nginx/fastcgi_params 配置文件 cgi配置相关、fastcgi配置
/etc/nginx/uwsgi_params
/etc/nginx/scgi_params
/etc/nginx/koi-utf 配置文件 编码转换映射转化文件
/etc/nginx/koi-win
/etc/nginx/win-utf
/etc/nginx/mime.types 配置文件 设置http协议的Content-Type与扩展名对应关系
/usr/lib/systemd/system/nginx-debug.service 配置文件 用于配置出系统守护进程管理器管理方式
/usr/lib/systemd/system/nginx.service
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib64/nginx/modules 目录 Nginx模块目录
/etc/nginx/modules
/usr/sbin/nginx 命令 Nginx服务的启动管理的终端命令
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.12.0 文件、目录 Nginx的手册和帮助文件
/usr/share/doc/nginx-1.12.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/var/cache/nginx 目录 Nginx的缓存目录
/var/log/nginx 目录 Nginx的日志目录
Nginx编译配置参数
nginx -V #查看编译参数
#安装目录
--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/nginx.pid
--lock-path=/var/run/nginx.lock
#执行对应模块时,Nginx所保留的临时性文件
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_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=parameters 设置附件的参数,链接系统库
默认配置语法
vi /etc/nginx/nginx.conf
user 设置nginx服务的系统使用用户
worker_processes 工作进程数
error_log nginx的错误日志
pid nginx 服务启动时候pid
events worker_connections 每个进程允许的最大连接数
use 工作进程数
http{
... ...
server{
listen 80;
server_name localhost;
location /{
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html{
root /usr/share/nginx/html;
}
}
server {
...
}
}
server{
listen 80; #端口
server_name localhost ;#server名称,可以是域名
location / {#/代表默认路径
root /usr/share/nginx/html;#目录
index index.html index.htm;#首页
}
error_page 500 502 503 504 404 /50x.html;#定义错误页面
location = /50x.html {//定义/50x.html访问路径
root /usr/share/nginx/html;
}
}
重启、重载
systemctl restart nginx.service #修改配置文件后需要重启服务
systemctl reload nginx.service #修改配置文件后需要重新加载配置文件
模拟HTTP请求
curl -v
http://www.baidu.com >> /dev/null
Nginx日志_log_format1
包括:error.log access.log
log_format
Syntax:logformat name [escape=default|json] string...;
Default:log_format combined "...";
Context:http
nginx.conf
error_log /var/log/nginx/error.log warn;#位置,级别
http{
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 /var/log/nginx/access.log main; #位置,main与上面的main对应
}
Nginx变量
http请求变量 - arg_PARAMETER、http_HEADER、send_
http_HEADER #HEADER内-要转成_
内置变量 - Nginx内置的,具体配置请看:nginx.org/en/docs->Logging to syslog->点击第一行连接"access_log"->找到
$status链接点击进去,即可看到所有的变量
自定义变量 - 自己定义
curl -v http://www.baidu.com >> /dev/null #查看支持的HEADER
添加一个HEADER参数,测试;注意:HEADER需要小写,且-换成_
nginx -t -c /etc/nginx/nginx.conf #检查配置文件
systemctl reload nginx.service
浏览器访问:
http://192.168.123.7/ 或
curl -v http://192.168.123.7 >> /dev/null
查看access日志:tail -f /var/log/nginx/access.log
Nginx日志_log_format2
remote_addr 客户端地址
remote_user http请求客户端认证的用户名
time_local nginx的时间
request request头请求行
status response返回状态
body_bytes_sent body大小
http_referer 上级页面url
http_user_agent 客户端信息
http_x_forwarded_for 每一级携带的http信息
自定义变量 - 自己定义
Nginx模块讲解_sub_status
编译选项
--with-http_stub_status_module Nginx的客户端状态
配置语法
Syntax:stub_status
Default:——
Context:server,location
location /mystatus {
stub_status;
}
重载服务
nginx -tc /etc/nginx/nginx.conf
nginx -s reload -c /etc/nginx/nginx.conf
访问
Nginx模块讲解_random_index
--with-http_random_index_module 目录中选择一个随机主页
Syntax:random_index on|off
Default:random_index off
Context:location
location / {
root /usr/share/nginx/html;
random_index on;
#index index.html index.htm
}
/usr/share/nginx/html/1.html
/usr/share/nginx/html/2.html
/usr/share/nginx/html/3.html
Nginx模块讲解_sub_module
--with-http_sub_module http内容替换
Syntax:sub_filter string replacement
Default:——
Context:http,server,location
例子:sub_filter '<a>imooc' '<a>IMOOC'
Syntax:sub_filter_last_modified on|off
Default:
sub_filter_last_modified off
Context:server,location
Syntax:sub_filter_once on|off
Default:
sub_filter_once
on
Context:server,location
例子:
sub_filter_once
off
Nginx的请求限制_配置语法与原理1
Nginx的请求限制
连接频率限制 - limit_conn_module
请求频率限制 - limit_req_module
连接限制
Syntax:limit_conn_zone key zone=name:size
Default:——
Context:http
Syntax:limit_conn zone number;
Default:——
Context:http,server,location
Nginx的请求限制_配置语法与原理2
请求限制
Syntax:limit_req_zone key zone=name:size rate=rate;
Default:——
Context:http
Syntax:limit_req zone=name [burst=number] [nodelay];
Default:——
Context:http,server,location
Nginx的请求限制_配置语法与原理2
连接限制
#定义
连接存储空间
Syntax:limit_conn_zone key zone=name:size
Default:——
Context:http
#限制连接并发的次数
Syntax:limit_conn zone number;
Default:——
Context:http,server,location
#限制服务端同一时刻只允许同一个ip连接
请求限制
#定义存储空间、速率
Syntax:limit_req_zone key zone=name:size rate=rate;
Default:——
Context:http
#
限制并发的次数
Syntax:limit_req zone=name [burst=number] [nodelay];
Default:——
Context:http,server,location
#
burst=number 等待响应的数量
nodelay:剩余的直接返回503
限制演示
http{
limit_conn_zone $binary_remote_addr zone=conn_zone:1m
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
location / {
root ...
#limit_conn conn_zone 1;
#limit_req zone=req_zone burst=3 nodelay;
#
limit_req zone=req_zone burst=3
#
limit_req zone=req_zone
}
}
压力测试工具
ab -n 40 -c 20
http://192.168.123.7/
参数说明:-n 总共发送请求数量;-c 并发数
可同时查看nginx errorlog日志
Nginx的访问控制_介绍实现访问控制的基本方式
两种访问控制方式
基于ip的访问控制 - http_access_module
基于用户的信任登录 - http_auth_basic_module
http_access_module配置
#允许
Syntax:allow address | CIDR |unix:|all #
CIDR 网段
Default:——
Context:http,server,location,limit_except
#不允许
Syntax:deny address | CIDR |unix:|all #
CIDR 网段
Default:——
Context:http,server,location,limit_except
Nginx的访问控制—access_module配置
2-25 Nginx的访问控制—access_module配置
2-26 Nginx的访问控制—access_module局限性
2-27 Nginx的访问控制—auth_basic_module配置
2-28 Nginx的访问控制—auth_basic_module局限性
Nginx作为静态资源web服务_静态资源类型
配置语法-文件读取
Syntax:sendfile on|off
Default:sendfile off;
Context:http,server,location.if in location
引读:--with-file-aio 异步文件读取
配置语法-tcp_nopush
Syntax:tcp_nopush on | off
Default:tcp_nopush off;
Context:http,server,location
作用:sendfile开启的情况下,提高网络传输效率
配置语法-tcp_nodely
Syntax:tcp_
nodely
on | off
Default:tcp_
nodely
off;
Context:http,server,location
作用:keeplive连接下,提高网络包的传输实时性
配置语法-压缩
Syntax:gzip
on | off
Default:
gzip
off;
Context:http,server,location,if in location
作用:压缩传输
Syntax:gzip_comp_level
level
Default:
gzip_comp_level 1;
Context:http,server,location
作用:压缩比
Syntax:gzip_http_version 1.0|1.1
Default:
gzip_http_version 1.1
Context:http,server,location
作用:gzip http版本
扩展Nginx压缩模块
http_gzip_static_module - 预读gzip功能
http_gunzip_module --应用支持gunzip的压缩方式
配置示例
浏览器缓存
跨站访问
防盗链
代理服务_代理服务
反向代理
客户端只可以访问80端口,通过80反向代理到8080端口
正向代理
主服务器
代理服务器28
客户端浏览器设置
扩展配置
反向代理通用配置示例
把配置提取到文件中
负载均衡
配置语法
配置示例
backup状态演示
轮询策略与加权轮询
ip_hash方式
url_hash策略
缓存服务器
关闭缓存
大文件分片请求
Nginx动静分离_动静分离场景演示
Rewrite规则
配置语法
正则表达式
pcregrep正则表达式命令
flag标识
flag配置示例
重定向
优先级
secure_link模块作用原理
配置语法
nginx -V命令确认secure_link模块是否已经编译进去
配置示例
生成加密url 的shell脚本
正式情况使用java代码生成
Geoip读取地域信息模块介绍
安装geopid模块
编辑nginx.conf加载geoip模块
下载地域文件
配置文件中添加地域文件配置
修改配置文件,打印地域信息
HTTPS原理和作用1
证书签名生成CA证书
参考马哥linux openssl证书生成
Nginx https配置语法
配置示例
ssl端口443
重启、启动
访问
加https访问