美文网首页
2018-03-13 Nginx从入门到实践-基础篇

2018-03-13 Nginx从入门到实践-基础篇

作者: tianjinliuchen | 来源:发表于2018-03-13 13:38 被阅读0次

    一.学习环境:系统硬件:CPU > =2Core,内存 >=256M

    操作系统:版本>=7.0,位数 X64

    二.环境调试确认

    1,确认系统网络

    2,确认yum可用

    yum list | grep gcc

    两项安装

    yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

    yum -y install wget httpd-tools vim

    一次初始化

    cd /opt;mkdir app download logs work backup

    3,确认关闭iptables规则

    service firewalld stop

    ps -ef | grep firewall

    4,确认停用seinux


    Nginx的中间件架构 Nginx的中间件架构  

         一.Nginx的简述:Nginx是一个开源且高性能,可靠的Http中间件,代理服务.

         二.常见的Http服务:httpd-Apache基金会,IIS-微软,GWS-Google

         三.问:为什么选择Nginx? 

        原因一,IO多路复用epoll多个描述符的I/O操作        都能在一个线程内并发      交替地顺序完成,这就叫I/O多路复用,这里的附庸指的是复用同一个线程.什       么是epoll IO多路复用实现方式select,poll,epoll


    IO多路复用epoll

    select缺点:1.能够监视文件描述符的数量存在最大限制

                       2.线形扫描效率低下

    epoll模型:1.每当FD就绪,采用系统的回调函数之间将fd放入,效率更高.

                     2.最大连接无限制

    原因二,轻量级 功能模块少 代码模块化

    原因三,CPU亲和(affinity)一,为什么需要CPU亲和


    Cpu亲和(affinity)

    什么是Cpu亲和:是一种把Cpu亲和和Nginx工作进程绑定方式,把每个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能.

    原因四,sendfile


    Sendfile Sendfile

    一,Nginx快速搭建与基本参数使用

    Mainline version - 开发版   Stable version - 稳定版本 Legacy version - 历史版本

    二,基本参数使用

    安装目录 编译参数 nginx基本配置语法

    1,安装目录讲解

    cd /etc/yum.repos.d/

    vim nginx.repo

    yum -y install nginx

    nginx -V

    yum list | grep nginx

    命令: rpm -ql nginx

    /etc/logrotate.d/nginx 配置文件

    /etc/nginx 目录

    /etc/nginx/nginx.conf 配置文件

    /etc/nginx/conf.d  目录

    /etc/nginx/conf.d/default.conf 默认配置目录

    /etc/nginx/fastcgi_params fastcgi配置文件

    /etc/nginx/uwsgi_params uwsgi配置文件

    /etc/nginx/scgi_params scgi配置文件

    /etc/nginx/koi-utf 编码转换影射转化

    /etc/nginx/koi-win 编码转换影射转化

    /etc/nginx/win-utf  编码转换影射转化

    /etc/nginx/mime.types 设置http协议的Content-Type与扩展名对应关系

    /usr/lib/sysemd/system/nginx-debug.service 

    /usr/lib/sysemd/system/nginx.service

    /etc/sysconfig/nginx

    /etc/sysconfig/nginx

    用于配置出系统守护进程管理器管理方式

    /usr/lib64/nginx/modules

    /etc/nginx/modules

    Nginx模块目录

    /usr/sbin/nginx

    /usr/sbin/nginx-debug

    Nginx服务的启动管理的终端命令

    /usr/share/doc/nginx-1.12.0

    /usr/share/doc/nginx-1.12.0/COPYRIGHT 

    /usr/share/man/man8/nginx.8.gz 

    Nginx的手册和帮助文件

    /var/cache/nginx

    Nginx的缓存目录

    /var/log/nginx

    Nginx的日志目录

    2,安装编译参数

    命令: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

    安装目的的目录或路径

    --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

    执行模块时,Nginx所保留的临时性文件

    --user=nginx --group=nginx 设定Nginx进程启动的用户和组的用户

    --with-cc-opt=parameters 设置额外的参数被添加到CFLAGS变量

    --with-ld-opt=parameters 设置附加的参数,链接系统库 

    Http请求

    Http请求

    request - 包括请求行,请求头部,请求数据

    > GET / HTTP/1.1

    > User-Agent: curl/7.29.0

    > Host: www.imooc.com

    > Accept: */*

    response - 包括状态行,消息报头,响应正文

    curl -v http://www.imooc.com >/del/null 类似于浏览器查看web请求

    2,Nginx日志类型

    包括:error.log access_log

    log_format

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

    Default:log_format combined "...",

    Context:http

    2.Nginx的变量

    Http请求变量 -arg_PARAMRTER,Http_HEADER,sent_http_HEADER

    内置变量 - Nginx内置的

    自定义变量 - 自己定义

    Nginx模块讲解

    Nginx官方模块

    第三方模块

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

    --with-http_stub_status_module配置

    Syntax:stub_status;

    Default:--

    Context:server,location

    四,默认模块讲解

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

    2,rangdom_index_module

    syntax:random_index on | off;

    Default:random_index off;

    Context:location

    --with-http_sub_module HTTP内容替换

    Syntax : sub_filter string replacement;

    Default:--

    Context:http,server,location

    2.http_sub_module

    Syntax: sub_filter_last_modified on|off;

    Default:sub_filter_last_modifiied off;

    Context:http,server,location

    2.http_sub_module

    Syntax: sub_filter_once on|off;

    Default:sub_filter_once on;

    Context:http,server,location

    八,Nginx的请求限制

    连接频率限制 - limit_conn_module

    请求频率限制- limit_req_module


    Http协议的连接与请求

    Http 1.0 Tcp不能服用

    Http 1.1顺序性Tcp复用

    Http2.0多路复用Tcp服用

    八,Http协议的连接与请求

    Http请求建立在一次Tcp连接基础上

    一次Tcp请求至少产生一次Http请求

    连接限制

    syntax:limit_conn_zone key zone=name:size;

    Default:--

    context:http

    Syntax:limint_conn zone number;

    Default:--

    Context:http,server,location

    八 请求限制

    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的访问控制

    基于IP的访问控制 http_access_module

    Syntax:allow address | CIDR |unix:|all;

    Default:--

    Context:http,server,location,limit_except

    Syntax:deny address | CIDR |unix:|all;

    Default:--

    Context:http,server,location,limit_except

    基于用户的信任登陆-http_auth_basic_module

    http_access_module局限性

    http_access_module局限性  只能通过$remote_addr控制信任 3.http_x_forwarded_for

    http_x_forwarded_for=Client IP,Proxy(1)IP,Proxy(2)IP,....

    方法一,采用别的Http头信息控制访问,如:Http_X_Forward_For

    方法二,结合geo模块作

    方法三,通过http自定义变量传递

    http_auth_basic_module

    Syntax:auth_basic strint | off;

    Default:auth_basic off;

    Context:http,server,location,limit_except

    Syntax:auth_basic_user_file file;

    Default:----

    Context:http,server,location,limit_except

    http_auth_basic_module局限性

    一,用户信息以来文件方式

    二,操作管理机械,效率低下

    3,解决方案

    一,Nginx结合Lua实现高效验证

    二,Nginx和LDAP打通,利用nginx-auth-ldap

    相关文章

      网友评论

          本文标题:2018-03-13 Nginx从入门到实践-基础篇

          本文链接:https://www.haomeiwen.com/subject/znhzfftx.html