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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章