[dev][nginx] 在阅读nginx代码之前都需要准备什么

前言

以前,我读过nginx的源码,甚至还改过。但是,现在回想起来几乎回想不起任何东西,

只记得到处都是回调和异步,我的vim+ctags索引起来十分吃力。

几乎没有任何收获,都是因为当时打开代码就看,完全不了解背景和设计思想,只知其然不知其所以然。

如今,做好准备工作,再学习一下。

简单的时候,这个准备工作分两步:

1. 掌握一般性的http,https知识。(应该是都掌握的,不然也没有读nginx代码的理由)

2. 把这个页https://nginx.org/en/docs/ 从上到下读一遍。(我当然是跳着读的)

 Author: classic_tong

详细如下:

一  获取代码

nginx官网提供的源码在这个位置:http://hg.nginx.org/nginx

github上有一个mirror: https://github.com/nginx/nginx

二 编译安装

两步,configure 然后make

[root@T9 nginx.git]# ls
auto  conf  contrib  docs  Makefile  misc  objs  src

顶级目录是如上这样的,configure脚本在auto下面,但是必须在顶级目录运行。。如下这样:

./auto/configure --prefix=/root/OUTPUT_nginx/ 
                --with-http_ssl_module 
                --with-http_v2_module 
                --with-http_gunzip_module 
                --with-stream 
                --with-stream_ssl_module 
                --with-debug

然后,make,Make install,就装好了:

[root@T9 nginx.git]# ll /root/OUTPUT_nginx/
total 4
drwx------ 2 nobody root    6 Sep  9 11:29 client_body_temp
drwxr-xr-x 2 root   root 4096 Sep 11 11:58 conf
drwx------ 2 nobody root    6 Sep  9 11:29 fastcgi_temp
drwxr-xr-x 2 root   root   40 Sep  9 11:20 html
drwxr-xr-x 2 root   root   58 Sep  9 16:03 logs
drwx------ 2 nobody root    6 Sep  9 11:29 proxy_temp
drwxr-xr-x 2 root   root   36 Sep  9 16:02 sbin
drwx------ 2 nobody root    6 Sep  9 11:29 scgi_temp
drwx------ 2 nobody root    6 Sep  9 11:29 uwsgi_temp

三 使用

(1)

直接运行二进制./sbin/nginx 会驱动nginx的daemon。一个master若干work进程:

[root@T9 OUTPUT_nginx]# ps -ef |grep nginx
root      9595     1  0 Sep09 ?        00:00:00 nginx: master process ./nginx
nobody    9596  9595  0 Sep09 ?        00:00:00 nginx: worker process

使用,-s参数可以进行不同的daemon交互。例如 -s reload可以重新加载配置。

(2)最简配置

做了个最简配置,运行使用一下:

[root@T9 OUTPUT_nginx]# cat conf/nginx.conf
events {
}
http {
        server {
                location / {
                        root /data/www;
                }
                location /images/ {
                        root /data;
                }
        }
}
[root@T9 OUTPUT_nginx]# tree /data/
/data/
├── images
│   └── 1.png
└── www
    └── 1.html

在此基础上,通过以下文档,理解最基本的概念:

https://nginx.org/en/docs/beginners_guide.html

四 其他配置实验

通过文档完成其他场景的配置实验:

如何配置https server: https://nginx.org/en/docs/http/configuring_https_servers.html

如何配置Load Balance:https://nginx.org/en/docs/http/load_balancing.html

五 熟悉一般流程

nginx 处理一个请求的一般过程:https://nginx.org/en/docs/http/request_processing.html

nginx 处理一个stream的一般过程:https://nginx.org/en/docs/stream/stream_processing.html

六 nginx开发者指南

必须详细阅读,不可略过:https://nginx.org/en/docs/dev/development_guide.html

七 nginx哲学

非常好,非常受益。讲了思想和缘由,一定要读。(包括为什么全是异步和回调)

必须详细阅读,不可略过:http://www.aosabook.org/en/nginx.html

八 可以读源码了

读读读 

九 书

代码其实没那么好读。为了效率,这本书很好:

 另一本书,tengine团队编写的,我并没看,浏览目录还不错。

http://tengine.taobao.org/book/index.html#

十 更多

第三方模块列表:https://www.nginx.com/resources/wiki/modules/index.html

原文地址:https://www.cnblogs.com/hugetong/p/11507613.html