Nginx学习笔记2-Nginx的中间件架构

中间件通俗理解:

将具体业务和底层逻辑解耦的组件。

大致的效果是:
需要利用服务的人(前端写业务的),不需要知道底层逻辑(提供服务的)的具体实现,只要拿着中间件结果来用就好了。

举个例子:
我开了一家炸鸡店(业务端),然而周边有太多屠鸡场(底层),为了成本我肯定想一个个比价,再综合质量挑选一家屠鸡场合作(适配不同底层逻辑)。由于市场变化,合作一段时间后,或许性价比最高的屠鸡场就不是我最开始选的了,我又要重新和另一家屠鸡场合作,进货方式、交易方式等等全都要重来一套(重新适配)。

然而我只想好好做炸鸡,有性价比高的肉送来就行。于是我找到了一个专门整合屠鸡场资源的第三方代理(中间件),跟他谈好价格和质量后(统一接口),从今天开始,我就只需要给代理钱,然后拿肉就行。代理负责保证肉的质量,至于如何根据实际性价比,选择不同的屠鸡场,那就是代理做的事了。


作者:Gocy
链接:https://www.zhihu.com/question/19730582/answer/140527549
来源:知乎

---------------------------------------------------------------------------------------------------------------------------------

什么是Nginx

nginx简述

Nginx是一个开源且高性能、可靠的HTPP中间件(Nginx在企业里应用最多的)、代理服务。

常见的HTTP服务

和Nginx类似的HTTP代理服务

HTTPD-Apache基金会
IIS -微软
GWS -Google 不对外开放


为什么选择Nginx

原因一、IO多路服用epoll

一、什么是IO复用


多并发就会产生多个请求,处理多个并发的请求对于中间件就会产生多个IO流,IO流请求系统内核有并行处理和串行处理两种方式,串行处理方式是一个一个处理,如果前一个发生阻塞后一个就无法发送请求,所以说我们用的是并行方式来完成整个IO流,以实现最大的并发和吞吐,这时就用了IO复用技术,IO复用技术就是用一个Socket来完成整个IO流。

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

二、什么是epoll

IO多路复用的实现方式selct、poll、epoll

原因二、轻量级

功能代码少
代码模块化

原因三、CPU亲和(affinity)

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


原因四、sendfile

基于Nginx的中间件架构

一、Nginx快速搭建与基本参数使用

Mainline version -开发版
stable vesion -稳定版
Legacy vesion -历史版本


安装nginx

首先在/etc/yum.repos.d中新建一个nginx.repo 文件
其中centos代表当前操作系统,7代表版本号,这段代码是从Nginx官方上复制出来并修改的。
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/    
gpgcheck=0
enabled=1
之后我们列出nginx的版本:
yum list | grep nginx
collectd-nginx.x86_64                   5.8.0-1.el7                    epel     
munin-nginx.noarch                      2.0.33-1.el7                   epel     
nextcloud-nginx.noarch                  10.0.4-2.el7                   epel     
nginx.x86_64                            1:1.12.2-1.el7_4.ngx           nginx    
nginx-all-modules.noarch                1:1.12.2-1.el7                 epel     
nginx-debug.x86_64                      1:1.8.0-1.el7.ngx              nginx    
nginx-debuginfo.x86_64                  1:1.12.2-1.el7_4.ngx           nginx    
nginx-filesystem.noarch                 1:1.12.2-1.el7                 epel     
nginx-mod-http-geoip.x86_64             1:1.12.2-1.el7                 epel     
nginx-mod-http-image-filter.x86_64      1:1.12.2-1.el7                 epel     
nginx-mod-http-perl.x86_64              1:1.12.2-1.el7                 epel     
nginx-mod-http-xslt-filter.x86_64       1:1.12.2-1.el7                 epel     
nginx-mod-mail.x86_64                   1:1.12.2-1.el7                 epel     
nginx-mod-stream.x86_64                 1:1.12.2-1.el7                 epel     
nginx-module-geoip.x86_64               1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-geoip-debuginfo.x86_64     1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-image-filter.x86_64        1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-image-filter-debuginfo.x86_64
                                        1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-njs.x86_64                 1:1.12.2.0.1.14-1.el7_4.ngx    nginx    
nginx-module-njs-debuginfo.x86_64       1:1.12.2.0.1.14-1.el7_4.ngx    nginx    
nginx-module-perl.x86_64                1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-perl-debuginfo.x86_64      1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-xslt.x86_64                1:1.12.2-1.el7_4.ngx           nginx    
nginx-module-xslt-debuginfo.x86_64      1:1.12.2-1.el7_4.ngx           nginx    
nginx-nr-agent.noarch                   2.0.0-12.el7.ngx               nginx    
owncloud-nginx.noarch                   9.1.5-1.el7                    epel     
pcp-pmda-nginx.x86_64                   3.11.8-7.el7                   os       
python2-certbot-nginx.noarch            0.20.0-1.el7                   epel 
我们用加下划线的版本
之后我们使用
yum install nginx
对nginx进行安装



nginx -v //查看当前nginx版本
nginx -V //看到编译的参数

基本参数使用

安装目录:
命令:
rpm -ql nginx
[root@VM_69_65_centos ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params

/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/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
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.12.2
/usr/share/doc/nginx-1.12.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
安装目录讲解:
/ect/logrotate.d/nginx 配置文件 作用是Nginx日志轮转,用于logrotate服务的日志切割
(比如说可以把一个日志按天轮转切割)
--------------------------------------------------------------------------------------------------------------
        /etc/nginx
/etc/nginx/conf.d
/etc/nginx/nginx.conf  //主要配置
/etc/nginx/conf.d/default.conf //默认配置文件

类型是目录、配置文件,作用:Nginx主配置文件
--------------------------------------------------------------------------------------------------------------
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
配置文件,cgi配置相关,fastcgi配置

--------------------------------------------------------------------------------------------------------------
/etc/nginx/mime.types
设置http协议的Content-Type与扩展名对应关系
--------------------------------------------------------------------------------------------------------------

/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
用于配置出系统守护进程管理器管理方式
--------------------------------------------------------------------------------------------------------------
/etc/nginx/modules
/usr/lib64/nginx/modules
Nignx模块目录
--------------------------------------------------------------------------------------------------------------
/usr/sbin/nginx
/usr/sbin/nginx-debug
Nginx服务启动管理终端命令
--------------------------------------------------------------------------------------------------------------
/usr/share/doc/nginx-1.12.2
/usr/share/doc/nginx-1.12.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
Nginx的手册和帮助文件
--------------------------------------------------------------------------------------------------------------
/var/cache/nginx
Nginx的缓存目录

--------------------------------------------------------------------------------------------------------------
/var/log/nginx
Nginx的日志目录
--------------------------------------------------------------------------------------------------------------






编译参数:
nginx -V

[root@VM_69_65_centos ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --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 --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=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 --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --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' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
-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
--lock-path=/var/run/nginx.lock
安装目的目录或路径
--------------------------------------------------------------------------------------------------------------


进入nginx的主目录 /etc/nginx/nginx.conf


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; //超时时间65S


    #gzip  on;


    include /etc/nginx/conf.d/*.conf;
}                   

之后我们查看 /etc/nginx/nginx.conf


进入nginx的主目录 /etc/nginx/conf.d/default.conf

server {
    listen       80; //监听的端口是80
    server_name  localhost;//server的服务名

    #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;
    #}
}









进入nginx的主目录 /etc/nginx/nginx.conf
进入nginx的主目录 /etc/nginx/nginx.conf

猜你喜欢

转载自blog.csdn.net/qq_22570497/article/details/79066331
今日推荐