nginx入门学习——编译安装 原

时间:2022-06-19
本文章向大家介绍nginx入门学习——编译安装 原,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

忙里偷闲,复盘一下nginx相关技术知识。

布景:nginx简介

Nginx是一款高性能的Web服务器、邮件服务器和反向代理服务器,由俄罗斯人Igor Sysoev(伊戈尔·赛索耶夫)开发的。

nginx优缺点

俗话说没有对比就没有伤害,相对Apache来说,nginx有以下优缺点:

优点

  • 高并发量:根据官方给出的数据,能够支持高达 50,000 个并发连接数的响应
  • 内存消耗少:处理静态文件,同样起web 服务,比apache 占用更少的内存及资源,所有它是轻量级的
  • 简单稳定:配置简单,基本在一个conf文件中配置,性能比较稳定,可以7*24小时长时间不间断运行
  • 模块化程度高:Nginx是高度模块化的设计,编写模块相对简单,包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter,支持 SSL 和 TLSSNI。
  • 支持Rwrite重写规则:能够根据域名、URL的不同, 将HTTP请求分发到不同的后端服务器群组。
  • 低成本:Nginx可以做高并发的负载均衡,且Nginx是开源免费的,如果使用F5等硬件来做负载均衡,硬件成本比较高。
  • 支持多系统:Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows,由于Nginx是免费开源的,可以在各系统上编译并使用。

缺点

  • 动态处理差:nginx处理静态文件好,耗费内存少,但是处理动态页面则很鸡肋,现在一般前端用nginx作为反向代理抗住压力,apache作为后端处理动态请求。
  • rewrite弱:虽然nginx支持rewrite功能,但是相比于Apache来说,Apache比nginx 的rewrite 强大。

nginx文件介绍

$ tree -L 1
.
├── auto  # 编译安装相关的文件
├── CHANGES  # NGINX变更历史
├── CHANGES.ru  # 俄语版
├── conf  # 配置文件模板,安装时会拷贝到nginx安装目录
├── configure  # 用来生成编译文件
├── contrib  # 两个perl脚本及vim命令符合nginx语法的相关配置,cp -r contrib/vim/* ~/.vim/
├── html  # 两个标准html文件,50x错误页面以及nginx默认页面
├── LICENSE
├── man  # nginx命令帮助文件
├── README
└── src  # nginx源代码文件

nginx安装示例

$ ./configure --prefix=/home/nginx
$ ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
# configure完成后会生成objs目录,其中的ngx_modules.c文件中的内容为之后编译到nginx的所有模块内容

$ make
$ ll objs/ 
总用量 3780
-rw-r--r-- 1 root root   17763 3月  24 22:53 autoconf.err
-rw-r--r-- 1 root root   39263 3月  24 22:53 Makefile
-rwxr-xr-x 1 root root 3746432 3月  24 22:56 nginx
-rw-r--r-- 1 root root    5321 3月  24 22:56 nginx.8
-rw-r--r-- 1 root root    6794 3月  24 22:53 ngx_auto_config.h
-rw-r--r-- 1 root root     657 3月  24 22:53 ngx_auto_headers.h
-rw-r--r-- 1 root root    5725 3月  24 22:53 ngx_modules.c
-rw-r--r-- 1 root root   31872 3月  24 22:56 ngx_modules.o
drwxr-xr-x 9 root root      91 3月  24 22:53 src
# 编译(make)完成后会生成用于运行的nginx二进制文件,以及c语言编译时生成的所有中间文件都放在src目录

$ make install   # 首次安装时使用
$ ls /home/nginx/   # 安装完成
conf  html  logs  sbin
$ tree /home/nginx/ -L 1
/home/nginx/
├── conf  # 配置文件夹
├── html
├── logs  # 日志文件夹
└── sbin  # 二进制文件夹

nginx二进制存放文件位置

为什么要知道nginx二进制文件存放位置?

因为在进行nginx版本升级时,不能直接执行 make install ,需要将该二进制文件拷贝到原有 /path/to/nginx/sbin/ 中。

正片:安装nginx

如果你的nginx想支持https协议、gzip压缩、正则,需要安装以下外部依赖库:OpenSSL、pcre、zlib等。

Note: 只需要准备OpenSSL、pcre、zlib源码包即可,安装会由nginx的configure、make自动完成。(关于这个问题后面有惊喜!)

configure

$ ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx --with-http_auth_request_module --with-http_realip_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-ld-opt=-ljemalloc --with-openssl=/usr/local/src/openssl-1.1.1b --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --add-module=/usr/local/src/nginx-module-vts --add-module=/usr/local/src/nginx-http-concat/ --add-module=/usr/local/src/ngx_cache_purge/ --add-module=/usr/local/src/echo-nginx-module/
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
checking for gcc -pipe switch ... found
checking for --with-ld-opt="-ljemalloc" ... not found
./configure: error: the invalid value in --with-ld-opt="-ljemalloc"

找不到 jemalloc 库,解决办法:

$ yum install -y jemalloc jemalloc-devel

安装完成后重新 configure 即可!

关于jemalloc

make

起初手动安装了OpenSSL、pcre、zlib,第一次进行configure的时候pcre、openssl、zlib使用的其安装路径,如 /usr/local/pcre ,结果在进行make的时候报错:

$ make 
make -f objs/Makefile 
make[1]: Entering directory `/usr/local/src/nginx-0.8.54′ 
cd /usr/local/pcre / 
        && if [ -f Makefile ]; then make distclean; fi / 
        && CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " / 
        ./configure –disable-shared 
/bin/sh: line 2: ./configure: No such file or directory 
make[1]: *** [/usr/local/pcre/Makefile] Error 127 
make[1]: Leaving directory `/usr/local/src/nginx-0.8.54′ 
make: *** [build] Error 2

根据报错信息可以看到,在进行make的时候找不到pcre、openssl、zlib对应目录下的Makefile文件,nginx怎么会到安装目录下找Makefile文件呢?查看nginx的Makefile文件发现下面一段代码:

/usr/local/src/openssl-1.1.1b/.openssl/include/openssl/ssl.h:   objs/Makefile
        cd /usr/local/src/openssl-1.1.1b 
        && if [ -f Makefile ]; then $(MAKE) clean; fi 
        && ./config --prefix=/usr/local/src/openssl-1.1.1b/.openssl no-shared no-threads  
        && $(MAKE) 
        && $(MAKE) install_sw LIBDIR=lib

即,新版本nginx完成configure后生产的Makefile文件中默认会到openssl等源文件自动编译安装。所以只需要准备好 pcre、openssl、zlib 即可,不需要再手动编译安装。于是更改configure时OpenSSL、pcre、zlib的路径为源码路径,再次编译,顺利通关!

关于这个问题 官方文档 也有说到:

--with-pcre=*path*

sets the path to the sources of the PCRE library. The library distribution (version 4.4 — 8.42) needs to be downloaded from the PCRE site and extracted. The rest is done by nginx’s ./configure andmake. The library is required for regular expressions support in the location directive and for thengx_http_rewrite_module module.

所以说,认真看文档还是很有用的!!!

检查安装结果

$ cd /usr/local/nginx
$ ./sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.1.1b  26 Feb 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_auth_request_module --with-http_realip_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-ld-opt=-ljemalloc --with-openssl=/usr/local/src/openssl-1.1.1b --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --add-module=/usr/local/src/nginx-module-vts --add-module=/usr/local/src/nginx-http-concat/ --add-module=/usr/local/src/ngx_cache_purge/ --add-module=/usr/local/src/echo-nginx-module/

即,安装完成!

话外音:关于几个第三方模块

第三方模块下载后编译到NGINX即可--add-module=/path/to/nginx_module ,具体功能及用法参考README。

nginx-module-vts

用于Prometheus监控。

$ git clone git://github.com/vozlt/nginx-module-vts.git

nginx_http_concat

$ git clone git://github.com/alibaba/nginx-http-concat.git

nginx_cache_purge

$ git clone https://github.com/FRiCKLE/ngx_cache_purge.git

echo-nginx-module

$ git clone https://github.com/openresty/echo-nginx-module.git

(adsbygoogle = window.adsbygoogle || []).push({});