NGINX笔记

作者:刘宾, thomas_liub@hotmail.com
请尊重作者著作权,转载请注明出处,谢谢!


参考

架构

  • 1个master进程,多个worker(对应CPU内核个数)进程
  • master进程接收外部信号,控制worker
    • nginx -s reload
    • 根据配置文件创建listenfd
    • fork worker进程
  • worker进程采用异步非阻塞,epoll
    • 同时处理成千上万请求
    • aparche一般采用多线程同步操作,数千个线程带来内存资源占用,CPU切换开销
    • 在24G内存的机器上,处理的并发请求数达到过200万
  • connection是对TCP的封装
    • ngx_connection_t结构
    • worker_connectons, 每个worer最大连接个数
    • worker_processes * worker_connections * ngx_connection_t
    • 作为反向代理, worker_processes * worker_connections/2
    • worker会维护ngx_accept_disabled计数器,保证连接资源多的worker优先处理
  • 作为客户端请求其他server数据,upstream模块

request处理流程

Nginx模块

Core模块,ngx_core_module

main()
    |_ ngx_master_process_cycle()
        |_ ngx_start_worker_processes()
            |_ ngx_worker_process_cycle()
                |_ ngx_worker_process_init()
                |_ main loop
                    |_ ngx_process_events_and_timers()
                        |_ ngx_event_process_posted(ngx_posted_accept_events)
                        |_ ngx_event_process_posted(ngx_posted_events)
                    |_ ngx_reopen_files()
ngx_events_module:
    |_ ngx_event_process_init()
        |_ ngx_event_accept()
        |_ ngx_event_recvmsg()
ngx_http_module:
    |_ ngx_http_block()
        |_ ngx_http_init_phase_handlers()
        |_ ngx_http_optimize_servers()
            |_ ngx_http_init_listening()
                |_ ngx_http_add_listening()
                    |_ ngx_create_listening()
                    |_ ngx_http_init_connection()
                        |_ ngx_http_wait_request_handler()
                            |_ ngx_http_create_request()
                            |_ ngx_http_process_request_line()
                                |_ ngx_http_read_request_header()
                                |_ ngx_http_parse_request_line()
                                |_ ngx_http_process_request()
                                    |_ ngx_http_request_handler()
                                    |_ ngx_http_handler()
                                |_ ngx_http_process_request_headers()

Nginx配置文件

  • 简单配置项
error_page   500 502 503 504  /50x.html;
  • 复杂配置项
location / {
    root   /home/jizhao/nginx-book/build/html;
    index  index.html index.htm;
}
  • main
    nginx在运行时与具体业务功能(比如http服务或者email服务代理)无关的一些参数,比如工作进程数,运行的身份等。
  • http
    与提供http服务相关的一些配置参数。例如:是否使用keepalive啊,是否使用gzip进行压缩等。
  • server
    http服务上支持若干虚拟主机。每个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也可以建立若干server.每个server通过监听的地址来区分。
  • location
    http服务中,某些特定的URL对应的一系列配置项。
  • mail
    实现email相关的SMTP/IMAP/POP3代理时,共享的一些配置项(因为可能实现多个代理,工作在多个监听地址上)。

编译

  1. 编写config文件
ngx_addon_name=ngx_http_hello_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c"
  1. configure
./configure --prefix=/usr/local/nginx --add-module=/home/liub/project/nginx_gateway/src/cy
  1. make build
  2. make install
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,747评论 24 1,002
  • 本文系转载》》》》》》》》》》》》》》》》 编者按:高可用架构分享及传播在架构领域具有典型意义的文章,本文由陈科在...
    demop阅读 8,099评论 0 7
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,889评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 能在冬天清晨起床出去晨跑的,都是条汉子。 说实话,我大概是从12年就开始折腾想跑步,但是一直都没行动力,早上起不来...
    cora的生活手册阅读 202评论 0 1