Nginx基础

发布时间:2020-03-11 15:30:04 作者:何小帅
来源:网络 阅读:891

简介

Nginx: engine X,2002年开始开发,2004年开源,2019年3月11日,Nginx公司被F5 Network以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/

1 Nginx功能介绍

静态的web资源服务器html,图⽚,js,css,txt等静态资源 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求 http/https协议的反向代理 imap4/pop3协议的反向代理 tcp/udp协议的请求转发(反向代理)

1.1 基础特性

特性:
模块化设计,较好的扩展性
⾼可靠性
⽀持热部署:不停机更新配置⽂件,升级版本,更换⽇志⽂件
低内存消耗:10000个keep-alive连接模式下的⾮活动连接,仅需2.5M内存
event-driven,aio,mmap,sendfile

基本功能:
静态资源的web服务器
http协议反向代理服务器
pop3/imap4协议反向代理服务器
FastCGI(LNMP),uWSGI(python)等协议
模块化(⾮DSO),如zip,SSL模块

1.2 和web服务相关的功能

虚拟主机(server)
⽀持 keep-alive 和管道连接(利⽤⼀个连接做多次请求)
访问⽇志(⽀持基于⽇志缓冲提⾼其性能)
url rewirte
路径别名
基于IP及⽤⼾的访问控制
⽀持速率限制及并发数限制
重新配置和在线升级⽽⽆须中断客⼾的⼯作进程

2 Nginx组织结构

web请求处理机制: 
1、多进程⽅式:服务器每接收到⼀个客⼾端请求就有服务器的主进程⽣成⼀个⼦进程响应客户端,直到⽤户关闭连接,这样的优势是处理速度快,各⼦进程之间相互独⽴,但是如果访问过⼤会导致服务器资源耗尽⽽⽆法提供请求。 
2、多线程⽅式:与多进程⽅式类似,但是每收到⼀个客⼾端请求会有服务进程派⽣出⼀
个线程来跟客户⽅进⾏交互,⼀个线程的开销远远⼩于⼀个进程,因此多线程⽅式在很⼤程度减轻了web服务器对系统资源的要求,但是多线程也有⾃⼰的缺点,即当多个线程位于同⼀个进程内⼯作的时候,可以相互访问同样的内存地址空间,所以他们相互影响,另外⼀旦主进程挂掉则所有⼦线程都不能⼯作了,IIS服务器使⽤了多线程的⽅式,需要间隔⼀段时间就重启⼀次才能稳定。

2.1 组织模型

Nginx是多进程组织模型,由Master主进程和Worker工作进程组成,如下图:

Nginx基础

主进程(master process)的功能:

读取Nginx 配置⽂件并验证其有效性和正确性
建⽴、绑定和关闭socket连接
按照配置⽣成、管理和结束⼯作进程
接受外界指令,⽐如重启、升级及退出服务器等指令
不中断服务,实现平滑升级,重启服务并应⽤新的配置
开启⽇志⽂件,获取⽂件描述符
不中断服务,实现平滑升级,升级失败进⾏回滚处理
编译和处理perl脚本

⼯作进程(woker process)的功能:

接受处理客⼾的请求
将请求以此送⼊各个功能模块进⾏处理
IO调⽤,获取响应数据
与后端服务器通信,接收后端服务器的处理结果
缓存数据,访问缓存索引,查询和调⽤缓存数据
发送请求结果,响应客⼾的请求
接收主程序指令,⽐如重启、升级和退出等

2.2 进程间通信

Nginx基础

# 主进程与工作进程通信
⼯作进程是由主进程⽣成的,主进程使⽤fork()函数,在Nginx服务器启动过程中主进程根据配置⽂件决定启动⼯作进程的数量,然后建⽴⼀张全局的⼯作表⽤于存放当前未退出的所有的⼯作进程,主进程⽣成⼯作进程后会将新⽣成的⼯作进程加⼊到⼯作进程表中,并建⽴⼀个单向的管道并将其传递给⼯作进程,该管道与普通的管道不同,它是由主进程指向⼯作进程的单项通道,包含了主进程向⼯作进程发出的指令、⼯作进程ID、⼯作进程在⼯作进程表中的索引和必要的⽂件描述符等信息。 主进程与外界通过信号机制进⾏通信,当接收到需要处理的信号时,它通过管道向相关的⼯作进程发送正确的指令,每个⼯作进程都有能⼒捕获管道中的可读事件,当管道中有可读事件的时候,⼯作进程就会从管道中读取并解析指令,然后采取相应的执⾏动作,这样就完成了主进程与⼯作进程的交互。
# 工作进程间通信
⼯作进程之间的通信原理基本上和主进程与⼯作进程之间的通信是⼀样的,只要⼯作进程之 间能够取得彼此的信息,建⽴管道即可通信,但是由于⼯作进程之间是完全隔离的,因此⼀个进程想要直到另外⼀个进程的状态信息就只能通过主进程来设置了。
为了实现⼯作进程之间的交互,主进程在⽣成⼯作进程只之后,在⼯作进程表中进⾏遍历,将该新进程的ID以及针对该进程建⽴的管道句柄传递给⼯作进程中的其他进程,为⼯作进程之间的通信做准备,当⼯作进程1向⼯作进程2发送指令的时候,⾸先在主进程给它的其他⼯作进程⼯作信息中找到2的进程ID,然后将正确的指令写⼊指向进程2的管道,⼯作进程2捕获到管道中的事件后,解析指令并进⾏相关操作,这样就完成了⼯作进程之间的通信。

3 Nginx模块介绍

名称 功能
核⼼模块 Nginx 服务器正常运⾏必不可少的模块,提供错误⽇志记录、配置⽂件解析、事件驱动机制 、进程管理等核⼼功能
标准HTTP模块 提供 HTTP 协议解析相关的功能,⽐如:端⼝配置、⽹⻚编码设置 、HTTP响应头设置等等
可选HTTP模块 主要⽤于扩展标准的 HTTP 功能,让 Nginx 能处理⼀些特殊的服务,⽐如:Flash多媒体传输、解析GeoIP请求、⽹络传输压缩、安全协议SSL⽀持等
邮件服务模块 主要⽤于⽀持Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的⽀持
第三⽅模块 是为了扩展Nginx服务器应⽤,完成开发者⾃定义功能,⽐如:Json⽀持、Lua⽀持等

4 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模块)等。

4.1 yum安装

# 此方式需要提前装备好epel源
[root@CentOS7-01 ~]#yum -y install epel-release
[root@CentOS7-01 ~]#yum -y install nginx
# 查看nginx软件包的文件
[root@CentOS7-01 ~]#rpm -ql nginx
# 查看nginx软件包的详细信息
[root@CentOS7-01 ~]#rpm -qi nginx
# 获取nginx使用帮助
[root@CentOS7-01 ~]#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: /usr/share/nginx/) #指定Nginx ⽬录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置⽂件路径
-g directives : set global directives out of configuration file #设置全局指令
# 测试nginx配置文件是否正常
[root@CentOS7-01 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 显示版本和编译参数,该选项在需要安装和现有版本一样配置的nginx时很有用
[root@CentOS7-01 ~]#nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
# Nginx启动脚本
[root@CentOS7-01 ~]#cat /usr/lib/systemd/system/nginx.service
# Nginx主配置文件
[root@CentOS7-01 ~]#cat /etc/nginx/nginx.conf
# 启动Nginx
[root@CentOS7-01 ~]#systemctl enable --now nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@CentOS7-01 ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-01-01 22:10:43 CST; 4s ago
  Process: 10961 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 10958 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 10956 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 10963 (nginx)
   CGroup: /system.slice/nginx.service
           ├─10963 nginx: master process /usr/sbin/nginx
           ├─10964 nginx: worker process
           └─10965 nginx: worker process

Jan 01 22:10:43 CentOS7-01.localdomain systemd[1]: Starting The nginx HTTP and r....
Jan 01 22:10:43 CentOS7-01.localdomain nginx[10958]: nginx: the configuration fi...k
Jan 01 22:10:43 CentOS7-01.localdomain nginx[10958]: nginx: configuration file /...l
Jan 01 22:10:43 CentOS7-01.localdomain systemd[1]: Started The nginx HTTP and re....
Hint: Some lines were ellipsized, use -l to show in full.
# 查看nginx进程信息
[root@CentOS7-01 ~]#ps -ef |grep nginx|grep -v grep
root      10963      1  0 22:10 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     10964  10963  0 22:10 ?        00:00:00 nginx: worker process
nginx     10965  10963  0 22:10 ?        00:00:00 nginx: worker process
# 访问测试
[root@CentOS7-01 ~]#curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 01 Jan 2020 14:14:02 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes

4.2 编译安装

# 准备编译安装的基础环境
[root@CentOS7-01 ~]#yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed GeoIP-devel gd-devel
# 安装nginx
[root@CentOS7-01 ~]#cd /usr/local/src/
[root@CentOS7-01 src]#wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@CentOS7-01 src]#tar xf nginx-1.16.1.tar.gz
[root@CentOS7-01 src]#cd nginx-1.16.1
# 注:编译是为了检查系统环境是否符合编译安装的要求,⽐如是否有gcc编译⼯具,是否⽀持编译参数当中的模块,并根据开启的参数等⽣成Makefile⽂件为下⼀步做准备:
[root@CentOS7-01 src]#./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@CentOS7-01 src]#make -j `lscpu |awk 'NR==4{print $2}'` && make install
[root@CentOS7-01 src]#useradd nginx -s /sbin/nologin -u 2000
[root@CentOS7-01 src]#chown nginx:nginx -R /apps/nginx/
注: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@CentOS7-01 src]#echo "PATH=/apps/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@CentOS7-01 src]#source /etc/profile.d/nginx.sh
# 验证版本及编译参数
[root@CentOS7-01 src]#nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
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
# 启动nginx并测试访问
[root@CentOS7-01 src]#nginx
[root@CentOS7-01 src]#curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 02 Jan 2020 03:37:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 02 Jan 2020 03:09:54 GMT
Connection: keep-alive
ETag: "5e0d5f02-264"
Accept-Ranges: bytes
# 创建nginx自启动脚本
[root@CentOS7-01 src]#cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t  #此处要写nginx编译安装时候指定安装的位置路径                                            
ExecStart=/apps/nginx/sbin/nginx #同上
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@CentOS7-01 src]#sed -i -e '/^#pid/a\pid \/run\/nginx.pid;' -e '/^#user/a\user nginx;' /apps/nginx/conf/nginx.conf
# 验证nginx自启动脚本
[root@CentOS7-01 src]#systemctl deamon-reload
[root@CentOS7-01 src]#systemctl enable --now nginx
[root@CentOS7-01 src]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-01-02 12:31:42 CST; 1s ago
  Process: 32753 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 32750 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 32748 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 32755 (nginx)
   CGroup: /system.slice/nginx.service
           ├─32755 nginx: master process /apps/nginx/sbin/nginx
           └─32756 nginx: worker process

Jan 02 12:31:42 CentOS7-01.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 02 12:31:42 CentOS7-01.localdomain nginx[32750]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Jan 02 12:31:42 CentOS7-01.localdomain nginx[32750]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Jan 02 12:31:42 CentOS7-01.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.

# nginx默认主配置文件解释
主配置文件路径:/apps/nginx/conf/nginx.conf
主配置文件的配置指令方式:
directive value [value2 ...];
注意:
(1) 指令必须以分号结尾
(2) ⽀持使⽤配置变量
        内建变量:由Nginx模块引⼊,可直接引⽤
        ⾃定义变量:由用户使⽤set命令定义
                   set variable_name value;
        引⽤变量:$variable_name

[root@CentOS7-01 src]#egrep -v "^$|#" /apps/nginx/conf/nginx.conf
#全局配置段,对全局⽣效,主要设置nginx工作进程的启动⽤户/组、启动的⼯作进程数量、⼯作模式、Nginx的PID文件路径,⽇志路径等。
user  nginx; #启动Nginx⼯作进程的⽤户,默认是nobody
worker_processes  1; #启动⼯作进程数数量
pid /run/nginx.pid; #nginx的pid文件路径
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; # 导入⽀持的mime类型,MIME(Multipurpose Internet Mail Extensions)多⽤途互联⽹邮件扩展类型,MIME消息能包含⽂本、图像、⾳频、视频以及其他应⽤程序专⽤的数据,是设定某种扩展名的⽂件⽤⼀种应⽤程序来打开的⽅式类型,当该扩展名⽂件被访问的时候,浏览器会⾃动使⽤指定应⽤程序来打开。多⽤于指定⼀些客户端⾃定义的⽂件名,以及⼀些媒体⽂件打开⽅式。MIME参考⽂档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/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会调⽤当前server内部的配置进程匹配。
        location / { #location其实是server的⼀个指令,为nginx服务器提供⽐较多⽽且灵活的指令,都是在location中体现的,主要是基于nginx接收到的请求字符串,对⽤户请求的URL进⾏匹配,并对特定的指令进⾏处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三⽅模块的配置也是在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; #定义默认⻚⾯所在的⽬录
        }
    }
}

4.3 编译安装后使用systemd管理nginx服务时遇到的问题及解决方法

# 在上面,编译安装完成后创建了一个nginx服务脚本,目的是为了使用systemd来管理nginx,然而在启动的时候报如下错误,导致nginx启动失败
[root@CentOS7-01 src]#systemctl restart nginx
Job for nginx.service failed because a timeout was exceeded. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@CentOS7-01 src]#tail -f /var/log/messages
Jan  2 14:37:09 CentOS7-01 systemd: Starting The nginx HTTP and reverse proxy server...
Jan  2 14:37:09 CentOS7-01 nginx: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Jan  2 14:37:09 CentOS7-01 nginx: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Jan  2 14:37:10 CentOS7-01 systemd: Can't open PID file /run/nginx.pid (yet?) after start: No such file or directory
Jan  2 14:38:40 CentOS7-01 systemd: nginx.service start operation timed out. Terminating.
Jan  2 14:38:40 CentOS7-01 systemd: Failed to start The nginx HTTP and reverse proxy server.
Jan  2 14:38:40 CentOS7-01 systemd: Unit nginx.service entered failed state.
Jan  2 14:38:40 CentOS7-01 systemd: nginx.service failed.

# 问题排查
根据报错提示是由于找不到/run/nginx.pid文件导致的nginx启动失败,来看一下nginx服务的service文件和主配置文件中定义的PID文件路径
[root@CentOS7-01 src]#grep "^PID" /usr/lib/systemd/system/nginx.service 
PIDFile=/run/nginx.pid
[root@CentOS7-01 src]#grep -w "pid" /apps/nginx/conf/nginx.conf
#pid        logs/nginx.pid;
可以发现它俩的路径并不是同一个,而使用systemctl来启动nginx时,会去找service文件中定义的PID文件路径,但在加载主配置文件时发现默认定义的PID文件路径是logs/nginx.pid,并且也没有具体定义PID文件的路径和service文件中定义的是同一个,故报此错误。

# 解决方法
a.在主配置文件中定义pid文件的绝对路径
[root@CentOS7-01 src]#vim /apps/nginx/conf/nginx.conf
pid /run/nginx.pid;

或

b.在nginx服务的service文件中指定默认定义的pid文件的绝对路径
[root@CentOS7-01 src]#vim /usr/lib/systemd/system/nginx.service 
PIDFile=/apps/nginx/logs/nginx.pid

然后再次启动服务即可
推荐阅读:
  1. 基于nginx基础负载均衡的详解
  2. Nginx网站服务——基础服务与访问控制

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx简介 nginx组织结构 nginx安装

上一篇:107. 二叉树的层次遍历 II

下一篇:Using Multiple Vertex Shader Inputs

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》