Nginx是一個(gè)開(kāi)源且高性能、可靠的HTTP中間件、代理服務(wù)。
2. 常見(jiàn)的HTTP服務(wù)httpd - Apache
IIS - 微軟
GWE - Google
tomcat - Sun
二、為什么選擇Nginx1. IO多路復(fù)用epoll什么是IO多路復(fù)用多個(gè)描述符的I/O操作都能在一個(gè)線程內(nèi)并發(fā)交替地順序完成,這就叫I/O多路復(fù)用,這里的“復(fù)用”指的是復(fù)用同一個(gè)線程。
什么是epollIO多路服用的實(shí)現(xiàn)方式:select、poll、epollselect基本原理:
select 函數(shù)監(jiān)視的文件描述符分3類(lèi),分別是writefds、readfds、和exceptfds。調(diào)用后select函數(shù)會(huì)阻塞,直到有描述符就緒(有數(shù)據(jù) 可讀、可寫(xiě)、或者有except),或者超時(shí)(timeout指定等待時(shí)間,如果立即返回設(shè)為null即可),函數(shù)返回。當(dāng)select函數(shù)返回后,可以通過(guò)遍歷fdset,來(lái)找到就緒的描述符。
select缺點(diǎn):
1.能夠監(jiān)視文件描述符的數(shù)量存在最大限制。
2.線性掃描效率低下。
基本原理:
epoll支持水平觸發(fā)和邊緣觸發(fā),最大的特點(diǎn)在于邊緣觸發(fā),它只告訴進(jìn)程哪些fd剛剛變?yōu)榫途w態(tài),并且只會(huì)通知一次。還有一個(gè)特點(diǎn)是,epoll使用“事件”的就緒通知方式,通過(guò)epoll_ctl注冊(cè)fd,一旦該fd就緒,內(nèi)核就會(huì)采用類(lèi)似callback的回調(diào)機(jī)制來(lái)激活該fd,epoll_wait便可以收到通知。
epoll的優(yōu)點(diǎn):
1.沒(méi)有最大并發(fā)連接的限制,能打開(kāi)的FD的上限遠(yuǎn)大于1024(1G的內(nèi)存上能監(jiān)聽(tīng)約10萬(wàn)個(gè)端口)。
2.效率提升,不是輪詢(xún)的方式,不會(huì)隨著FD數(shù)目的增加效率下降。
3.內(nèi)存拷貝,利用mmap()文件映射內(nèi)存加速與內(nèi)核空間的消息傳遞;即epoll使用mmap減少?gòu)?fù)制開(kāi)銷(xiāo)。
CPU親和性(affinity)是一種把CPU核心和Nginx工作進(jìn)程綁定方式,把每個(gè)worker進(jìn)程固定在一個(gè)CPU上執(zhí)行,減少CPU的cache miss,獲得更好的性能。
4. sendfile三、Nginx的快速搭建和基本參數(shù)(CentOS7)1. yum方式安裝【參考】創(chuàng)建/etc/yum.repos.d/nginx.repo文件,并輸入如下內(nèi)容[nginx]name=nginx repobaseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/gpgcheck=0enabled=1OS 可選值有 centos 和 rhel。
[root~]# nginx -vnginx version: nginx/1.14.02. 編譯參數(shù)詳解查看nginx安裝時(shí)的編譯參數(shù)
nginx -V
[root~]# nginx -Vnginx version: nginx/1.14.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)built with OpenSSL 1.0.2k-fips 26 Jan 2017TLS SNI support enabledconfigure 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_html' target='_blank'>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安裝編譯參數(shù)詳解【參考】編譯選項(xiàng)作用--prefix=/etc/nginx配置文件目錄--sbin-path=/usr/sbin/nginx可執(zhí)行文件名稱(chēng)和所在目錄--modules-path=/usr/lib64/nginx/modulesnginx動(dòng)態(tài)模塊的安裝目錄--conf-path=/etc/nginx/nginx.conf主配置文件名稱(chēng)和所在目錄--error-log-path=/var/log/nginx/error.log全局錯(cuò)誤日志文件名稱(chēng)和所在目錄--http-log-path=/var/log/nginx/access.logHTTP服務(wù)器的主請(qǐng)求日志文件的名稱(chēng)和所在目錄--pid-path=/var/run/nginx.pidnginx.pid所在目錄,這是儲(chǔ)存主進(jìn)程的進(jìn)程ID文件--lock-path=/var/run/nginx.locknginx.lock所在目錄--http-client-body-temp-path=/var/cache/nginx/client_temp
rpm -ql nginx
[root~]# rpm -ql nginx/etc/logrotate.d/nginx/etc/nginx/etc/nginx/nginx.conf/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/win-utf/etc/nginx/mime.types/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/etc/nginx/modules/usr/sbin/nginx/usr/sbin/nginx-debug/usr/share/doc/nginx-1.14.0/usr/share/doc/nginx-1.14.0/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/usr/libexec/initscripts/legacy-actions/nginx/usr/libexec/initscripts/legacy-actions/nginx/check-reload/usr/libexec/initscripts/legacy-actions/nginx/upgrade默認(rèn)路徑類(lèi)型作用/etc/logrotate.d/nginx配置文件Nginx日志輪轉(zhuǎn),用于logrotate服務(wù)的日志切割/etc/nginx
Nginx的master進(jìn)程檢查配置文件的正確性,若是錯(cuò)誤則返回錯(cuò)誤信息,nginx繼續(xù)采用原配置文件進(jìn)行工作(因?yàn)閣orker未受到影響)
Nginx啟動(dòng)新的worker進(jìn)程,采用新的配置文件
Nginx將新的請(qǐng)求分配新的worker進(jìn)程
Nginx等待以前的worker進(jìn)程的全部請(qǐng)求都返回后,關(guān)閉相關(guān)worker進(jìn)程
重復(fù)上面過(guò)程,直到全部舊的worker進(jìn)程都被關(guān)閉掉
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP !
相關(guān)推薦:
Nginx負(fù)載調(diào)度器+雙Tomcat負(fù)載及會(huì)話共享+MySQL后端數(shù)據(jù)庫(kù)
使用nginx在一臺(tái)服務(wù)器部署多個(gè)Web Server
以上就是快速搭建Nginx及其基本參數(shù)的配置的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選