目录
1. Nginx概述
1.1 Nginx简介
Nginx (“engine x”) 就是www服务软件,俄罗斯人用c语言开发的,开源。因为开源,淘宝更改的nginx—> tengine( http://tengine.taobao.org/ ) Nginx 一个轻量级(不到1M)但性能很高的 HTTP和 反向代理服务器,也是一个IMAP/POP3/SMTP 服务器。
1.2 Nginx服务端支持的平台
Unix,Linux,Windows都可以。
1.3 Nginx的特点
- 配置简单,灵活,轻量;
- 静态小文件;(一款只能解析静态资源的软件,如:html,js,css,jpg)web服务器。
- 支持高并发;
- 占用资源少;eg: 3w并发量,10个进程,消耗内存才150M。
- 功能种类比较多,但每一个功能都不特别强。
- 支持高并发是得益于nginx支持epoll网络IO模型。(apache使用select模型)
- Nginx 可以配合动态服务,(FASTCGI接口);
- 利用nginx可以对IP限制速度,可以限制链接的个数。
1.4 Nginx服务端支持的平台
- a. www web服务,邮件服务,邮件代理
- b. 负载均衡(反向代理proxy)
- c. web cache(web缓存)
1.5 Nginx支持的常见虚拟主机
注意:一个server标签段就是一个虚拟主机。一般都是一个服务器下面有多个域名。
一个集群下面有多个这样的服务器。
- 1.基于域名的虚拟主机。通过域名来区分虚拟主机。主要应用于外部网站。***
- 2.基于端口的虚拟主机。通过端口来区分虚拟主机。主要应用于内部网站。**
- 3.基于IP的虚拟主机。因为不好记几乎不用。
2. 学习Nginx的必要性
2.1 全球市场占有率逐渐升高
- 根据网站调查显示:Nginx现在全球排名第三,现在市场占有率达35.9%,并且持上升趋势;
http://www.ha97.com/3996.html
https://www.oschina.net/news/90109/october-2017-web-server-survey
https://w3techs.com/technologies/overview/web_server/all
- 其实,国内网站应用nginx更多一些,例如:
新浪:http://www.sina.com/
网易: http://www.163.com/
淘宝:https://world.taobao.com/
菜鸟教程:http://www.runoob.com/
- 打开linux终端,使用命令:curl -I 网址 进行网页抓取(-I参数 查看http响应头处理)
[root@myhost ~]# curl -I www.163.com
HTTP/1.1 200 OK
Date: Tue, 23 Apr 2019 06:47:30 GMT
Server: nginx ##服务器
Content-Type: text/html; charset=GBK
Vary: Accept-Encoding,User-Agent,Accept
Expires: Tue, 23 Apr 2019 06:48:50 GMT
Cache-Control: max-age=80
X-Via: 1.1 PSzjwzdx11dh86:9 (Cdn Cache Server V2.0), 1.1 PSscmydx2mk75:12 (Cdn Cache Server V2.0)
Connection: keep-alive
3. Nginx依赖安装
- PCRE:因为http rewrite module(伪静态/URI改写) requires the PCRE library,所以安装Nginx之前需要安装环境PCRE(Perl Compatible Regular Expressions)perl兼容的正则表达式;
PCRE依赖库官网 - Zlib:Nginx提供gzip模块,需要zlib的支持;
- Openssl:Nginx提供SSL的功能;
3.1 查看Nginx依赖库是否已经安装
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# rpm -qa pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@myhost tools]# rpm -qa libxslt-devel gd-devel perl-ExtUtils-Embed perl-devel GeoIP-devel GeoIP-data gperftools-devel gcc gcc-c++ autoconf automake
3.2 安装Nginx所需要的依赖库
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# yum -y groupinstall "Development Tools" "Development Libraries"
[root@myhost tools]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@myhost tools]# yum -y install libxslt-devel gd-devel perl-ExtUtils-Embed perl-devel GeoIP-devel GeoIP-data gperftools-devel gcc gcc-c++ autoconf automake
[root@myhost tools]# rpm -qa pcre pcre-devel zlib zlib-devel openssl openssl-devel
openssl-devel-1.0.2k-21.el7_9.x86_64
pcre-8.32-17.el7.x86_64
zlib-1.2.7-19.el7_9.x86_64
pcre-devel-8.32-17.el7.x86_64
zlib-devel-1.2.7-19.el7_9.x86_64
openssl-1.0.2k-21.el7_9.x86_64
[root@myhost tools]# rpm -qa libxslt-devel gd-devel perl-ExtUtils-Embed perl-devel GeoIP-devel GeoIP-data gperftools-devel gcc gcc-c++ autoconf automake
GeoIP-devel-1.5.0-14.el7.x86_64
gd-devel-2.0.35-27.el7_9.x86_64
autoconf-2.69-11.el7.noarch
perl-devel-5.16.3-299.el7_9.x86_64
GeoIP-data-1.5.0-14.el7.noarch
gperftools-devel-2.6.1-1.el7.x86_64
automake-1.13.4-3.el7.noarch
gcc-4.8.5-44.el7.x86_64
gcc-c++-4.8.5-44.el7.x86_64
perl-ExtUtils-Embed-1.30-299.el7_9.noarch
libxslt-devel-1.1.28-6.el7.x86_64
注意:上面用yum安装的pcre是系统依赖的pcre,另外需要下载pcre源码包(只需解压,不需要安装),然后编译安装nginx时指定这个pcre的源码包路径,此包的功能是支持地址重写rewrite功能。
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
[root@myhost tools]# wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz/download
[root@myhost tools]# ll pcre-8.37.tar.gz
-rw-r--r--. 1 root root 2041593 Apr 28 2015 pcre-8.37.tar.gz
[root@myhost tools]# tar xf pcre-8.37.tar.gz
[root@myhost tools]# ll
total 10334
drwxr-xr-x. 7 1169 1169 4096 Apr 28 2015 pcre-8.37
-rw-r--r--. 1 root root 2041593 Apr 28 2015 pcre-8.37.tar.gz
[root@myhost tools]# mv pcre-8.37 /usr/local/
[root@myhost tools]# cd /usr/local/
[root@myhost local]# ll
total 20
drwxr-xr-x. 2 root root 6 Aug 12 2015 bin
drwxr-xr-x. 2 root root 6 Aug 12 2015 etc
drwxr-xr-x. 2 root root 6 Aug 12 2015 games
drwxr-xr-x. 2 root root 6 Aug 12 2015 include
drwxr-xr-x. 2 root root 6 Aug 12 2015 lib
drwxr-xr-x. 2 root root 6 Aug 12 2015 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2015 libexec
drwxr-xr-x. 7 1169 1169 4096 Apr 28 2015 pcre-8.37
drwxr-xr-x. 2 root root 6 Aug 12 2015 sbin
drwxr-xr-x. 5 root root 46 Aug 4 2020 share
drwxr-xr-x. 2 root root 6 Aug 12 2015 src
到此,nginx安装环境准备完毕。
4. Nginx安装部署
4.1 首先查看系统是否已经安装了Nginx
[root@myhost tools]# rpm -qa nginx
[root@myhost tools]# rpm -qa|grep nginx
[root@myhost tools]# find / -name nginx
4.2 创建nginx用户
创建一个管理nginx应用的虚拟用户,虚拟用户不能登录,仅仅满足nginx启动服务的一个用户。
[root@myhost tools]# useradd nginx -s /sbin/nologin -M
[root@myhost tools]# id nginx
uid=1004(nginx) gid=1004(nginx) groups=1004(nginx)
4.3 下载Nginx源码安装包(编译安装比yum安装更灵活)
- Mainline version 主线版本
- Stable version 稳定版本
- Legacy versions 老版本,历史版本
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@myhost tools]# ll
total 123044
-rw-r--r--. 1 root root 1032630 Aug 4 2020 nginx-1.16.1.tar.gz
-rw-r--r--. 1 root root 2041593 Apr 28 2015 pcre-8.37.tar.gz
4.3 解压安装包并查看解压包文件
[root@myhost tools]# ll
total 123044
-rw-r--r--. 1 root root 1032630 Aug 4 2020 nginx-1.16.1.tar.gz
-rw-r--r--. 1 root root 2041593 Apr 28 2015 pcre-8.37.tar.gz
[root@myhost tools]# tar xf nginx-1.16.1.tar.gz
[root@myhost tools]# ll
total 120136
drwxr-xr-x. 8 test 1001 4096 Aug 13 2019 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 Aug 4 2020 nginx-1.16.1.tar.gz
-rw-r--r--. 1 root root 2041593 Apr 28 2015 pcre-8.37.tar.gz
[root@myhost tools]# cd nginx-1.16.1
[root@myhost nginx-1.16.1]# ll
total 756
drwxr-xr-x. 6 test 1001 4096 Jul 13 11:29 auto
-rw-r--r--. 1 test 1001 296463 Aug 13 2019 CHANGES
-rw-r--r--. 1 test 1001 452171 Aug 13 2019 CHANGES.ru
drwxr-xr-x. 2 test 1001 4096 Jul 13 11:29 conf
-rwxr-xr-x. 1 test 1001 2502 Aug 13 2019 configure ####
drwxr-xr-x. 4 test 1001 68 Jul 13 11:29 contrib
drwxr-xr-x. 2 test 1001 38 Jul 13 11:29 html
-rw-r--r--. 1 test 1001 1397 Aug 13 2019 LICENSE
drwxr-xr-x. 2 test 1001 20 Jul 13 11:29 man
-rw-r--r--. 1 test 1001 49 Aug 13 2019 README
drwxr-xr-x. 9 test 1001 84 Jul 13 11:29 src
4.5 查看需要编译的参数
[root@myhost nginx-1.16.1]# ./configure --help
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
...
...
...
#只列出了一小部分,其他的可以自行查看
4.6 自定义编译参数进行编译安装
4.6.1 编译(平时用的编译参数)
[root@myhost nginx-1.16.1]# pwd
/home/tools/nginx-1.16.1
[root@myhost nginx-1.16.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre=/usr/local/pcre-8.37 \
--with-file-aio \
--with-debug \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module
[root@myhost nginx-1.16.1]# echo $?
0
#如果安装过程中有error字样,需要仔细阅读错误信息然后对症下药。
4.6.1 编译(lnmp架构的编译参数)
[root@myhost nginx-1.16.1]# pwd
/home/tools/nginx-1.16.1
[root@myhost nginx-1.16.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre=/usr/local/pcre-8.37 \
--with-file-aio \
--with-debug \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-pcre \
--with-pcre-jit \
--with-google_perftools_module
[root@myhost nginx-1.16.1]# echo $?
0
4.6.2 安装
[root@myhost nginx-1.16.1]# grep processor /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
[root@myhost nginx-1.16.1]# grep processor /proc/cpuinfo | wc -l
4
[root@myhost nginx-1.16.1]# make -j `grep processor /proc/cpuinfo | wc -l` && make install
[root@myhost nginx-1.16.1]# echo $?
0
4.6.3 修改nginx安装目录的所属主和所属组
[root@myhost nginx-1.16.1]# chown -R nginx:nginx /usr/local/nginx/
4.6.4 查看nginx的目录结构
[root@myhost nginx-1.16.1]# ll /usr/local/nginx
总用量 8
drwxr-xr-x. 2 nginx nginx 4096 7月 13 14:23 conf #Nginx相关配置文件
drwxr-xr-x. 2 nginx nginx 38 7月 13 14:23 html #网站根目录
drwxr-xr-x. 2 nginx nginx 6 7月 13 14:23 logs #日志文件
drwxr-xr-x. 2 nginx nginx 4096 7月 13 14:23 modules #nginx 模块
drwxr-xr-x. 2 nginx nginx 18 7月 13 14:23 sbin #Nginx启动脚本
4.6.5 查看安装nginx时都有哪些模块已经被编译进去
[root@myhost nginx]# cd modules/
[root@myhost modules]# ll
总用量 2448
-rwxr-xr-x. 1 nginx nginx 136240 7月 13 14:23 ngx_http_geoip_module.so
-rwxr-xr-x. 1 nginx nginx 150032 7月 13 14:23 ngx_http_image_filter_module.so
-rwxr-xr-x. 1 nginx nginx 180112 7月 13 14:23 ngx_http_perl_module.so
-rwxr-xr-x. 1 nginx nginx 161304 7月 13 14:23 ngx_http_xslt_filter_module.so
-rwxr-xr-x. 1 nginx nginx 685320 7月 13 14:23 ngx_mail_module.so
-rwxr-xr-x. 1 nginx nginx 1181096 7月 13 14:23 ngx_stream_module.so
[root@myhost modules]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-pcre=/usr/local/pcre-8.37 --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
4.6.6 nginx启动
[root@myhost modules]# cd
[root@myhost ~]# cd /usr/local/nginx/sbin/
[root@myhost sbin]# ll
总用量 7688
-rwxr-xr-x. 1 nginx nginx 7868488 7月 13 14:23 nginx
#检查配置文件语法是否正确
[root@myhost sbin]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#启动nginx服务
[root@myhost sbin]# /usr/local/nginx/sbin/nginx
#nginx的http默认端口号是80
[root@myhost sbin]# ps -ef |grep nginx
nginx 79944 123922 0 10:39 ? 00:00:00 nginx: worker process
root 84904 63899 0 11:30 pts/0 00:00:00 grep --color=auto nginx
root 123922 1 0 7月13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@myhost sbin]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 79944 nginx 6u IPv4 4106972 0t0 TCP *:http (LISTEN)
nginx 123922 root 6u IPv4 4106972 0t0 TCP *:http (LISTEN)
[root@myhost sbin]# netstat -lntup |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 79944/nginx: worker
4.6.7 nginx维护命令
#平滑重启nginx
[root@myhost sbin]# /usr/local/nginx/sbin/nginx -s reload
#平滑停止nginx
[root@myhost sbin]# /usr/local/nginx/sbin/nginx -s stop
# 平滑重启(保持了C-S链接,不断开,服务器只是重新加载了配置文件,没有开启和关闭的服务器的一个动作)
4.6.8 卸载nginx
- 如果是按照以上步骤源码编译安装的,可以直接删除安装包即卸载成功;
[root@myhost sbin]# cd
[root@myhost ~]# rm -rf /usr/local/nginx
- 如果是yum安装的
[root@myhost ~]# rpm -qa nginx
nginx包名
[root@myhost ~]# rpm -e nginx包名
5 拓展
5.1 参数解释
5.1.1 通用配置选项
--prefix=PATH Nginx安装的根路径,其它安装选项不明确指定安装路径,默认安装在此路径下
--user=USER 运行worker进程的用户
--group=GROUP 运行worker进程的组
--with-debug 这个选项用于启用调试日志,在生产环境的系统中不推荐使用该选项
--with-file-aio 为Linux 2.6.22+ 系统启用异步I/O
--sbin-path=PATH 指定Nginx二进制文件的路径,如果没有指定,默认使用–prefix选项指定的路径
--modules-path=PATH 指定模块文件放置的路径
--conf-path=PATH 命令行未明确指定配置文件时,使用的配置文件所在路径
--error-log-path=PATH 指定错误日志的路径
--pid-path=PATH 指定写入nginx master进程pid的文件,通常在/var/run/下
--lock-path=PATH 共享存储器互斥锁文件的路径
5.1.2 HTTP模块配置选项
--with-http_ssl_module 使用https协议进行通信,对通信的流量进行加密,需要依赖OpenSSL库。
--with-http_stub_status_module 启用这个模块后会收集Nginx自身的状态信息
--with-http_gzip_static_module 当被调用的资源是没有.gz结尾格式的文件时,如果想支持发送预压缩版本的静态文件,可以使用该模块。
--with-http_auth_request_module 启用构建ngx_http_auth_request_module模块,该模块基于子请求的结果实现客户端授权。默认情况下不生成此模块。
--with-http_v2_module 构建一个支持http协议2.0版本的模块,该模块默认是不生成的。
--with-http_realip_module Nginx在七层负载均衡器或者其它设备之后时,启用此模块,可以获取调度器转发的请求报文首部中真实客户端的IP。
--with-http_addition_module 这是一个作为输出过滤器的模块,能够在一个请求经过location前或后时在该location自身添加内容。
--with-http_xslt_module=dynamic 该模块用于处理XML响应转换,基于一个或多个XSLT格式,需要依赖libxml2和libxslt库。
--with-http_image_filter_module=dynamic 该模块作为图像过滤器使用,在将图像发送给客户之前进行处理,需要依赖libgd库。
--with-http_geoip_module=dynamic 使用该模块能,可以设置各种变量在配置文件中的区段使用,能够基于客户端的ip地址查找代理位置,需要依赖MaxMindGeoIP库和相应的预编译数据库文件。
--with-http_sub_module 该模块实现替代过滤,在响应中用一个字符串替代另一个字符串,Note:使用该模块隐式禁用标头缓存。
--with-http_dav_module 启用这个模块将激活使用WebDAV的配置指令,Note:建议该模块只有在需要时才进行启用,如果配置不正确可能会带来安全隐患。
--with-http_flv_module 如果需要提供Flash流媒体视频文件,需要启用该模块提供伪流媒体。
--with-http_mp4_module 该模块支持H.264/AA文件伪流媒体
--with-http_gunzip_module 对于不支持gzip编码的客户,该模块用于为客户解压缩预压缩内容。
--with-http_random_index_module 启用这个模块可以提供从一个目录中随机选择文件的索引文件。
--with-http_secure_link_module 该模块提供了一种机制,会将一个散列值链接到一个URL中,只有使用正确的密码才能计算出链接。
--with-http_degradation_module 可以构建ngx_http_degradation_module模块,默认情况下,此模块是不构建的。
--with-http_slice_module 构建将请求拆分为子请求的ngx_http_slice_module模块,每个子请求可以返回一定范围的响应,该模块为大的响应提供了更有效的缓存,默认情况下,此模块是不构建的。
--with-http_perl_module=dynamic 构建嵌入式Perl模块,该模块默认不生成。
5.1.3 HTTP配置选项
--http-log-path=PATH http访问日志的默认路径
--http-client-body-temp-path=PATH 设置临时存储http客户端请求报文的目录路径
--http-proxy-temp-path=PATH 设置使用代理后存放临时文件的路径
--http-fastcgi-temp-path=PATH 设置fastcgi后存放临时文件的路径
--http-uwsgi-temp-path=PATH 设置uwsgi后存放临时文件的路径
--http-scgi-temp-path=PATH 设置scgi后存放临时文件的路径
5.1.4 配置优化选项
--with-cc-opt=OPTIONS 设置将被添加到CFLAGS变量的附加参数
--with-ld-opt=OPTIONS 设置将在链接期间使用的额外参数
5.1.5 邮件代理配置选项
--with-mail=dynamic 动态启用POP3/IMAP4/SMTP代理模块
--with-mail_ssl_module 构建一个支持SSL/TLS协议的邮件代理服务模块,该模块默认不生成,构建和运行需要依赖OpenSSL库。
5.1.6 反向代理配置选项
--with-stream=dynamic 动态启用构建stream module模块,作为通用的TCP/UDP代理和负载均衡流模块,该模块默认不构建。
--with-stream_ssl_module 构建一个支持SSL/TLS协议的stream模块,该模块默认不生成,构建和运行需要依赖OpenSSL库。
5.1.7 其它配置选项
--with-pcre 强制使用PCRE库
--with-pcre-jit 使用即时编译构建PCRE库,支持1.1.12 pcre_jit指令
--with-google_perftools_module 启用构建ngx_google_perftools_module模块,该模块可使用Google Performance Tools对nginx工作进程进行分析,该模块专供nginx开发人员使用,默认不会生成。
--with-ipv6 启用IPv6支持,在1.11.x后的版本编译时,已经不需要该编译选项
5.2 nginx.conf
[root@myhost ~]# cd /usr/local/nginx/conf
[root@myhost conf]# cp nginx.conf nginx.conf.orginal
[root@myhost conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 300m;
log_format main '$remote_addr - $remote_user [$time_local] '
'$connection $upstream_addr '
'$upstream_response_time $request_time '
'$http_host $request '
'$status $body_bytes_sent ';
server {
listen 80;
server_name localhost;
access_log logs/access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
5.3 goacess
[root@myhost conf]# cat /home/goaccess/www/goaccess_nginx.conf
real-time-html true
time-format %T
date-format %d/%b/%Y
#log_format %h %^ %^ [%d:%^:%^:%^ %^] %^ %^%^ %^ %^ %m %r %H %s %b"%R" "%u"
#log_format %h %^ %^ [%d:%^:%^:%^ %^] %^ %^ %^ %^ %^ %m %r %H %s %b" %R" "%u"
log_format %h %^ %^ [%d:%t %^] %^ %^ %^ %^ %v %m %r %H %s %b "%R" "%u"