概述 :
html
Nginx ("engine x") 是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP服務器。Nginx是由Igor Sysoev爲俄羅斯訪問量第二的Rambler.ru站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名nginx
Nginx的程序架構:
master/worker
一個master進程:
負載加載配置文件、管理worker進程、平滑升級
一個或多個worker進程
處理並響應用戶請求
緩存相關的進程:
cache loader:載入緩存對象
cache manager:管理緩存對象web
nginx高度模塊塊:高度模塊化,但其模塊早期不支持DSO機制;近期版本支持動態裝載和卸載;
模塊分類:
核心模塊:core module
標準模塊:
Standard HTTP modules
Optional HTTP modules
Mail modules
Stream modules
3rd party modulecentos
特性:異步、事件驅動和非阻塞
併發請求處理:經過kevent/epoll/select
文件IO:高級IO sendfile,異步,mmap緩存
sendfile : 用戶空間沒法調用系統硬件,只有經過內核調用,當硬件將信息返回給內核後,內核直接製做相應報文迴應。bash
nginx的功用:服務器
靜態的web資源服務器;
結合FastCGI/uwSGI/SCGI等協議反代動態資源請求;(反向代理服務器,且還能緩存)
http/https協議的反向代理;(LNMP或者LANMP)
imap4/pop3協議的反抽代理;
tcp/udp協議的反代;架構
nginx的安裝配置:
併發
1)官方的預製包:
http://nginx.org/packages/centos/7/x86_64/RPMS/
app
2)編譯安裝
實例:
實驗環境:CentOS 7.2
源碼包:nginx-1.10.0.tar.gz
#安裝必要的開發包組以及相關開發包 [root@dashui ~]# yum groupinstall "Development Tools" [root@dashui ~]# yum install pcre-devel openssl-devel zlib-devel -y #添加nginx用戶 [root@dashui ~]# useradd -r nginx #解壓nginx源碼包 [root@dashui ~]# tar xf nginx-1.10.0.tar.gz #切換至解壓目錄,而後編譯安裝 [root@dashui ~]# ./configure --prefix=/usr/local/nginx --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 --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 #安裝 [root@dashui ~]# make && make install #添加nginx的bin目錄至PATH環境變量 [root@dashui ~]# echo "export PATH=/usr/local/nginx/bin:PATH" > /etc/profile.d/nginx.sh #啓動Nginx [root@dashui ~]# nginx [root@dashui ~]# nginx [root@dashui ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::*
Nginx的配置文件:
配置文件的組成部分:
主配置文件:nginx.conf
include conf.d/*.conf
fastcgi, uwsgi,scgi等協議相關的配置文件
mime.types:支持的mime類型
主配置文件的配置指令:
directive value [value2 ...];
注意:
(1) 指令必須以分號結尾;
(2) 支持使用配置變量;
內建變量:由Nginx模塊引入,可直接引用;
自定義變量:由用戶使用set命令定義;
set variable_name value;
引用變量:$variable_name
主配置文件結構:
main block:主配置段,也即全局配置段;
#實例 1 2 #user nobody; 3 worker_processes 1; 4 5 #error_log logs/error.log; 6 #error_log logs/error.log notice; 7 #error_log logs/error.log info; 8 9 #pid logs/nginx.pid; 10 11
event {
...
}:事件驅動相關的配置;
12 events { 13 worker_connections 1024; 14 } 15
http {
...
}:http/https 協議相關的配置段;
http { 18 include mime.types; 19 default_type application/octet-stream; 20 21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 22 # '$status $body_bytes_sent "$http_referer" ' 23 # '"$http_user_agent" "$http_x_forwarded_for"'; 24 25 #access_log logs/access.log main; 26 27 sendfile on; 28 #tcp_nopush on; 29 30 #keepalive_timeout 0; 31 keepalive_timeout 65; 32 33 #gzip on; 34 35 server { 36 listen 80; 37 server_name localhost; 38 39 #charset koi8-r; 40 41 #access_log logs/host.access.log main; 42 .....省略..... 81 82 # another virtual host using mix of IP-, name-, and port-based configuration 83 # 84 #server { 85 # listen 8000; 86 # listen somename:8080; 87 # server_name somename alias another.alias; 88 89 # location / { 90 # root html; 91 # index index.html index.htm; 92 # } 93 #}
mail { ... } stream { ... }