[nginx] 从源码编译安装NGINX

时间:2023-03-08 19:45:54

nginx通过rpm包进行的安装和配置:

[web][nginx] 初识nginx -- 使用nginx搭建https DPI解码测试环境

现在,要通过源码进行安装。

参考:https://nginx.org/en/docs/configure.html

1. 下载PCRE-8.41   https://ftp.pcre.org/pub/pcre/ 并解压。

2.  安装 zlib

yum install zlib-devel

3.  configure

root@D128 ~/S/nginx-1.13.# ./configure --prefix=/root/BUILD --with-pcre=/root/Src/pcre-8.41/
root@D128 ~/S/nginx-1.13.9# make

4.  安装

root@D128 ~/S/nginx-1.13.# make install

安装完了,在这里

root@D128 ~/BUILD# ls
client_body_temp/ conf/ fastcgi_temp/ html/ logs/ proxy_temp/ sbin/ scgi_temp/ uwsgi_temp/

5. 启动,

执行这个程序,就会用默认配置启动了。

/root/BUILD/sbin/nginx

但是,关掉iptables,selinux之后,依然报错

// :: [error] #: * "/root/BUILD/html/index.html" is forbidden (: Permission denied), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1"
// :: [error] #: * "/root/BUILD/html/index.html" is forbidden (: Permission denied), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1"

原来是因为我放在了 /root/目录下,root的目录权限导致了这个问题。

root@D128 /# chmod  root/

6 GDB

默认编译文件objs/Makefile就是-g的,所以默认就能gdb

https://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/

(gdb) set follow-fork-mode child

在fork后的子进程中设置断点:

(gdb) b ngx_daemon.c:
Breakpoint at 0x42f014: file src/os/unix/ngx_daemon.c, line .

nginx的进程启动大流程: (配置文件的初始化在第一次fork之前进行。)

[nginx] 从源码编译安装NGINX

7. loction

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

今天重点研究的侧重点是location与pcre的问题,用来提升7层LB的性能。

现在,要搞清楚下面两个函数的调用契机和逻辑

ngx_http_core_find_config_phase

    ngx_http_core_find_location 调用了 PCRE (ngx_http_regex_exec -> pcre_exec)

ngx_http_core_location

    ngx_http_core_regex_location 调用了PCRE (ngx_http_regex_compile)

从如下代码中可以发现,该ngx_http_core_location函数用于做location关键字的配置

src/http/ngx_http_core_module.c: line 273

      { ngx_string("location"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
ngx_http_core_location,
NGX_HTTP_SRV_CONF_OFFSET,
,
NULL },