Nginx的中间件架构

发布 : 2017-08-16 分类 : 大数据 浏览 :

准备

1
Cetntos 7

安装基本库

1
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

安装基本工具

1
yum -y install wget httpd-tools vim

初始化

1
cd /data/nginx/;mkdir app download logs work backup
  • /data/nginx
    • app:代码目录
    • download:源码包
    • logs:自定义日志
    • work:shell脚本
    • backup:默认配置文件

查看yum源是否可用

1
yum list | grep gcc

查看是否有iptables规则

1
iptables -L

All text

1
iptables -t nat -L

关闭iptables规则

1
2
iptables -F
iptables -t nat -F

查看命令

1
[root@sparsematrix nginx]# getenforce

All text

Nginx简述

1
Nginx是一个开源且高性能、可靠的HTTP中间件、代理服务

常见的HTTP服务

  • HTTPD - Apache基金会
  • IIS - 微软
  • GWS - Google

为什么选择Nginx

IO多路复用epoll/并发问题

多个描述符I/O操作都能在一个线程内并发交替地顺序完成,这就叫I/O多路复用,这里的”复用”指的是复用同一个线程

All text

什么是epoll

1
IO多路复用的实现方式select、poll、epoll
  • select:结账的时候,服务员不告诉老板哪桌要结账,老板一个个去问才知道哪桌要结账
  • epoll:结账的时候,服务员告诉老板哪桌要结账

轻量级

  • 功能模块少
  • 代码模块化

CPU亲和(affinity)

CPU亲和

是一种把CPU核心和Nginx工作进程绑定的方式,把每个worker进程固定在一个CPU上执行,减少切换CPU的cache miss,获得更好的性能

All text

0拷贝

Nginx安装

  • Mainline version:开发版
  • Stable version:稳定版
1
[root@sparsematrix ~]# vim /etc/yum.repos.d/nginx.repo
1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

All text

查看yum源中nginx相关版本

1
[root@sparsematrix ~]# yum list | grep nginx

使用yum源安装Nginx

1
[root@sparsematrix ~]# yum install nginx -y

查看当前安装nginx版本

1
[root@sparsematrix ~]# nginx -v

All text

查看Nginx编译参数

1
[root@sparsematrix ~]# nginx -V

All text

Nginx安装编译参数详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
--prefix=/etc/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 【安装目的目录或路径】
--pid-path=/var/run/nginx.pid 【安装目的目录或路径】
--lock-path=/var/run/nginx.lock 【安装目的目录或路径】
--http-client-body-temp-path=/var/cache/nginx/client_temp【执行对应模块时,Nginx所保留的临时性文件】
--http-proxy-temp-path=/var/cache/nginx/proxy_temp 【执行对应模块时,Nginx所保留的临时性文件】
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 【执行对应模块时,Nginx所保留的临时性文件】
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 【执行对应模块时,Nginx所保留的临时性文件】
--http-scgi-temp-path=/var/cache/nginx/scgi_temp【执行对应模块时,Nginx所保留的临时性文件】
--user=nginx
--group=nginx【设定Nginx进程启动的用户和组用户】
--with-compat
--with-file-aio
--with-threads
--with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_mp4_module
--with-http_random_index_module【目录中选择一个随机主页】
Syntax:random_index on | off
Default:random_index off
Context:location
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module【Nginx的客户端状态】
Syntax:stub_status
Default: -
Context: server,location
--with-http_sub_module
--with-http_v2_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'【设置额外的参数将被添加到CFLAGS变量】
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'【设置附加的参数,链接系统库】

Nginx安装目录解析

1
使用yum源安装其实安装的都是一个个rpm包

使用rpm -ql查看nginx的安装目录

1
[root@sparsematrix ~]# rpm -ql nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/etc/logrotate.d/nginx【配置文件】Nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx【目录、配置文件】Nginx主配置文件
/etc/nginx/conf.d【目录、配置文件】Nginx主配置文件
/etc/nginx/conf.d/default.conf【目录、配置文件】Nginx主配置文件
/etc/nginx/fastcgi_params【配置文件】cgi配置相关,fastcgi配置
/etc/nginx/koi-utf【配置文件】编码转换映射转化文件
/etc/nginx/koi-win【配置文件】编码转换映射转化文件
/etc/nginx/mime.types【配置文件】设置http协议的Content-Type与拓展名对应关系
/etc/nginx/modules【目录】Nginx模块目录
/etc/nginx/nginx.conf【目录、配置文件】Nginx主配置文件
/etc/nginx/scgi_params【配置文件】cgi配置相关,fastcgi配置
/etc/nginx/uwsgi_params【配置文件】cgi配置相关,fastcgi配置
/etc/nginx/win-utf【配置文件】编码转换映射转化文件
/etc/sysconfig/nginx【配置文件】用于配置出系统守护进程管理器管理方式
/etc/sysconfig/nginx-debug【配置文件】用于配置出系统守护进程管理器管理方式
/usr/lib/systemd/system/nginx-debug.service【配置文件】用于配置出系统守护进程管理器管理方式
/usr/lib/systemd/system/nginx.service【配置文件】用于配置出系统守护进程管理器管理方式
/usr/lib64/nginx
/usr/lib64/nginx/modules【目录】Nginx模块目录
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx【命令】Nginx服务的启动管理的终端命令
/usr/sbin/nginx-debug【命令】Nginx服务的启动管理的终端命令
/usr/share/doc/nginx-1.12.1【文件、目录】Nginx的手册和帮助文件
/usr/share/doc/nginx-1.12.1/COPYRIGHT【文件、目录】Nginx的手册和帮助文件
/usr/share/man/man8/nginx.8.gz【文件、目录】Nginx的手册和帮助文件
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx【目录】Nginx的缓存目录
/var/log/nginx【目录】Nginx的日志目录

1
[root@sparsematrix ~]# yum install -y curl

curl可以理解为浏览器

1
[root@sparsematrix ~]# curl -v http://www.baidu.com

启动Nginx

1
[root@sparsematrix ~]# /usr/sbin/nginx

All text

关闭nginx

1
[root@sparsematrix ~]# nginx -s stop

平滑启动 nginx

1
nginx -s reload

Nginx默认配置语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@sparsematrix ~]# cd /etc/nginx/
[root@sparsematrix nginx]# cat nginx.conf

user nginx;# user【设置Nginx服务的系统使用用户】
worker_processes 1;# worker_processes【工作进程数】

error_log /var/log/nginx/error.log warn;# error_log【Nginx的错误日志】
pid /var/run/nginx.pid;# pid【Nginx服务启动时候pid】

# events worker_connections【每个进程允许最大连接数】
# events use【工作进程数】
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;
}

查看/etc/nginx/conf.d/default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@sparsematrix nginx]# cd /etc/nginx/conf.d/
[root@sparsematrix conf.d]# ls
default.conf
[root@sparsematrix conf.d]# cat default.conf
server {
listen 8088;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

检查配置文件语法

1
[root@sparsematrix ~]# nginx -t -c /etc/nginx/nginx.conf

All text

log_format

查看access_log

1
[root@sparsematrix ~]# head /var/log/nginx/access.log

All text

All text

Nginx模块

  • 官方模块
  • 第三方模块

stub_status

1
2
3
4
[root@sparsematrix ~]# vi /etc/nginx/conf.d/default.conf
location /mystatus {
stub_status;
}

All text

All text

random_index

1
2
3
4
5
6
7
[root@sparsematrix test]# pwd
/data/nginx/test
[root@sparsematrix test]# ll
总用量 12
-rw-r--r--. 1 root root 149 8月 21 08:05 1.html
-rw-r--r--. 1 root root 149 8月 21 08:06 2.html
-rw-r--r--. 1 root root 151 8月 21 08:06 3.html
1
2
3
4
5
6
7
[root@sparsematrix ~]# vi /etc/nginx/conf.d/default.conf
location / {
#root /usr/share/nginx/html;
root /data/nginx/test;
random_index on;
#index index.html index.htm;
}

不断刷新浏览器

All text

All text

All text

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/08/16/Nginx的中间件架构/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹