Back
Please upgrade your browser or check your network connection.

编译适合自己的Nginx

安装Nginx

一般来说,安装Nginx无非就是那几种方式,从仓库里面安装,拿别人编译好的二进制文件,自己源码进行编译。

下面这两个方式无疑都是较快的安装方式。

  • 从仓库里面安装

  • 拿别人编译好的二进制文件

但是里面的模块,不是你自己能够定制的。一般来说,这种编译好的,都是不能够满足于个性化需求的。所以需要我们自己从源码里面去编译Nginx。


下载Nginx

先从Nginx的开源官网下载最新的stable版本

1wget http://nginx.org/download/nginx-1.14.2.tar.gz
2
3tar xzf nginx-1.14.2.tar.gz
4
5x nginx-1.14.2.tar.gz #装有 zsh 插件x 可用(一样是解压的啦)

介绍各目录以及部分文件

 1nginx-1.14.2
 2├── CHANGES
 3├── CHANGES.ru
 4├── LICENSE
 5├── README
 6├── auto          d
 7├── conf          d
 8├── configure
 9├── contrib       d
10├── html          d
11├── man           d
12└── src           d
13
146 directories, 5 files

文件 CHANGES

 1Changes with nginx 1.14.2                                        04 Dec 2018
 2
 3    *) Bugfix: nginx could not be built by gcc 8.1.
 4
 5    *) Bugfix: nginx could not be built on Fedora 28 Linux.
 6
 7    *) Bugfix: in handling of client addresses when using unix domain listen
 8       sockets to work with datagrams on Linux.
 9
10    *) Change: the logging level of the "http request", "https proxy
11....

记录的是Nginx的一些版本的Bugfix,一些feature。


文件 CHANGES.ru

 1
 2Изменения в nginx 1.14.2                                          04.12.2018
 3
 4    *) Исправление: nginx не собирался gcc 8.1.
 5
 6    *) Исправление: nginx не собирался на Fedora 28 Linux.
 7
 8    *) Исправление: в обработке адресов клиентов при использовании unix
 9       domain listen-сокетов для работы с датаграммами на Linux.
10.....

记录的是Nginx的一些版本的Bugfix,一些feature。 因为Nginx的作者是一个俄罗斯人,有个俄罗斯文的说明很正常。


文件 LICENSE

 1/*
 2 * Copyright (C) 2002-2018 Igor Sysoev
 3 * Copyright (C) 2011-2018 Nginx, Inc.
 4 * All rights reserved.
 5 *
 6 * Redistribution and use in source and binary forms, with or without
 7 * modification, are permitted provided that the following conditions
 8 * are met:
 9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11....

Nginx的开源协议说明


文件 README

1
2Documentation is available at http://nginx.org
3

告诉你可以在哪里找到文档。


目录 auto

 1auto
 2├── cc              d
 3├── define
 4├── endianness
 5├── feature
 6├── have
 7├── have_headers
 8├── headers
 9├── include
10├── init
11├── install
12├── lib             d
13├── make
14├── module
15├── modules
16├── nohave
17├── options
18├── os              d
19├── sources
20├── stubs
21├── summary
22├── threads
23├── types           d
24└── unix
25
264 directories, 19 files

cc,lib库 都是为了编译的。

 1cc
 2
 3total 96
 4drwxr-xr-x  13 hello  staff   416 Dec  4 22:52 .
 5drwxr-xr-x  25 hello  staff   800 Dec  4 22:52 ..
 6-rw-r--r--   1 hello  staff   201 Dec  4 22:52 acc
 7-rw-r--r--   1 hello  staff  1275 Dec  4 22:52 bcc
 8-rw-r--r--   1 hello  staff  1324 Dec  4 22:52 ccc
 9-rw-r--r--   1 hello  staff  1799 Dec  4 22:52 clang
10-rw-r--r--   1 hello  staff  5269 Dec  4 22:52 conf
11-rw-r--r--   1 hello  staff  3588 Dec  4 22:52 gcc
12-rw-r--r--   1 hello  staff  2874 Dec  4 22:52 icc
13-rw-r--r--   1 hello  staff  3285 Dec  4 22:52 msvc
14-rw-r--r--   1 hello  staff  1568 Dec  4 22:52 name
15-rw-r--r--   1 hello  staff  1862 Dec  4 22:52 owc
16-rw-r--r--   1 hello  staff  3516 Dec  4 22:52 sunc
17
18lib
19
20total 16
21-rw-r--r--  1 hello  staff   952B Dec  4 22:52 conf
22drwxr-xr-x  3 hello  staff    96B Dec  4 22:52 geoip
23drwxr-xr-x  3 hello  staff    96B Dec  4 22:52 google-perftools
24drwxr-xr-x  4 hello  staff   128B Dec  4 22:52 libatomic
25drwxr-xr-x  3 hello  staff    96B Dec  4 22:52 libgd
26drwxr-xr-x  3 hello  staff    96B Dec  4 22:52 libxslt
27-rw-r--r--  1 hello  staff   482B Dec  4 22:52 make
28drwxr-xr-x  6 hello  staff   192B Dec  4 22:52 openssl
29drwxr-xr-x  7 hello  staff   224B Dec  4 22:52 pcre
30drwxr-xr-x  4 hello  staff   128B Dec  4 22:52 perl
31drwxr-xr-x  7 hello  staff   224B Dec  4 22:52 zlib
32

os 是为了判断操作系统的。

1total 56
2-rw-r--r--  1 hello  staff   2.6K Dec  4 22:52 conf
3-rw-r--r--  1 hello  staff   3.0K Dec  4 22:52 darwin
4-rw-r--r--  1 hello  staff   2.3K Dec  4 22:52 freebsd
5-rw-r--r--  1 hello  staff   5.4K Dec  4 22:52 linux
6-rw-r--r--  1 hello  staff   1.4K Dec  4 22:52 solaris
7-rw-r--r--  1 hello  staff   892B Dec  4 22:52 win32

其他的都是为了辅助脚本configure执行时候,去判定当前的Nginx支持哪些模块,当前的操作系统有哪些特性可以提供给Nginx使用。


目录 conf

 1conf
 2├── fastcgi.conf
 3├── fastcgi_params
 4├── koi-utf
 5├── koi-win
 6├── mime.types
 7├── nginx.conf
 8├── scgi_params
 9├── uwsgi_params
10└── win-utf
11
120 directories, 9 files

这个目录都是一些示例文件,方便运维去配置。在make install的时候会拷贝到安装目录。


文件 configure

用来生成中间文件,进行编译前的必须操作。

稍后有详细的解析….


目录 contrib

 1contrib
 2├── README
 3├── geo2nginx.pl
 4├── unicode2nginx
 5│   ├── koi-utf
 6│   ├── unicode-to-nginx.pl
 7│   └── win-utf
 8└── vim
 9    ├── ftdetect
10    │   └── nginx.vim
11    ├── ftplugin
12    │   └── nginx.vim
13    ├── indent
14    │   └── nginx.vim
15    └── syntax
16        └── nginx.vim
17
186 directories, 9 files

这里的README都已经介绍了。

 1
 2geo2nginx.pl 		by Andrei Nigmatulin
 3
 4	The perl script to convert CSV geoip database ( free download
 5	at http://www.maxmind.com/app/geoip_country ) to format, suitable
 6	for use by the ngx_http_geo_module.
 7
 8
 9unicode2nginx		by Maxim Dounin
10
11	The perl script to convert unicode mappings ( available
12	at http://www.unicode.org/Public/MAPPINGS/ ) to the nginx
13	configuration file format.
14	Two generated full maps for windows-1251 and koi8-r.
15
16
17vim			by Evan Miller
18
19	Syntax highlighting of nginx configuration for vim, to be
20	placed into ~/.vim/.
21

两个用于转换的perl脚本,一个vim的Nginx高亮配置。

没有配置vim前,打开Nginx的配置文件:

现在将高亮的配置丢到~/.vim/

1mkdir -p ~/.vim/
2cp -r contrib/vim/* ~/.vim/
3
4注意文件的移动放置,仅仅盯住命令是没有用的。
5
6- Copy a directory recursively to another location:
7  cp -r path/to/directory path/to/copy
8

其实这里我是在centos7里面截图的233,因为我在macOS下面踩了坑,没配出来。


目录 html

1html
2├── 50x.html
3└── index.html
4
50 directories, 2 files

一个是碰到50x错误的时候的页面,一个是安装好Nginx时候正常加载的页面。


目录 man

1man
2└── nginx.8
3
40 directories, 1 file

使用man ./nginx.8


目录 src

 1src
 2├── core
 3├── event
 4├── http
 5├── mail
 6├── misc
 7├── os
 8└── stream
 9
107 directories, 0 files

源代码,Nginx的整个框架就在里面。

Configure

先看看configure的命令行参数

1./configure --help

可以上面的命令得到它的命令行参数.

这里主要讲几个大块:

  1. PATH
1--prefix=PATH                      set installation prefix ## Nginx的安装路径
2--sbin-path=PATH                   set nginx binary pathname
3--modules-path=PATH                set modules path
4--conf-path=PATH                   set nginx.conf pathname
5--error-log-path=PATH              set error log pathname
6--pid-path=PATH                    set nginx.pid pathname
7--lock-path=PATH                   set nginx.lock pathname

这里是指定在编译之后,文件存放的地方。

一般就是直接指定--prefix=PATH这个参数就好了。

nginx binarymodulesnginx.conferror lognginx.pidnginx.lock,这些文件都会自己在指定的prefix目录下,自己建个文件夹来安顿好自己。

  1. 使用/禁用自身模块
 1
 2--with-select_module               enable select module
 3--without-select_module            disable select module
 4--with-poll_module                 enable poll module
 5--without-poll_module              disable poll module
 6
 7--with-threads                     enable thread pool support
 8
 9--with-file-aio                    enable file AIO support
10

前面带without的,基本都是禁用模块的。

而带with的,一般都是启用模块的。

而且通常,像下面的这种带with的,需要自己手动加参数启用的。

在默认编译里面,一般都是不启用该模块的。

1--with-http_ssl_module             enable ngx_http_ssl_module
2--with-http_v2_module              enable ngx_http_v2_module
3--with-http_realip_module          enable ngx_http_realip_module
4--with-http_addition_module        enable ngx_http_addition_module
5--with-http_xslt_module            enable ngx_http_xslt_module
6--with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
7--with-http_image_filter_module    enable ngx_http_image_filter_module
8--with-http_image_filter_module=dynamic  enable dynamic ngx_http_image_filter_module

而下面这种带without的,一般都是默认启用该模块的。当你加上下面的参数进行编译的时候才会禁用它。

 1  --without-http_charset_module      disable ngx_http_charset_module
 2  --without-http_gzip_module         disable ngx_http_gzip_module
 3  --without-http_ssi_module          disable ngx_http_ssi_module
 4  --without-http_userid_module       disable ngx_http_userid_module
 5  --without-http_access_module       disable ngx_http_access_module
 6  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
 7  --without-http_mirror_module       disable ngx_http_mirror_module
 8  --without-http_autoindex_module    disable ngx_http_autoindex_module
 9  --without-http_geo_module          disable ngx_http_geo_module
10  --without-http_map_module          disable ngx_http_map_module
  1. 第三方模块/编译优化
 1--with-cc=PATH                     set C compiler pathname
 2--with-cpp=PATH                    set C preprocessor pathname
 3--with-cc-opt=OPTIONS              set additional C compiler options
 4--with-ld-opt=OPTIONS              set additional linker options
 5--with-cpu-opt=CPU                 build for the specified CPU, valid values:
 6                                   pentium, pentiumpro, pentium3, pentium4,
 7                                   athlon, opteron, sparc32, sparc64, ppc64
 8
 9--without-pcre                     disable PCRE library usage
10--with-pcre                        force PCRE library usage
11--with-pcre=DIR                    set path to PCRE library sources
12--with-pcre-opt=OPTIONS            set additional build options for PCRE
13--with-pcre-jit                    build PCRE with JIT compilation support
14
15--with-zlib=DIR                    set path to zlib library sources
16--with-zlib-opt=OPTIONS            set additional build options for zlib
17--with-zlib-asm=CPU                use zlib assembler sources optimized
18                                   for the specified CPU, valid values:
19                                   pentium, pentiumpro
20

这里的参数,你可以指定像c c++编译器的位置,以及优化的参数。以及一些第三方库的位置设定以及优化参宿等等。

开始执行Configure生成中间文件

这里指定位置

./configure --prefix=/home/jane/preview

在执行的过程中,可能需要安装相关依赖。

yum install gcc gcc-c++ -y

http_rewrite_module需要PCRE library

需要的话这个模块,就装PCRE library

如果不需要,加上参数--without-http_rewrite_module即可。

就是关掉这个模块,就不用相关的依赖啦。

http_gzip_module需要zlib library

如果不需要,加上参数--without-http_gzip_module即可。

我知道你们都是要装的了=。=

yum install pcre-devel zlib-devel

在出现各种Nginx的路径的时候就执行完了。


中间文件,都放在了objs。执行完上一步就会出现了。

 1objs/
 2├── autoconf.err
 3├── Makefile
 4├── ngx_auto_config.h
 5├── ngx_auto_headers.h
 6├── ngx_modules.c
 7└── src
 8    ├── core
 9    ├── event
10    │   └── modules
11    ├── http
12    │   ├── modules
13    │   │   └── perl
14    │   └── v2
15    ├── mail
16    ├── misc
17    ├── os
18    │   ├── unix
19    │   └── win32
20    └── stream
21

这里比较重要的就是 ngx_modules.c,里面记录了将要编译进去Nginx的模块。

编译

现在make,进行编译。

编译完成之后在objs目录里面,就会生成大量的中间文件以及我们最终需要运行的Nginx二进制执行文件。

 1total 3780
 2-rw-rw-r--. 1 jane jane   17763 Feb 15 13:51 autoconf.err
 3-rw-rw-r--. 1 jane jane   39349 Feb 15 13:51 Makefile
 4-rwxrwxr-x. 1 jane jane 3746432 Feb 15 14:36 nginx
 5-rw-rw-r--. 1 jane jane    5329 Feb 15 14:36 nginx.8
 6-rw-rw-r--. 1 jane jane    6798 Feb 15 13:51 ngx_auto_config.h
 7-rw-rw-r--. 1 jane jane     657 Feb 15 13:51 ngx_auto_headers.h
 8-rw-rw-r--. 1 jane jane    5725 Feb 15 13:51 ngx_modules.c
 9-rw-rw-r--. 1 jane jane   31872 Feb 15 14:36 ngx_modules.o
10drwxrwxr-x. 9 jane jane      91 Feb 15 13:51 src

为什么需要知道这个目录,因为在你升级的时候,只是更换Nginx的二进制文件,其他的都不用变。

C语言编译生成的中间文件都会放在src目录。

如果使用了动态模块,则会生成动态文件so,ngx_modules.o

安装

首次安装可以使用make install

如果是版本升级则不在本篇文章中。

看到make一顿猛操作,并且没有报错的话,那你就安装完成了。

 1这个是指定安装的目录
 2preview/
 3├── conf #控制Nginx的文件在这里
 4│   ├── fastcgi.conf
 5│   ├── fastcgi.conf.default
 6│   ├── fastcgi_params
 7│   ├── fastcgi_params.default
 8│   ├── koi-utf
 9│   ├── koi-win
10│   ├── mime.types
11│   ├── mime.types.default
12│   ├── nginx.conf
13│   ├── nginx.conf.default
14│   ├── scgi_params
15│   ├── scgi_params.default
16│   ├── uwsgi_params
17│   ├── uwsgi_params.default
18│   └── win-utf
19├── html
20│   ├── 50x.html
21│   └── index.html
22├── logs #Nginx的日志文件在这里
23└── sbin
24    └── nginx
25