• nginx基本知识


    《深入理解Nginx 模块开发与架构解析》第2版

    一、研究Nginx前的准备工作

    nginx的特点:

    1.更快

    2.高扩展性

    3.高可靠性

    4.低内存消耗

    5.单机支持10万以上的并发连接

    6.热部署

    7.最自由的BSD许可协议

    1.准备工作

    1.1使用nginx的必备软件

    1.GCC编译器

    yum install -y gcc
    yum install -y gcc-c++

    2.PCRE库


    yum install -y pcre pcre-devel

    3.zlib库


    yum install -y zlib zlib-devel

    4.OpenSSL开发库


    yum install -y openssl openssl-devel

    1.2Linux内核参数的优化

    修改/etc/sysctl.conf来更改内核参数。

    执行sysctl -p命令,使修改生效

    1.3获取Nginx源码

    (http://nginx.org/en/download.html)


    tar -zxvf nginx-1.0.14.tar.gz

    1.4编译安装Nginx

    进入nginx-1.0.14目录后执行


    ./configure
    make
    make install

    configure:检测操作系统内核和已经安装的软件,参数的解析,中间目录的生成以及根据各种参数生成一些C源码文件、Makefile文件等。

    make:根据configure命令生成的Makefile文件编译Nginx工程,并生成目标文件、最终的二进制文件。

    make install:根据configure执行时的参数将Nginx部署到指定的安装目录,包括相关目录的建立和二进制文件、配置文件的复制。

    1.5configure详解

    1.configure的命令参数


    ./configure --help

    2.configure执行成功后,会生成objs目录,

    src目录用于存放编译时产生的目标文件。

    Makefile文件用于编译Nginx工程以及在加入install参数后安装Nginx。

    autoconf.err保存configure执行过程中产生的结果

    ngx_auto_headers.h和ngx_auto_config.h保存了一些宏,这两个头文件会被src/core/ngx_config.h及src/oc/unix/ngx_linux_config.h文件引用

    ngx_modules.c用来定义ngx_modules数组的

    1.6Nginx的命令行控制

    默认情况下:

    Nginx被安装在目录/usr/local/nginx/中

    二进制文件路径为/usr/local/nginx/sbin/nginx

    配置文件路径为/usr/local/nginx/conf/nginx.conf

    1、默认方式启动


    /usr/local/nginx/sbin/nginx

    2.指定配置文件的启动方式


    /usr/local/nginx/sbin/nginx -c /tmp/nginx.conf

    3.指定安装目录的启动方式


    /usr/local/nginx/sbin/nginx -p /usr/local/nginx/

    4.指定全局配置项的启动方式


    /usr/local/nginx/sbin/nginx -g "pid /var/nginx/test.pid;"

    5.测试配置信息是否有错误


    /usr/local/nginx/sbin/nginx -t

    6.在测试配置阶段不输出信息


    /usr/local/nginx/sbin/nginx -t -q

    7.显示版本信息


    /usr/local/nginx/sbin/nginx -v

    8.显示编译阶段的参数


    /usr/local/nginx/sbin/nginx -V

    9.快速地停止服务


    /usr/local/nginx/sbin/nginx -s stop

    10、优雅地停止服务


    /usr/local/nginx/sbin/nginx -s quit

    11.使运行中的Nginx重读配置项并生效


    /usr/local/nginx/sbin/nginx -s reload

    12.日志文件回滚


    /usr/local/nginx/sbin/nginx -s reopen

    二、Nginx的配置

    1.块配置项:由一个块配置项名和一个大括号组成

    2.配置项的语法格式:

    配置项名 配置项值1 配置项值2 ...;

    3.在配置中使用变量,加上$符号

    1.Nginx服务的基本配置

    1.1用于调试进程和定位问题的配置项

    1.是否以守护进程方式运行Nginx

    语法:daemon on|off;

    默认:daemon on;

    2.是否以master/worker方式工作

    语法:master_process on|off;

    默认:master_process on;

    3.error日志的设置

    语法:error_log /path/file level;

    默认:error_log logs/error.log error;

    (level:debug/info/notice/warn/error/crit/alert/emerg)

    4.是否处理几个特殊的调试点

    语法:debug_points [stop|abort]

    5.仅对指定的客户端输出debug级别的日志

    语法:debug_connection [IP|CIDR]

    6.限制coredump核心转储文件的大小

    语法:worker_rlimit_core size;

    1.2正常运行的配置项

    1.定义环境变量

    语法:env VAR|VAR=VALUE

    2.嵌入其他配置文件

    语法:include /path/file;

    3.pid文件的路径

    语法:pid path/file;

    默认:pid logs/nginx.pid;

    4.Nginx worker进程运行的用户及用户组

    语法:user username[groupname];

    默认:user nobody nobody;

    5.指定Nginx worker进程可以打开的最大句柄描述符个数

    语法:worker_rlimit_nofile limit;

    6.限制信号队列

    语法:worker_rlimit_sigpending limit;

    1.3优化性能的配置项

    1.Nginx worker进程个数

    语法:worker_processes number;

    默认:worker_processes 1;

    2.绑定Nginx worker进程到指定的CPU内核

    语法: worker_cpu_affinity cpumask[cpumask...]

    3.SSL硬件加速

    语法:ssl_engine device;

    4.系统调用gettimeofday的执行频率

    语法:timer_resolution t;

    5.Nginx worker进程优先级设置

    语法:worker_priority nice;

    默认:worker_priority 0;

    1.4事件类配置项

    1.是否打开accept锁

    语法:accept_mutext [on|off]

    默认:accept_mutext on;

    2.lock文件的路径

    语法:lock_file path/file;

    默认:lock_file logs/nginx.lock;

    3.使用accept锁后到真正建立连接之间的延迟时间

    语法:accept_mutex_delay Nms;

    默认:accept_mutex_delay 500ms;

    4.批量建立连接

    语法:multi_accept [on|off];

    默认:multi_accept off;

    5.选择事件模型

    语法:use [kqueue|rtsig|epoll|/dev/poll|select|poll|eventport];

    默认:Nginx会自动使用最合适的事件模型

    6.每个worker的最大连接数

    语法:worker_connections number;

    2.用HTTP核心模块配置一个静态Web服务器

    2.1虚拟主机与请求的分发

    1.监听端口

    语法:listen address:port [default(deprecated in 0.8.21)|default_server|[backlog=num|rcvbuf=size|sndbuf=size|accept_filter=filter|deferred|bind|ipv6only=[on|off]|ssl]];

    默认:listen 80;

    配置块:server

    参数含义:

    default:将所在的server块作为整个web服务的默认server块

    default_server:同上

    backlog=num:表示TCP中backlog队列的大小。默认为-1,表示不予设置。

    rcvbuf=size:设置监听句柄的SO_RCVBUF参数。

    sndbuf=size:设置监听句柄的SO_SNDBUF参数。

    accept_filter:设置accept过滤器,只对FreeBSD操作系统有用

    deferred:在设置该参数后,若用户发起建立连接请求,并且完成了TCP的三次握手,内核也不会为了这次的连接调度worker进程来处理,只有用户真的发送请求数据时,内核才会唤醒worker进程处理这个连接。

    这个参数适用于大并发的情况下,它减轻了worker进程的负担。

    bind:绑定当前端口/地址对,只有同时对一个端口监听多个地址时才会生效。

    ssl:在当前监听的端口上建立的连接必须基于ssl协议

    2.主机名称

    语法:server_name name [...];

    默认:server_name "";

    配置块:server

    server_name与Host的匹配优先级:

    1)首先选择所有字符串完全匹配的server_name,如www.testweb.com

    2) 其次选择通配符在前面的server_name,如*.testweb.com

    3) 再次选择通配符在后面的server_name,如www.testweb.*

    4)最后选择使用正则表达式才匹配的server_name,如~^.testweb.com$

    3.server_names_hash_bucket_size

    语法:server_names_hash_bucket_size size;

    默认:server_names_hash_bucket_size 32|64|128;

    配置块:http server location

    4.server_names_hash_max_size

    语法:server_names_hash_max_size size;

    默认:server_names_hash_max_size 512;

    配置块:http server location

    5.重定向主机名称的处理

    语法:server_name_in _redirect on|off;

    默认:server_name_in _redirect on;

    配置块:http server location

    6.location

    语法: location [=|~|~*|^~|@]/uri/{...}

    配置块:server

    匹配规则:

    1)=表示把uri作为字符串,以便与参数中的uri做完全匹配。

    2)~表示匹配uri时是字母大小写敏感的

    3)~*表示匹配uri时忽略字母大小写问题

    4)^~表示匹配uri时只需要其前半部分与uri参数匹配即可

    5)@表示仅用于Nginx服务内部请求之间的重定向,带有@的location不直接处理用户请求

    2.2文件路径的定义

    1.以root方式设置资源路径

    语法:root path

    默认:root html;

    配置块:http server location if

    2.以alias方式设置资源路径

    语法:alias path;

    配置块:location

    3.访问首页

    语法:index file ...;

    默认:index index.html;

    配置块:http server location

    4.根据http返回码重定向页面

    语法:error_page code [ code... ]空格[=|=answer-code] uri|@named_location

    配置块:http server location if

    error_page 404 /404.html

    5.是否允许递归使用error_page

    语法:recursive_error_pages [on|off];

    默认:recursive_error_pages off;

    配置块:http server location

    6.try_files

    语法:try_files path1 [path2] uri;

    配置块: server location

    2.3内存及磁盘资源的分配

    1.http包体只存储到磁盘文件中

    语法:client_body_in_file_only on|clean|off;

    默认:client_body_in_file_only off;

    配置块:http server location

    2.http包体尽量写入到一个内存buffer中

    语法:client_body_in_single_buffer on|off;

    默认:client_body_in_single_buffer off;

    配置块:http server location

    3.存储http头部的内存buffer大小

    语法:client_header_buffer_size size;

    默认:client_header_buffer_size 1k;

    配置块:http server

    4.存储超大http头部的内存buffer大小

    语法:large_client_header_buffers number size;

    默认:large_client_header_buffers 4 8k;

    配置块:http server

    5.存储http包体的内存buffer大小

    语法:client_body_buffer_size size;

    默认:client_body_buffer_size 8k/16k;

    配置块:http server location

    6.http包体的临时存放目录

    语法:client_body_temp_path dir-path [level1 [level2[level3]]]

    默认:client_body_temp_path client_body_temp;

    配置块:http server location

    7.connection_pool_size

    语法:connection_pool_size size;

    默认:connection_pool_size 256;

    配置块:http server

    8.request_pool_size

    语法:request_pool_size size;

    默认:request_pool_size 4k;

    配置块:http server

    2.4网络连接的设置

    1.获取http头部的超时时间

    语法:client_header_timeout time(默认单位:秒);

    默认:client_header_timeout 60;

    配置块:http server location

    2.读取http包体的超时时间

    语法:client_body_timeout time(默认:秒);

    默认:client_body_timeout 60;

    配置块:http server location

    3.发送响应的超时时间

    语法:send_timeout time;

    默认:send_timeout 60;

    配置块:http server location

    4.reset_timeout_connection

    语法:reset_timeout_connection on|off;

    默认:reset_timeout_connection off;

    配置块:http server location

    5.lingering_close

    语法:lingering_close off|on|always;

    默认:lingering_close on;

    配置块:http server location

    6.lingering_time

    语法:lingering_time time;

    默认:lingering_time 30s;

    配置块:http server location

    7.lingering_timeout

    语法:lingering_timeout time;

    默认:lingering_timeout 5s;

    配置块:http server location

    8.对某些浏览器禁用keepalive功能

    语法:keepalive_disable [msie6|safari|none]...

    默认:keepalive_disable msie6 safari

    配置块:http server location

    9.keepalive超时时间

    语法:keepalive_timeout time(默认单位:秒);

    默认:keepalive_timeout 75;

    配置块:http server location

    10.一个keepalive长连接上允许承载的请求最大数

    语法:keepalive_requests n;

    默认:keepalive_requests 100;

    配置块:http server location

    11.tcp_nodelay

    语法:tcp_nodelay on|off;

    默认:tcp_nodelay on;

    配置块:http server location

    12.tcp_nopush

    语法:tcp_nopush on|off;

    默认:tcp_nopush off;

    配置块:http server location

    2.5MIME类型的设置

    1.MIME type与文件扩展的映射

    语法:type {...};

    配置块:http server location

    2.默认MIME type

    语法:default_type MIME-type

    默认:default_type text/plain;

    配置块:http server location

    2.6对客户端请求的限制

    2.7文件操作的优化

    2.8对客户端请求的特殊处理

    2.9ngx_http_core_module 模块提供的变量

    3.用HTTP proxy module 配置一个反向代理服务器

    3.1负载均衡的基本配置

    1.upstream块

    2.server

    3.ip_hash

    4.记录日志时支持的变量

    3.2反向代理的基本配置

    1.proxy_pass

    2.proxy_method

    3.proxy_hide_header

    4.proxy_pass_header

    5.proxy_pass_request_body

    6.proxy_pass_request_headers

    7.proxy_redirect

    8.proxy_next_upstream

     

  • 相关阅读:
    python并发之concurrent.futures
    Twisted 简介
    Python进程、线程、协程的对比
    协程的优点(Python)
    python concurrent.futures
    gevent 协程 使用
    python高性能代码之多线程优化
    python并发编程之多进程、多线程、异步和协程
    Java——动态代理
    NHibernate之旅(14):探索NHibernate中使用视图
  • 原文地址:https://www.cnblogs.com/mufeng07/p/12626491.html
Copyright © 2020-2023  润新知