nginx从入门到实践-基础篇

一、nginx简述?

nginx是一个开源且高性能、可靠的http中间件、代理服务。

二、nginx实现特性 优点

1.io多路复用epoll

1.什么是io多路复?

多个描述符的的io操作都能在一个线程内并发交替的顺序完成,这就叫io多路复用,这里的复用是指复用一个线程。

2.什么是epoll?

io多路复用的实现方式select,poll,epoll

2.轻量级

功能模块少

代码模块化

3.CPU亲和

什么是cpu亲和?

是一种把cpu核心和nginx工作进程的绑定方式,把每个work进程固定在一个cpu上进行,减少切换cpu的cache miss,获得更好的进行.

4.sendfile

三、nginx安装

mainline version-开发板

stable version-稳定版

legacy version-历史版本

四、nginx配置参数

1.  --user=nginx

--group=nginx 设置nginx进程启动的用户和组

 

2. 主要配置文件 /etc/nginx/nginx.conf

默认配置文件 /etc/nginx/conf.d/default.conf

 

3. user 设置nginx服务的系统使用用户

worker-processes 工作进程数  和系统核心数设置一致

error_log nginx的错误日志

pid nginx启动时候pid

 

4.ecent     worker_connections  每个进程允许最大连接数(10000个)

               use   定义使用的内核模型  (select,epoll,poll)

2-12 HTTP请求

 yum -y install curl  安装curl(相当于浏览器)

curl --version  查看版本

curl -v http://www.baidu.com  访问百度官网(-v可以看到请求行)

2-13 Nginx日志_log_format1

error.log  收集http错误信息以及nginx本身服务的错误信息,按照不同的级别记录

access_log  记录每次http请求的访问状态

 

log_format

syntax:log_format name [escape=default|json] string...;

default:log_format combined "..."

context:http

nginx -t -c /etc/nginx/nginx.conf  -t  代表对nginx配置文件进行语法检查-c代表配置文件的路径

nginx -s reload -c /etc/nginx/nginx.conf  重载nginx服务

 

nginx变量

http请求变量-arg_PARAMETER,http_HEADER(request),sent_http_HEADER(response)

内置变量-nginx内置的

自定义变量-自己定义的

 

2-14 Nginx日志_log_format2

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$http_user_agent'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

2-15 Nginx模块讲解_模块介绍

nginx官方模块

第三方模块

2-16 Nginx模块讲解_sub_status

 --with-http_stub_status_module  nginx的客户端状态

http_stub_status_module配置

synax:stub_status

default:--

context:server,location

 

Active connections: 2  nginx当前活跃的连接数
server accepts handled requests
 12 12 12   握手数  连接数  请求数
Reading: 0 Writing: 1 Waiting: 1   正在读个数 正在写个数  保持连接(keeplive强连接)个数

 

2-17 Nginx模块讲解_random_index

--with-http_random_index_module   目录中选择一个随机主页

synax:random_index on|off

default:random_index off

context:location

加载不到   .3.html(隐藏目录)

2-18 Nginx模块讲解_sub_module

--with-http_sub_module   http内容替换(response)

synax:sub_filter string replacement

default:--

context:http,server,location

 

synax:sub_filter_last_mofified on|off

default:sub_filter_last_mofified off

context:http,server,location

检查服务端内容是否发生变更,返回给客户端最新内容,减少不必要的消耗

 

synax:sub_filter_once on|off

default:sub_filteronce on

context:http,server,location

默认只替换和html第一个匹配的字符

2-19 Nginx模块讲解_sub_module配置演示

2-20 Nginx的请求限制_配置语法与原理1??????

nginx请求限制

连接频率限制-limit_conn_module(基于tcp连接)

请求频率限制-limit_req_module(基于http请求)

 

synax:limit_conn_zone key zone=name:size;

default:--

context:http

synax:limit_conn zone number;

default:--

context:http,server,location

 

http版本  连接关系

http1.0   tcp不能复用

http1.1   顺序性tcp复用

http2.0    多路复用tcp复用

 

2-21 Nginx的请求限制_配置语法与原理2

synax:limit_req_zone key zone=name:size rate=rate;

default:--

context:http

synax:limit_req zone number [burst=number] [nodelay];

default:--

context:http,server,location

2-22 Nginx的请求限制_配置语法与原理3

 

2-23 Nginx的访问控制_介绍实现访问控制的基本方式

nginx的访问控制

基于ip的访问控制-http_access_module

基于用户的信任登录-http_auth_basic_module

2-24 Nginx的访问控制—access_module配置语法介绍

http_access_module

synax:allow address | CIDR | unix: | all    (ip地址,ip网段,sockct,允许所有)

default:--

context:http,server,location,limit_except

 

synax:deny address | CIDR | unix: | all    (ip地址,ip网段,sockct,允许所有)

default:--

context:http,server,location,limit_except

2-25 Nginx的访问控制—access_module配置

location ~ ^/3.html {     (~正则表达式  以^结尾)
       root /opt/app/code;
       allow 192.168.0.0/24;
       deny all;
       index  1.html 2.html 3.html;
    }

2-26 Nginx的访问控制—access_module局限性

对中间层代理做了限制而不是对客户端做了限制

http_x_forwarded_for  = clientip,proxy(1)ip,proxy(2)ip...

2-27 Nginx的访问控制—auth_basic_module配置

Syntax:auth_basic string | off;
Default:
auth_basic off;
Context:httpserverlocationlimit_except
Syntax:auth_basic_user_file file;
Default:
Context:httpserverlocationlimit_except

htpasswd建立和更新存储用户名、密码的文本文件,用于对HTTP用户的basic认证

rpm -qf /usr/bin/htpasswd

yum install httpd-tools -y

htpasswd -h   帮助信息

htpasswd -c   Create a new file

2-28 Nginx的访问控制—auth_basic_module局限性

解决方案

nginx结合lua实现高效认证

nignx和LDAP打通,利用nginx-auth-ldap模块

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值