nginx之安装与程序目录结构介绍

nginx安装与程序目录结构介绍

nginx简介

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev.

官方网址:http://nginx.org/en/

ps:二次开发版本有tengine和openrestry

可充当的角色:

  • 高并发web服务器
  • 邮件服务器
  • 通用四层代理;七层http、fastcgi、uwscgi等的代理
  • 内容缓存:https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/

特性:

  • 模块化,扩展性好
  • 高可靠
  • 支持热部署:在线,重载配置文件、升级回滚主程序、日志文件更换
  • 低内存消耗:1w个非活动长连接,只需2.5M,(官方doc)
  • event-driven,aio,mmap,sendfile,

模块

1.9.11开始支持动态 装卸载模块;

  • 核心模块:core module
  • 标准模块:ngx_http_*
    • http core modules 默认带
    • http optional modules 需编译时指定
  • mail模块:ngx_mail_*
  • stream模块:ngx_stream_*
  • 第3方

工作模型

​ nginx采用的是进程工作模型,一个主进程负责管理子进程的启停、配置;子进程负责监听、接收请求、处理请求、响应请求、断开连接等;进程处理为异步非阻塞,用内存存放阻塞了、不活跃的连接,从而可以处理其他的请求;理论上、内存越大,并发就越高;

worker进程采用争抢互斥锁获取监听套接字,监听资格;不考虑端口重用技术,同一时刻,只有一个在监听;

worker用accept消费了一个已经建立完成的连接,之后;分析请求、读取本地数据、封装响应、发送到send buffer,全过程几乎都是异步非阻塞!连接都放在连接池中,所以并发高!无论是等待准备数据、等待send buffer可写、空闲长连接,nginx的worker进程都不会干等,可以去做其他的请求;

​ 当某资源准备好,或send buffer可以写了,都会用epoll进行事件通知;从而继续处理请求;

程序结构

​ 由一个master和N个worker进程组成,启用缓存的话,还有2个cache相关的进程;cache loader and cache manager,异步非阻塞,

img

安装

yum安装

采用nginx.org提供的repo文件,配置好nginx.repo,直接yum install即可,默认安装的是最新的稳定版;

编译安装

1,下载依赖包、编译工具

正则的pcre
压缩的zlib
ssl加密的openssl
全部下devel包;以及编译工具 gcc gcc++

[root@host2 ~]# yum install -y gcc gcc++ zlib-devel openssl-devel pcre-devel

2,下载源码包,解压,进入解压后目录

此处采用1.14版本的nginx

[root@host2 src]# pwd
/usr/local/src
[root@host2 src]# ll
total 992
-rw-r--r-- 1 root root 1015384 Aug 24 09:23 nginx-1.14.2.tar.gz
[root@host2 src]# tar -xf nginx-1.14.2.tar.gz 

[root@host2 nginx-1.14.2]# ll
total 728
drwxr-xr-x 6 1001 1001    326 Aug 27 15:46 auto
-rw-r--r-- 1 1001 1001 288742 Dec  4  2018 CHANGES
-rw-r--r-- 1 1001 1001 440121 Dec  4  2018 CHANGES.ru
drwxr-xr-x 2 1001 1001    168 Aug 27 15:46 conf
-rwxr-xr-x 1 1001 1001   2502 Dec  4  2018 configure
drwxr-xr-x 4 1001 1001     72 Aug 27 15:46 contrib
drwxr-xr-x 2 1001 1001     40 Aug 27 15:46 html
-rw-r--r-- 1 1001 1001   1397 Dec  4  2018 LICENSE
drwxr-xr-x 2 1001 1001     21 Aug 27 15:46 man
-rw-r--r-- 1 1001 1001     49 Dec  4  2018 README
drwxr-xr-x 9 1001 1001     91 Aug 27 15:46 src

3,创建nginx账户

[root@host2 src]# useradd -u 2000 -r -s /sbin/nologin nginx

4,三步走

利用自带的configure脚本,根据需要定制makefile文件即可,此处编译进了第三方模块:echo模块

[root@host2 src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
从github克隆echo模块的目录到本地

[root@host2 src]# ll
total 992
drwxr-xr-x 6 root root     186 Aug 27 15:51 echo-nginx-module
drwxr-xr-x 8 1001 1001     158 Dec  4  2018 nginx-1.14.2
-rw-r--r-- 1 root root 1015384 Aug 24 09:23 nginx-1.14.2.tar.gz


开始configure,选项如下:
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio --with-stream_realip_module --with-stream_ssl_module --with-stream --with-pcre --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/src/echo-nginx-module

make 
make install

4,配置环境变量

若configure指定的目录带有版本号,可给带有版本号的目录,做软链接,方便以后升级;

ln -s /usr/local/nginx-1.14.2/ /usr/local/nginx

[root@host2 nginx]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin:$PATH

[root@host2 nginx-1.14.2]# source /etc/profile.d/nginx.sh 

5,启动,访问

[root@host2 nginx-1.14.2]# 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
[root@host2 nginx-1.14.2]# nginx 
[root@host2 nginx-1.14.2]# ss -nlt
State      Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN     0      128                *:80                             *:*                  

[root@host2 nginx-1.14.2]# curl 192.168.80.101
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

[root@host2 conf]# ps aux |grep nginx
root       5903  0.0  0.2  46352  2088 ?        Ss   16:06   0:00 nginx: master process nginx
nginx     11956  0.0  0.2  46732  2064 ?        S    16:32   0:00 nginx: worker process
默认一主一worker
主进程身份为root,worker进程身份为nginx,configure时指定的,默认是nobody;
尤其注意:单独配置网页文件目录时,nginx用户对网页目录的权限问题;

安装后目录结构

[root@host2 nginx-1.14.2]# ll /usr/local/nginx/
total 0
drwxr-xr-x 2 root root 333 Aug 27 15:59 conf
drwxr-xr-x 2 root root  40 Aug 27 15:59 html
drwxr-xr-x 2 root root   6 Aug 27 15:59 logs
drwxr-xr-x 2 root root  19 Aug 27 15:59 sbin

分别为:
配置文件目录
网页文件目录
日志目录:configure时没指定到别的路径,access.log error.log nginx.pid都在里面
主程序目录,就一个nginx可执行程序文件,如果是tengine,还有个dso_install

drwx------ 2 nginx root   6 Aug 27 16:05 client_body_temp
drwxr-xr-x 2 root  root 333 Aug 27 15:59 conf
drwx------ 2 nginx root   6 Aug 27 16:05 fastcgi_temp
drwxr-xr-x 2 root  root  40 Aug 27 15:59 html
drwxr-xr-x 2 root  root  58 Aug 27 16:05 logs
drwx------ 2 nginx root   6 Aug 27 16:05 proxy_temp
drwxr-xr-x 2 root  root  19 Aug 27 15:59 sbin
drwx------ 2 nginx root   6 Aug 27 16:05 scgi_temp
drwx------ 2 nginx root   6 Aug 27 16:05 uwsgi_temp

启动后生成的目录,有:
客户端请求数据、后端fastcgi,scgi,uwsgi传来数据的临时文件存放目录;启用cache时的文件目录;

nginx主程序

nginx -h

nginx version: nginx/1.14.2
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/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

-t测试配置文件语法
-v查看版本号
-V查看编译时选项,生产中需要升级时先查看此项,确保兼容之前的编译选项;
-s 发送信号给nginx主程序
	stop 立刻停止
	quit 优雅停止,即先处理完当前的请求
	reopen reopening the log files
	reload 重新读取配置文件,修改配置后,一定!先-t测试语法,然后-s reload重载配置文件

配置文件目录

[root@host2 conf]# pwd
/usr/local/nginx/conf

名字自定义,建立不同的目录,用于存放不同类别的配置,在nginx.conf主配置中【合适位置】用include引入;
[root@host2 conf]# mkdir {vhosts,stream,mail,proxy}
[root@host2 conf]# ll -rt
total 68
-rw-r--r-- 1 root root 2223 Aug 27 15:59 koi-win
-rw-r--r-- 1 root root 2837 Aug 27 15:59 koi-utf
-rw-r--r-- 1 root root 3610 Aug 27 15:59 win-utf
-rw-r--r-- 1 root root 5170 Aug 27 15:59 mime.types mime指定多媒体文件扩展的文件,nginx.conf中引入
-rw-r--r-- 1 root root 5170 Aug 27 15:59 mime.types.default
-rw-r--r-- 1 root root 1007 Aug 27 15:59 fastcgi_params
-rw-r--r-- 1 root root 1007 Aug 27 15:59 fastcgi_params.default
-rw-r--r-- 1 root root 1077 Aug 27 15:59 fastcgi.conf
-rw-r--r-- 1 root root 1077 Aug 27 15:59 fastcgi.conf.default
-rw-r--r-- 1 root root  664 Aug 27 15:59 uwsgi_params
-rw-r--r-- 1 root root  664 Aug 27 15:59 uwsgi_params.default
-rw-r--r-- 1 root root  636 Aug 27 15:59 scgi_params
-rw-r--r-- 1 root root  636 Aug 27 15:59 scgi_params.default
-rw-r--r-- 1 root root 2656 Aug 27 15:59 nginx.conf
-rw-r--r-- 1 root root 2656 Aug 27 15:59 nginx.conf.default
drwxr-xr-x 2 root root    6 Aug 27 16:16 vhosts 虚拟主机
drwxr-xr-x 2 root root    6 Aug 27 16:16 stream 四层代理
drwxr-xr-x 2 root root    6 Aug 27 16:16 mail 邮件服务
drwxr-xr-x 2 root root    6 Aug 27 16:18 proxy 七层代理:http,fastcgi等

自带配置举例说明:
-rw-r--r-- 1 root root 1007 Aug 27 15:59 fastcgi_params fastcgi的参数文件
-rw-r--r-- 1 root root 1007 Aug 27 15:59 fastcgi_params.default  fastcgi的参数文件备份
-rw-r--r-- 1 root root 1077 Aug 27 15:59 fastcgi.conf  fastcgi的默认配置文件
fastcgi_params在配置php-fpm转发时,会引用,注意相对路径,用绝对路径更保险;

nginx.conf

  • 花括号
  • 分号:单行指令配置必须以分号结尾
  • 变量:可以引用nginx内建变量$varname,参见官网
  • set varname value; 自定义变量,其值可引用内建变量、文本,or两者结合
  • 5大配置段
    • main段:最上面,最外层,无大括号
    • event段:控制进程数量,并发数等
    • http段:web服务、http反代、fastcgi反代等
    • mail:邮件相关
    • stream:四层通用代理相关

默认配置:

[root@host2 conf]# cat nginx.conf |grep -Ev "^$" |grep -Ev "^[[:space:]]+#"
user  nginx; 
# worker进程启动用户身份

worker_processes  1;
# worker数量

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

#日志路径和级别,
#pid路径
#相对路径,相对于<prefix>,此处是/usr/local/nginx

# main段

events {
    worker_connections  1024;
    # 每个worker可以接收的最大并发连接数;
    # worker_processes * worker_connections 为理论上nginx并发数
}
# event段

http {
    include       mime.types; mime文件相对路径,
    default_type  application/octet-stream; 默认文件类型,不识别的默认行为是下载;
    sendfile        on; 启用sendfile零复制技术
    keepalive_timeout  65; 长连接时长为65s
    
    # 默认虚拟主机,监听端口80,主机名localhost
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html; #该location的网页文件所在目录,采用相对路径,此处为/usr/local/nginx/html
            index  index.html index.htm; #当访问的uri为目录时,寻找该目录下index.html或index.htm文件返回,只有前一个不存在时,才寻找后一个;
        }
        error_page   500 502 503 504  /50x.html; 发生500等这4个错误时,返回/50x.html这个uri给客户端;
        location = /50x.html { 
            root   html;
        }
        # 定义/50x.html这个uri在文件系统的路径;此处为/usr/local/nginx/html/50x.html;
    }
}
http段

stream {

}
mail {

}
# stream和mail段,默认无;

定义404.html

# 自定义404,覆盖默认的
...
   error_page  404              /404.html;

        location = /404.html {
            root   html;
        }

#
[root@host2 nginx]# echo my-def-404 > html/404.html
[root@host2 nginx]# nginx -s reload


#
[root@host1 ~]# curl 192.168.80.101/saasdfasdfas
my-def-404


# 原来自带的404页面,没找到在哪里,程序里写死了?
[root@host1 ~]# curl 192.168.80.101/saasdfasdfas
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>
updatedupdated2020-10-192020-10-19
加载评论