Nginx基础
Nginx:engine X ,2002年开始开发,2004年开源,2019年3⽉11⽇,Nginx公司被F5 Networks以6.7亿美元收购。
Nginx 则是免费的、开源的、⾼性能的HTTP和反向代理服务器、邮件代理服务器、以及TCP/UDP代理服务器 解决C10K问题(10K Connections)
http://www.ideawu.net/blog/archives/740.html
Nginx官⽹:http://nginx.org
Nginx 商业版为Nginx Plus:https://www.nginx.com/products/nginx/
nginx的其它的⼆次发⾏版: Tengine:由淘宝⽹发起的Web服务器项⽬。它在Nginx的基础上,针对⼤访问量⽹站的需求,添加了很多⾼级功能和特性。Tengine的性能和稳定性已经在⼤型的⽹站如淘宝⽹,天猫商城等得到了很好的检验。它的最终⽬标是打造⼀个⾼效、稳定、安全、易⽤的Web平台,从2011年12⽉开始,Tengine成为⼀个开源项⽬,官⽹ http://tengine.taobao.org/
OpenResty:基于 Nginx 与 Lua 语⾔的⾼性能 Web 平台, 章亦春团队开发,官⽹:http://openresty.org/cn/
Nginx功能介绍
静态的web资源服务器html,图⽚,js,css,txt等静态资源
结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
http/https协议的反向代理
imap4/pop3协议的反向代理
tcp/udp协议的请求转发(反向代理)
基础特性
特性:
模块化设计,较好的扩展性
⾼可靠性 远远超过apache
⽀持热部署:不停机更新配置⽂件,升级版本,更换⽇志⽂件
低内存消耗:10000个keep-alive连接模式下的⾮活动连接,仅需2.5M内存
event-driven,aio,mmap(内存映射),sendfile
基本功能:
静态资源的web服务器(js文件,访问了js文件配置有SND以及多域名)
http协议反向代理服务器
pop3/imap4协议反向代理服务器
FastCGI(LNMP),uWSGI(python)等协议
模块化(⾮DSO),如zip,SSL模块
web服务相关的功能
虚拟主机(server)基于不同端口 主机 域名
⽀持 keep-alive 和管道连接(利⽤⼀个连接做多次请求)
访问⽇志(⽀持基于⽇志缓冲提⾼其性能)
url rewirte
路径别名 alias
基于IP及⽤⼾的访问控制
⽀持速率限制及并发数限制
重新配置和在线升级⽽⽆须中断客⼾的⼯作进程
Nginx组织结构
web请求处理机制:
1、多进程⽅式:服务器每接收到⼀个客⼾端请求就有服务器的主进程⽣成⼀个⼦进程响应客⼾端,直到⽤⼾关闭连接,这样的优势是处理速度快,各⼦进程之间相互独⽴,但是如果访问过⼤会导致服务器资源耗尽⽽⽆法提供请求。
2、多线程⽅式:与多进程⽅式类似,但是每收到⼀个客⼾端请求会有服务进程派⽣出⼀个线程来个客⼾⽅进⾏交互,⼀个线程的开销远远⼩于⼀个进程,因此多线程⽅式在很⼤程度减轻了web服务器对系统资源的要求,但是多线程也有⾃⼰的缺点,即当多个线程位于同⼀个进程内⼯作的时候,可以相互访问同样的内存地址空间,所以他们相互影响,另外⼀旦主进程挂掉则所有⼦线程都不能⼯作了,IIS服务器使⽤了多线程的⽅式,需要间隔⼀段时间就重启⼀次才能稳定。类似监听进程挂掉之后,对应的所有子进程也不能工作。
组织模型
Nginx是多进程组织模型,⽽且是⼀个由Master主进程和Worker⼯作进程组成。
工作进程一般开启小于或者等于CPU的核数
主进程(master process)的功能:
读取Nginx 配置⽂件并验证其有效性和正确性 eg:分号结尾
建⽴、绑定和关闭socket连接
按照配置⽣成、管理和结束⼯作进程
接受外界指令,⽐如重启、升级及退出服务器等指令
不中断服务,实现平滑升级,重启服务并应⽤新的配置 eg:nginx -s reload (配置生效,之前连接的子进程不影响)
开启⽇志⽂件,获取⽂件描述符
不中断服务,实现平滑升级,升级失败进⾏回滚处理
编译和处理perl脚本
⼯作进程(woker process)的功能:
接受处理客⼾的请求
将请求以此送⼊各个功能模块进⾏处理
IO调⽤,获取相应的数据
与后端服务器通信,接收后端服务器的处理结果
缓存数据,访问缓存索引,查询和调⽤缓存数据
发送请求结果,响应客⼾的请求
接收主程序指令,⽐如重启、升级和退出等
进程间通信
⼯作进程是有主进程⽣成的,主进程由root启用,主进程使⽤fork()函数,在Nginx服务器启动过程中主进程根据配置⽂件决定启动⼯作进程的数量,然后建⽴⼀张全局的⼯作表⽤于存放当前未退出的所有的⼯作进程,主进程⽣成⼯作进程后会将新⽣成的⼯作进程加⼊到⼯作进程表中,并建⽴⼀个单向的管道并将其传递给⼯作进程,该管道与普通的管道不同,它是由主进程指向⼯作进程的单项通道,包含了主进程向⼯作进程发出的指令、⼯作进程ID、⼯作进程在⼯作进程表中的索引和必要的⽂件描述符等信息,单向管道,工作进程只能监听内容之后读取指令。 主进程与外界通过信号机制进⾏通信,当接收到需要处理的信号时,它通过管道向相关的⼯作进程发送正确的指令,每个⼯作进程都有能⼒捕获管道中的可读事件,当管道中有可读事件的时候,⼯作进程就会从管道中读取并解析指令,然后采取相应的执⾏动作,这样就完成了主进程与⼯作进程的交互。
⼯作进程之间的通信原理基本上和主进程与⼯作进程之间的通信是⼀样的,只要⼯作进程之间能够取得彼此的信
息,建⽴管道即可通信,但是由于⼯作进程之间是完全隔离的,因此⼀个进程想要知道另外⼀个进程的状态信息就只能
通过主进程来设置了。
为了实现⼯作进程之间的交互,主进程在⽣成⼯作进程之后,在⼯作进程表中进⾏遍历,将该新进程的ID以及针
对该进程建⽴的管道句柄传递给⼯作进程中的其他进程,为⼯作进程之间的通信做准备,当⼯作进程1向⼯作进程2发送
指令的时候,⾸先在主进程给它的其他⼯作进程⼯作信息中找到2的进程ID,然后将正确的指令写⼊指向进程2的管道,
⼯作进程2捕获到管道中的事件后,解析指令并进⾏相关操作,这样就完成了⼯作进程之间的通信。
Nginx模块介绍
核⼼模块:是 Nginx 服务器正常运⾏ 必不可少 的模块,提供 错误⽇志记录 、 配置⽂件解析 、 事件驱动机制 、进程管理等核⼼功能
标准HTTP模块:提供 HTTP 协议解析相关的功能,⽐如:端⼝配置 、⽹⻚编码设置 、HTTP响应头设置等等
可选HTTP模块:主要⽤于扩展标准的 HTTP 功能,让 Nginx 能处理⼀些特殊的服务,⽐如: Flash 多媒体传输 、解析 GeoIP 请求、 ⽹络传输压缩 、 安全协议 SSL ⽀持等
邮件服务模块:主要⽤于⽀持Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的⽀持
第三⽅模块:是为了扩展 Nginx 服务器应⽤,完成开发者⾃定义功能,⽐如: Json ⽀持、 Lua ⽀持等
nginx⾼度模块化,但其模块早期不⽀持DSO机制;1.9.11版本⽀持动态装载和卸载
模块分类:
核⼼模块:core module
标准模块:
HTTP 模块: ngx_http_*
HTTP Core modules 默认功能
HTTP Optional modules 需编译时指定
Mail 模块 ngx_mail_*
Stream 模块 ngx_stream_*
第三⽅模块
Nginx安装
Nginx的安装版本分为Mainline version(主要开发版本,其实就是还处于开发版)、Stable version(当前最新稳定版)和Legacy versions(旧的稳定版), Nginx安装可以使⽤yum或源码安装,但是推荐使⽤源码,⼀是yum的版本⽐较旧,⼆是编译安装可以更⽅便⾃定义相关路径,三是使⽤源码编译可以⾃定义相关功能,更⽅便业务的上的使⽤,源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是⾃由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语⾔,所以原名为GNU C语⾔编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective-C,java以及Ada等其他语⾔,此外还需要Automake⼯具,以完成⾃动创建Makefile的⼯作,Nginx的⼀些模块需要依赖第三⽅库,⽐如pcre(⽀持rewrite),zlib(⽀持gzip模块)和openssl(⽀持ssl模块)等。
Nginx yum安装
Nginx下载官网
Ubantu安装
上述网址也有centos的nginx安装代码
Ubuntu
Install the prerequisites:
sudo apt install curl gnupg2 ca-certificates lsb-release
To set up the apt repository for stable nginx packages, run the following command:
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
If you would like to use mainline nginx packages, run the following command instead:
echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
Next, import an official nginx signing key so apt could verify the packages authenticity:
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
Verify that you now have the proper key:
sudo apt-key fingerprint ABF5BD827BD9BF62
The output should contain the full fingerprint 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62 as follows:
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
uid [ unknown] nginx signing key <signing-key@nginx.com>
To install nginx, run the following commands:
sudo apt update
sudo apt install nginx
root@apt-install:~# apt-cache madison nginx #查看能安装的版本
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-security/main i386 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-updates/main i386 Packages
nginx | 1.14.0-0ubuntu1 | http://mirrors.aliyun.com/ubuntu bionic/main amd64 Packages
nginx | 1.14.0-0ubuntu1 | http://mirrors.aliyun.com/ubuntu bionic/main i386 Packages
nginx | 1.14.0-0ubuntu1 | http://mirrors.aliyun.com/ubuntu bionic/main Sources
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-security/main Sources
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-updates/main Sources
root@apt-install:~#
安装比较新的版本就需要重新添加下载源仓库加载下载路径
root@apt-install:~# sudo apt install curl gnupg2 ca-certificates lsb-release -y
root@apt-install:~# echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
> | sudo tee /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/ubuntu bionic nginx
root@apt-install:~#
root@apt-install:~# cat /etc/apt/sources.list.d/nginx.list #增加源到了子配置文件
deb http://nginx.org/packages/ubuntu bionic nginx
root@apt-install:~# curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add - #导KEY
OK
root@apt-install:~#
root@apt-install:~# sudo apt-key fingerprint ABF5BD827BD9BF62 #验证KEY
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
uid [ unknown] nginx signing key <signing-key@nginx.com>
root@apt-install:~#
root@apt-install:~# apt update #ubantu中增加了yum(apt)源之后必须 apt update一下
root@apt-install:~# apt-cache madison nginx #查看是否出现新的稳定版本Nginx1.16.1
nginx | 1.16.1-1~bionic | http://nginx.org/packages/ubuntu bionic/nginx amd64 Packages
nginx | 1.16.0-1~bionic | http://nginx.org/packages/ubuntu bionic/nginx amd64 Packages
nginx | 1.14.2-1~bionic | http://nginx.org/packages/ubuntu bionic/nginx amd64 Packages
nginx | 1.14.1-1~bionic | http://nginx.org/packages/ubuntu bionic/nginx amd64 Packages
nginx | 1.14.0-1~bionic | http://nginx.org/packages/ubuntu bionic/nginx amd64 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-security/main i386 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-updates/main i386 Packages
nginx | 1.14.0-0ubuntu1 | http://mirrors.aliyun.com/ubuntu bionic/main amd64 Packages
nginx | 1.14.0-0ubuntu1 | http://mirrors.aliyun.com/ubuntu bionic/main i386 Packages
nginx | 1.14.0-0ubuntu1 | http://mirrors.aliyun.com/ubuntu bionic/main Sources
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-security/main Sources
nginx | 1.14.0-0ubuntu1.6 | http://mirrors.aliyun.com/ubuntu bionic-updates/main Sources
root@apt-install:~# apt install nginx=1.16.1-1~bionic -y #安装nginx=1.16.1
root@apt-install:~# nginx -v
nginx version: nginx/1.16.1
root@apt-install:~# nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显⽰版本和编译参数
-t : test configuration and exit #测试配置⽂件是否异常
-T : test configuration, dump it and exit #测试并打印
-q : suppress non-error messages during configuration testing #静默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx ⽬录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置⽂件路径
-g directives : set global directives out of configuration file #设置全局指令
root@apt-install:~# nginx #第一次启动Nginx
root@apt-install:~# ss -ntlp |grep nginx #80端口
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:
(("nginx",pid=3762,fd=6),("nginx",pid=3761,fd=6))
root@apt-install:~#
root@apt-install:~# nginx -s stop #停止nginx
root@apt-install:~# cat /etc/nginx/nginx.conf |grep -Ev "^#|^$" #基础配置
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@apt-install:~#
root@apt-install:~# ps -ef | grep nginx #查看进程
root 3761 1 0 16:02 ? 00:00:00 nginx: master process nginx
nginx 3762 3761 0 16:02 ? 00:00:00 nginx: worker process
root 3775 2113 0 16:06 pts/0 00:00:00 grep --color=auto nginx
Nginx 编译安装
官⽅源码包下载地址:https://nginx.org/en/download.html
安装可能需要的命令以及依赖包
root@make-install:~#apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server
\nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump
\ telnet traceroute gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev
\ zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip libgd-dev libgeoip-dev -y
root@make-install:~# cd /usr/local/src/
root@make-install:/usr/local/src# wget https://nginx.org/download/nginx-1.16.1.tar.gz
root@make-install:/usr/local/src# ll
total 1020
drwxr-xr-x 2 root root 4096 Dec 31 16:31 ./
drwxr-xr-x 10 root root 4096 Dec 28 05:33 ../
-rw-r--r-- 1 root root 1032630 Dec 31 16:31 nginx-1.16.1.tar.gz
root@make-install:/usr/local/src# tar zxvf nginx-1.16.1.tar.gz
root@make-install:/usr/local/src# cd nginx-1.16.1/
root@make-install:/usr/local/src/nginx-1.16.1# ll
total 780
drwxr-xr-x 8 1001 1001 4096 Aug 13 20:51 ./
drwxr-xr-x 3 root root 4096 Dec 31 16:33 ../
drwxr-xr-x 6 1001 1001 4096 Dec 31 16:33 auto/
-rw-r--r-- 1 1001 1001 296463 Aug 13 20:51 CHANGES
-rw-r--r-- 1 1001 1001 452171 Aug 13 20:51 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Dec 31 16:33 conf/
-rwxr-xr-x 1 1001 1001 2502 Aug 13 20:51 configure* #编译文件
drwxr-xr-x 4 1001 1001 4096 Dec 31 16:33 contrib/
drwxr-xr-x 2 1001 1001 4096 Dec 31 16:33 html/
-rw-r--r-- 1 1001 1001 1397 Aug 13 20:51 LICENSE
drwxr-xr-x 2 1001 1001 4096 Dec 31 16:33 man/
-rw-r--r-- 1 1001 1001 49 Aug 13 20:51 README
drwxr-xr-x 9 1001 1001 4096 Dec 31 16:33 src/
root@make-install:/usr/local/src/nginx-1.16.1#
编译是为了检查系统环境是否符合编译安装的要求,⽐如是否有gcc编译⼯具,是否⽀持编译参数当中的模块,并根据开启的参数等⽣成Makefile⽂件为下⼀步做准备
root@make-install:/usr/local/src/nginx-1.16.1# ./configure --help #查看编译帮助参数信息
root@make-install:/usr/local/src/nginx-1.16.1#./configure --prefix=/apps/nginx --with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
root@make-install:/usr/local/src/nginx-1.16.1# make && make install
root@make-install:/usr/local/src/nginx-1.16.1# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
root@make-install:/usr/local/src/nginx-1.16.1# /apps/nginx/sbin/nginx #启动服务
root@make-install:/usr/local/src/nginx-1.16.1# ss -ntlp |grep nginx
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:
(("nginx",pid=13056,fd=6),("nginx",pid=13055,fd=6))
root@make-install:/usr/local/src/nginx-1.16.1#
创建一个独立系统用户。
root@make-install:~# useradd -r -s /sbin/nologin nginx
root@make-install:~# id nginx
uid=999(nginx) gid=999(nginx) groups=999(nginx)
root@make-install:~# chown nginx.nginx -R /apps/nginx/ #设置权限
root@make-install:~# vim /apps/nginx/conf/nginx.conf
#user nobody;
user nginx; #配置一个独立的系统用户
root@make-install:~# /apps/nginx/sbin/nginx -s reload
root@make-install:~# ps -ef |grep nginx
root 13055 1 0 16:44 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 13248 13055 0 17:15 ? 00:00:00 nginx: worker process #普通用户身份启动
root 13250 1216 0 17:15 pts/1 00:00:00 grep --color=auto nginx
root@make-install:~#
访问编译安装的nginx web界⾯
备注:nginx完成安装以后,有四个主要的⽬录:
root@make-install:/usr/local/src/nginx-1.16.1# cd /apps/nginx/
root@make-install:/apps/nginx# ll
total 44
drwxr-xr-x 11 root root 4096 Dec 31 16:43 ./
drwxr-xr-x 3 root root 4096 Dec 31 16:41 ../
drwx------ 2 nobody root 4096 Dec 31 16:43 client_body_temp/
drwxr-xr-x 2 root root 4096 Dec 31 16:41 conf/
drwx------ 2 nobody root 4096 Dec 31 16:43 fastcgi_temp/
drwxr-xr-x 2 root root 4096 Dec 31 16:41 html/ #默认的页面文件
drwxr-xr-x 2 root root 4096 Dec 31 16:44 logs/
drwx------ 2 nobody root 4096 Dec 31 16:43 proxy_temp/
drwxr-xr-x 2 root root 4096 Dec 31 16:41 sbin/ #二进制程序
drwx------ 2 nobody root 4096 Dec 31 16:43 scgi_temp/
drwx------ 2 nobody root 4096 Dec 31 16:43 uwsgi_temp/
root@make-install:/usr/local/src/nginx-1.16.1# cd /apps/nginx/
root@make-install:/apps/nginx# cat html/index.html
172.20.32.110 first make install Nginx
conf:该⽬录中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服务器的最核⼼最主要的配置⽂件,其他
的.conf则是⽤来配置nginx相关的功能的,例如fastcgi功能使⽤的是fastcgi.conf和fastcgi_params两个⽂
件,配置⽂件⼀般都有个样板配置⽂件,是⽂件名.default结尾,使⽤的使⽤将其复制为并将default去掉即可。
html:该⽬录中保存了nginx服务器的web⽂件,但是可以更改为其他⽬录保存web⽂件,另外还有⼀个50x的web⽂件是
默认的错误⻚⾯提⽰⻚⾯。
logs:该⽬录⽤来保存nginx服务器的访问⽇志错误⽇志等⽇志,logs⽬录可以放在其他路径,⽐
如/var/logs/nginx⾥⾯。
sbin:该⽬录⽤来保存nginx⼆进制启动脚本,可以接受不同的参数以实现不同的功能。
验证版本及编译参数
root@make-install:/usr/local/src/nginx-1.16.1# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-
http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --
with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-
stream --with-stream_ssl_module --with-stream_realip_module
root@make-install:/usr/local/src/nginx-1.16.1#
创建Nginx自启动脚本
可以从yum(apt)安装的Nginx的启动脚本配置拷贝到对应的编译安装目录中,然后进行参数的修改即可。
这里用之前yum(apt)安装的Nginx的启动脚本配置文件为例
在yum(apt)安装中的对应配置文件:
root@apt-install:~# cat /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
root@apt-install:~#
拷贝到源码安装的主机中
root@apt-install:~# scp /lib/systemd/system/nginx.service 172.20.32.110:/lib/systemd/system/nginx.service
The authenticity of host '172.20.32.110 (172.20.32.110)' can't be established.
ECDSA key fingerprint is SHA256:k0siGQ7JYS+LYiXcRspcHA8+dy+cWs4xsV+82GHk3ng.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.20.32.110' (ECDSA) to the list of known hosts.
root@172.20.32.110's password:
nginx.service 100% 399 674.4KB/s 00:00
root@apt-install:~#
源码安装的主机中操作,要创建自启动脚本文件,先要把之前的nginx先停止
root@make-install:~# /apps/nginx/sbin/nginx -s stop
root@make-install:~# ss -ntlp |grep nginx
root@make-install:~#
root@make-install:~# mkdir /apps/nginx/run/ #创建一个路径给PID文件使用
root@make-install:~# cat /apps/nginx/conf/nginx.conf |head -15
#user nobody;
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #默认PID路径
pid /apps/nginx/run/nginx.pid; #自己创建的一个PID路径 这个路径就是启动服务脚本中PID的路径
events {
root@make-install:~#
root@make-install:~# vim /lib/systemd/system/nginx.service
root@make-install:~# cat /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid #PID文件路径一定要和nginx配置文件中的PID路径一致
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
# ExecStart是二进制启动Nginx命令的路径 -c 之后的参数是nginx配置文件的路径
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
root@make-install:~#
root@make-install:~# chown nginx.nginx /apps/nginx/ -R #修改对应的读写权限
root@make-install:~# systemctl start nginx #启动nginx服务
root@make-install:~# ss -ntlp |grep nginx
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:
(("nginx",pid=13606,fd=6),("nginx",pid=13605,fd=6))
root@make-install:~#
root@make-install:~# systemctl daemon-reload
root@make-install:~# systemctl start nginx
root@make-install:~# systemctl enable nginx
root@make-install:~# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2019-12-31 17:37:52 CST; 10min ago
Docs: http://nginx.org/en/docs/
Process: 13604 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, s
Main PID: 13605 (nginx)
Tasks: 2 (limit: 2290)
CGroup: /system.slice/nginx.service
├─13605 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
└─13606 nginx: worker process
......
配置Nginx
Nginx的配置⽂件的组成部分:
主配置⽂件:nginx.conf
⼦配置⽂件 include conf.d/*.conf
fastcgi, uwsgi,scgi等协议相关的配置⽂件
mime.types:⽀持的mime类型,MIME(Multipurpose Internet Mail Extensions)多⽤途互联⽹邮件扩展类
型,MIME消息能包含⽂本、图像、⾳频、视频以及其他应⽤程序专⽤的数据,是设定某种扩展名的⽂件⽤⼀种应⽤程序来
打开的⽅式类型,当该扩展名⽂件被访问的时候,浏览器会⾃动使⽤指定应⽤程序来打开。多⽤于指定⼀些客⼾端⾃定义
的⽂件名,以及⼀些媒体⽂件打开⽅式。
Nginx主配置⽂件的配置指令⽅式:
directive value [value2 ...];
注意:
(1) 指令必须以分号结尾
(2) ⽀持使⽤配置变量
内建变量:由Nginx模块引⼊,可直接引⽤
⾃定义变量:由⽤⼾使⽤set命令定义
set variable_name value;
引⽤变量:$variable_name
MIME参考⽂档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types
默认配置文件
~# grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
#全局配置端,对全局⽣效,主要设置nginx的启动⽤⼾/组,启动的⼯作进程数量,⼯作模式,Nginx的PID路径,⽇志路径等。
user nginx nginx;
worker_processes 1; #启动⼯作进程数数量
events { #events设置快,主要影响nginx服务器与⽤⼾的⽹络连接,⽐如是否允许同时接受多个⽹络连接,使⽤哪
种事件驱动模型处理请求,每个⼯作进程可以同时⽀持的最⼤连接数,是否开启对多⼯作进程下的⽹络连接进⾏序列化等。
worker_connections 1024; #设置单个nginx⼯作进程可以接受的最⼤并发,作为web服务器的时候最
⼤并发数为worker_connections * worker_processes,作为反向代理的时候为(worker_connections *worker_processes)/2
}
http { #http块是Nginx服务器配置中的重要部分,缓存、代理和⽇志格式定义等绝⼤多数功能和第三⽅模块都可以在这设置,http
块可以包含多个server块,⽽⼀个server块中⼜可以包含多个location块,server块可以配置⽂件引⼊、MIME-Type定义、⽇志⾃定
义、是否启⽤sendfile、连接超时时间和单个链接的请求上限等。
include mime.types;
default_type application/octet-stream;
sendfile on; #作为web服务器的时候打开sendfile加快静态⽂件传输,指定是否使⽤sendfile系统调⽤来传输⽂件,sendfile系统
调⽤在两个⽂件描述符之间直接传递数据(完全在内核中操作),从⽽避免了数据在内核缓冲区和⽤⼾缓冲区之间的拷⻉,操作效率很
⾼,被称之为零拷⻉,硬盘 >> kernel buffer (快速拷⻉到kernelsocket buffer) >>协议栈。
keepalive_timeout 65; #⻓连接超时时间,单位是秒
server { #设置⼀个虚拟机主机,可以包含⾃⼰的全局快,同时也可以包含多个location模块。⽐如本虚拟机
监听的端⼝、本虚拟机的名称和IP配置,多个server 可以使⽤⼀个端⼝,⽐如都使⽤80端⼝提供web服务、
listen 80; #配置server监听的端⼝
server_name localhost; 本server的名称,当访问此名称的时候nginx会调⽤当前serevr内部的配置进程匹配。
location / { #location其实是server的⼀个指令,为nginx服务器提供⽐较多⽽且灵活的指令,都是在location中
提现的,主要是基于nginx接受到的请求字符串,对⽤⼾请求的UIL进⾏匹配,并对特定的指令进⾏处理,包括地址重定向、
数据缓存和应答控制等功能都是在这部分实现,另外很多第三⽅模块的配置也是在location模块中配置。
root html; #相当于默认⻚⾯的⽬录名称,默认是相对路径,可以使⽤绝对路径配置。
index index.html index.htm; #默认的⻚⾯⽂件名称
}
error_page 500 502 503 504 /50x.html; #错误⻚⾯的⽂件名称
location = /50x.html { #location处理对应的不同错误码的⻚⾯定义到/50x.html,这个跟对应其server中定义的⽬录下。
root html; #定义默认⻚⾯所在的⽬录
}
}
#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段
#tcp代理配置,1.9版本以上⽀持
#stream {
# ...
# } stream 服务器相关配置段
#导⼊其他路径的配置⽂件
#include /apps/nginx/conf.d/*.conf
}
编译安装基础配置
root@make-install:~# grep -Ev "#|^$" /apps/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid /apps/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
root@make-install:~#