一、环境说明
序号 | 操作系统(版本号) | 基础软件(版本号) |
---|---|---|
001 | CentOS/RedHat/Oracle Linux 6/7 x86_64 | nginx 1.8.0 |
Nginx服务器实现如下场景和需求:
(1) 作为http协议静态资源服务器,支持SSL加密。
(2) 作为http协议反向代理服务器,支持ssl加密。
(3) 作为http/https正向代理服务器。
(4) TCP协议反向代理转发。
(5) UDP协议反向代理转发。
以上场景可在某一台web服务器同时实现,也可按功能划分分别实现。
二、软件安装
Nginx根据功能需求,可在编译时自行添加和定制官方模块、第三方模块,可以应用最新的安全补丁,可以禁用不使用的模块。
2.1 版本和依赖库
截止2020年8月20日,nginx官方发布的稳定版本最新为1.18.0。Nginx的一些模块需要依赖一些常用的工具库。依赖库原则上要做到独立于操作系统,与 NGINX 编译版本一同打包发布,以便于及时更新和重新编译。以下三个库为必要组件所需库,已将相应版本放置到nginx源码中的src目录中,以便随时编译使用。
- PCRE 支持正则表达式语法。用于 NGINX Core 、Rewrite 模块。
- zlib 支持头部压缩。用于 NGINX Gzip 模块。
- OpenSSL 支持 HTTPS 协议。用于 NGINX SSL 和其它模块。
2.2 安装模块选择
Nginx 源码下载网页:http://nginx.org/download/。模块选择的基本原则:以满足生产配置需要的最小集合为准。在最小集合的基础上可以定制化模块、添加第三方模块、满足特殊系统需求的模块等等。
模块最小集合列表如下(以勾选为准):
[x] http_access_module | [ ] http_auth_basic_module | [ ] http_autoindex_module |
---|---|---|
[x] http_browser_module | [x] http_charset_module | [ ] http_empty_gif_module |
[ ] http_fastcgi_module | [x] http_geo_module | [x] http_gzip_module |
[x] http_limit_conn_module | [x] http_limit_req_module address. | [x] http_map_module |
[ ] http_memcached_module | [x] http_proxy_module | [x] http_referer_module |
[x] http_rewrite_module | [ ] http_scgi_module | [ ] http_ssi_module |
[ ] http_split_clients_module | [x] http_upstream_hash_module | [x] http_upstream_ip_hash_module |
[x] http_upstream_keepalive_module | [x] http_upstream_least_conn_module | [x] http_upstream_zone_module |
[x] http_userid_module | [ ] http_uwsgi_module | [x] http_gzip_static_module |
[x] http_mp4_module | [x] http_realip_module | [x] http_ssl_module |
[x] http_sub_module | [ ] http_v2_module | [x] stream |
[x] stream_ssl_module | [x] stream_realip_module | [ ] stream_geoip_module |
[x] http_secure_link_module | [x] http_slice_module | [X] threads |
[x] ngx_http_proxy_connect_module | [x] nginx_upstream_check_module |
构建到 Nginx 开放源码中的大多数模块都是静态链接的: 它们在编译时构建到 Nginx 开放源码中,并静态地链接到 Nginx 二进制文件。
其中:
- 正向代理模块ngx_http_proxy_connect_module的源码下载网页为https://github.com/chobits/ngx_http_proxy_connect_module。
- 健康检查模块nginx_upstream_check_module的源码下载网页为https://github.com/yaoweibin/nginx_upstream_check_module。
- 会话保持模块nginx-sticky-module-ng目录为: https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src/master/。
- 日志过滤模块ngx_log_if下载链接为:https://github.com/cfsego/ngx_log_if。
将四个模块的源码目录放入nginx源码文件夹src下(已存在于nginx-1.18.0.src.tar.gz中)。
源码结构如下:
[root@ks-allinone src]# tree -L 1 .
.
├── core
├── event
├── http
├── mail
├── misc
├── nginx-sticky-module-ng
├── nginx_upstream_check_module
├── ngx_http_proxy_connect_module
├── ngx_log_if
├── openssl-1.1.1g
├── os
├── pcre-8.44
├── stream
└── zlib-1.2.11
如果需要将模块添加到二进制文件中,需要重新构建 Nginx,并加上如下列的配置选项:
$./configure ... --add-module=/usr/build/nginx-rtmp-module
2.3 安装目录说明
安装目录根:/usr/local/nginx ,也就是配置参数 --prefix 指定的目录,也是 Nginx 的推荐默认目录。该目录中要包含几个子目录:
注意: 该目录是 root 权限,可以根据需要授权给应用用户。
子目录 | 说明 |
---|---|
$PREFIX/logs | 日志存储目录,存放 访问日志、错误日志、nginx.pid |
$PREFIX/sbin | 程序文件目录,存放执行程序 nginx |
$PREFIX/conf | 配置文件目录,存放配置文件,例如:nginx.conf |
2.4 配置模块选项
构建和编译的目录结构:
nginx-x.y.z: Nginx 源文件目录。
nginx-x.y.z/src:Nginx 源代码目录,其子目录中包含库源代码目录和模块源代码目录。