nginx 第一章:編譯安裝php
1.基礎說明html
基本HTTP服務器功能
其餘HTTP服務器功能
郵件代理服務器功能
TCP/UDP代理服務器功能
體系結構和可伸縮性
經測試的操做系統和平臺mysql
nginx [engine x]是一個HTTP和反向代理服務器,一個郵件代理服務器和一個通用的TCP / UDP代理服務器,最初由Igor Sysoev編寫。很長一段時間,它一直在許多負載很重的俄羅斯網站上運行,包括 Yandex, Mail.Ru, VK和 Rambler。據Netcraft稱,nginx 在2018年12月服務或代理了 25.89%最繁忙的網站。如下是一些成功案例: Dropbox, Netflix, Wordpress.com, FastMail.FM。nginx
1.基本的HTTP服務器功能c++
提供靜態和 索引 文件, 自動索引 ; 打開文件描述符緩存 ;
經過緩存加速反向代理 ; 負載均衡和容錯 ;
經過緩存FastCGI, uwsgi, SCGI和 memcached 服務器加速支持 ; 負載均衡和容錯 ;
模塊化架構。過濾器包括 gzipping,字節範圍,分塊響應, XSLT, SSI和圖像變換過濾器。若是由代理或FastCGI / uwsgi / SCGI服務器處理,則能夠並行處理單個頁面中的多個SSI包含;
SSL和TLS SNI支持 ;
支持HTTP / 2, 具備加權和基於依賴性的優先級。web
2.其餘HTTP服務器功能
基於名稱和基於IP的 虛擬服務器 ;
保持活躍 和流水線鏈接支持;
訪問日誌格式, 緩衝日誌寫入, 快速日誌輪換和 syslog日誌記錄 ;
3xx-5xx錯誤代碼 重定向 ;
重寫模塊: 使用正則表達式更改URI ;
根據 客戶端地址執行不一樣的功能 ;
基於客戶端IP地址, 密碼(HTTP基自己份驗證)和 子請求結果的訪問控制 ;
驗證 HTTP referer ;
該PUT,DELETE,MKCOL,複製和移動方法;
FLV 和 MP4 流媒體;
響應速度限制 ;
限制 來自一個地址的同時鏈接 或 請求的數量 ;
基於IP的地理定位 ;
A / B測試 ;
請求鏡像 ;
嵌入式Perl ;
njs腳本語言。正則表達式
3.郵件代理服務器功能sql
用戶 使用外部HTTP 身份驗證 服務器重定向到 IMAP 或 POP3服務器;緩存
使用外部HTTP 身份驗證 服務器進行用戶身份 驗證,並將鏈接重定向到內部 SMTP服務器;
驗證方法:
POP3:USER / PASS,APOP,AUTH LOGIN / PLAIN / CRAM-MD5;
IMAP:登陸,AUTH LOGIN / PLAIN / CRAM-MD5;
SMTP:AUTH LOGIN / PLAIN / CRAM-MD5;bash
SSL支持;
STARTTLS和STLS支持。
4.TCP / UDP代理服務器功能
TCP和UDP的通用代理 ;
SSL和TLS SNI支持TCP;
負載均衡和容錯 ;
基於客戶端地址的訪問控制 ;
根據客戶端地址執行不一樣的功能 ;
限制 來自一個地址的同時鏈接數 ;
訪問日誌格式, 緩衝日誌寫入, 快速日誌輪換和 syslog日誌記錄 ;
基於IP的地理定位 ;
A / B測試 ;
njs腳本語言。
5.架構和可擴展性
一個主要和幾個工人流程; 工做進程在非特權用戶下運行;
配置靈活 ;
在不中斷客戶端服務的狀況下從新配置 和升級可執行文件 ;
支持 kqueue(FreeBSD 4.1+),epoll(Linux 2.6+),/ dev / poll(Solaris 7 11/99 +),事件端口(Solaris 10),select和poll;
支持各類kqueue功能,包括EV_CLEAR,EV_DISABLE(暫時禁用事件),NOTE_LOWAT,EV_EOF,可用數據的數量,錯誤代碼;
支持各類epoll功能,包括EPOLLRDHUP(Linux 2.6.17 +,glibc 2.8+)和EPOLLEXCLUSIVE(Linux 4.5 +,glibc 2.24+);
sendfile(FreeBSD 3.1 +,Linux 2.2 +,macOS 10.5+),sendfile64(Linux 2.4.21+)和sendfilev(Solaris 8 7/01 +)支持;
文件AIO (FreeBSD 4.3 +,Linux 2.6.22+);
DIRECTIO (FreeBSD 4.4 +,Linux 2.4 +,Solaris 2.6 +,macOS);
Accept-filters(FreeBSD 4.1 +,NetBSD 5.0+)和TCP_DEFER_ACCEPT(Linux 2.4+) 支持 ;
10,000個非活動HTTP保持活動鏈接佔用大約2.5M內存;
數據複製操做保持在最低限度。
6.經測試的操做系統和平臺
FreeBSD 3 - 11 / i386; FreeBSD 5 - 11 / amd64;
Linux 2.2 - 4 / i386; Linux 2.6 - 4 / amd64; Linux 3 - 4 / armv6l,armv7l,aarch64,ppc64le;
Solaris 9 / i386,sun4u; Solaris 10 / i386,amd64,sun4v;
AIX 7.1 / powerpc;
HP-UX 11.31 / ia64;
macOS / ppc,i386;
Windows XP,Windows Server 2003。
二.安裝
一、環境準備:先安裝準備環境
yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
gcc爲GNU Compiler Collection的縮寫,能夠編譯C和C++源代碼等,它是GNU開發的C和C++以及其餘不少種語言 的編譯器(最先的時候只能編譯C,後來很快進化成一個編譯多種語言的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。) gcc 在編譯C++源代碼的階段,只能編譯 C++ 源文件,而不能自動和 C++ 程序使用的庫連接(編譯過程分爲編譯、連接兩個階段,注意不要和可執行文件這個概念搞混,相對可執行文件來講有三個重要的概念:編譯(compile)、連接(link)、加載(load)。
源程序文件被編譯成目標文件,多個目標文件連同庫被連接成一個最終的可執行文件,可執行文件被加載到內存中運行)。所以,一般使用 g++ 命令來完成 C++ 程序的編譯和鏈接,該程序會自動調用 gcc 實現編譯。 gcc-c++也能編譯C源代碼,只不過把會把它當成C++源代碼,後綴爲.c的,gcc把它看成是C程序,而g++看成是c++程序;後綴爲.cpp的,二者都會認爲是c++程序,注意,雖然c++是c的超集,可是二者對語法的要求是有區別的。 automake是一個從Makefile.am文件自動生成Makefile.in的工具。爲了生成Makefile.in,automake還需用到perl,因爲automake建立的發佈徹底遵循GNU標準,因此在建立中不須要perl。libtool是一款方便生成各類程序庫的工具。 pcre pcre-devel:在Nginx編譯須要 PCRE(Perl Compatible Regular Expression),由於Nginx 的Rewrite模塊和HTTP 核心模塊會使用到PCRE正則表達式語法。 zlip zlib-devel:nginx啓用壓縮功能的時候,須要此模塊的支持。 openssl openssl-devel:開啓SSL的時候須要此模塊的支持。
安裝pcre 和zlib
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz tar xvf pcre-8.42.tar.gz cd pcre-8.42 ./configure make make install wget https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make make install
二、下載nginx 安裝包: 官網地址:http://nginx.org/
截止當前最新穩定版本爲 nginx-1.14.2,在Linux 下使用 wget 進行下載:
[root@localhost sbin]# cd /usr/local/src/ [root@localhost src]# ls [root@localhost src]# wget http://120.52.51.15/nginx.org/download/nginx-1.14.2.tar.gz
--2019-01-04 04:33:01-- http://120.52.51.15/nginx.org/download/nginx-1.14.2.tar.gz
Connecting to 120.52.51.15:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1015384 (992K) [application/octet-stream] Saving to: ‘nginx-1.14.2.tar.gz’ 100%[=================================================>] 1,015,384 --.-K/s in 0.1s 2019-01-04 04:33:01 (7.43 MB/s) - ‘nginx-1.14.2.tar.gz’ saved [1015384/1015384]
3.解壓安裝包
[root@localhost src]# [root@localhost src]# [root@localhost src]# tar xvf nginx-1.14.2.tar.gz ^C [root@localhost src]# cd nginx-1.14.2 [root@localhost nginx-1.14.2]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.14.2]# pwd /usr/local/src/nginx-1.14.2 [root@localhost nginx-1.14.2]#
4.開始編譯安裝
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 [root@localhost nginx-1.14.2]# make [root@localhost nginx-1.14.2]# make install
備註:nginx完成安裝之後,有四個主要的目錄:
conf:保存nginx全部的配置文件,其中nginx.conf是nginx服務器的最核心最主要的配置文件,其餘的.conf則是用來配置nginx相關的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params兩個文件,配置文件通常都有個樣板配置文件,是文件名.default結尾,使用的使用將其複製爲並將default去掉便可。 html目錄中保存了nginx服務器的web文件,可是能夠更改成其餘目錄保存web文件,另外還有一個50x的web文件是默認的錯誤頁面提示頁面。 logs:用來保存nginx服務器的訪問日誌錯誤日誌等日誌,logs目錄能夠放在其餘路徑,好比/var/logs/nginx裏面。 sbin:保存nginx二進制啓動腳本,能夠接受不一樣的參數以實現不一樣的功能。
5.經過命令啓動和關閉nginx:
[root@localhost sbin]# ./nginx nginx: [emerg] getpwnam("nginx") failed [root@localhost sbin]# useradd -s /s sbin/ srv/ sys/ [root@localhost sbin]# useradd -s /sbin/nologin -M nginx [root@localhost sbin]# id nginx uid=1000(nginx) gid=1000(nginx) groups=1000(nginx) [root@localhost sbin]# ls nginx [root@localhost sbin]# ./nginx [root@localhost sbin]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@localhost sbin]#
6.從新讀取配置文件和關閉服務:
/usr/local/nginx/sbin/nginx/nginx #啓動 服務 /usr/local/nginx/sbin/nginx/nginx -s reload #不中止服務重讀配置文件 /usr/local/nginx/sbin/nginx/nginx -s stop #中止服務 #中止服務
7.驗證端口是否開啓:
[root@localhost sbin]# [root@localhost sbin]# ps -ef |grep nginx root 1314 1 0 05:10 ? 00:00:00 nginx: master process ./nginx # nginx 主進程只有一個 nginx 1315 1314 0 05:10 ? 00:00:00 nginx: worker process #nginx 工做進程,能夠經過配置文件修改 root 1319 21710 0 05:13 pts/1 00:00:00 grep --color=auto nginx [root@localhost sbin]# ss -tnl #查看開啓的監聽端口 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@localhost sbin]# lsof -i:80 #查看 80 端口綁定的應用程序 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 1314 root 6u IPv4 27941366 0t0 TCP *:http (LISTEN) nginx 1315 nginx 6u IPv4 27941366 0t0 TCP *:http (LISTEN) [root@localhost sbin]#
支持的信號:
kill -QUIT 13337 #平緩關閉Nginx,即再也不接受新的請求,可是等當前請求處理完畢後再關閉Nginx。 kill -TERM 21665 #快速中止Nginx服務 kill -HUP 21703 #使用新的配置文件啓動進程而後平緩中止原有的nginx進程,即平滑重啓。 kill -USR1 21703 #從新打開配置文件,用於nginx 日誌切割 日誌切割的腳本: --------------------------------------------------------------------------------------- #!/bin/bash PID=`cat /var/run/nginx/nginx.pid` mv /var/log/nginx/error.log /var/log/nginx/`date +%Y_%m_%d:%H:%M:%S`.error.log mv /var/log/nginx/access.log /var/log/nginx/`date +%Y_%m_%d:%H:%M:%S`.access.log kill -USR1 $PID --------------------------------------------------------------------------------------- [root@localhost sbin]# chmod 755 log_cutting.sh [root@localhost sbin]# ./log_cutting.sh [root@localhost sbin]# ls log_cutting.sh nginx [root@localhost sbin]# cd /var/log/nginx/ [root@localhost nginx]# ls 2019_01_04:05:44:39.access.log 2019_01_04:05:44:39.error.log access.log error.log
三:nginx 主配置文件:nginx.conf
3.1:默認配置:配置文件默認保存在path/conf當中,默認的配置文件爲nginx.conf,如下是編譯安裝後的默認配置:
[root@localhost conf]# cat nginx.conf #全局生效,主要設置nginx的啓動用戶/組,啓動的工做進程數量,nginx PID 路徑,日誌路徑等。 #user nobody; worker_processes 1; #默認啓動一個工做進程 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { #events模塊,主要影響nginx服務器與用戶網絡鏈接,好比是夠容許同時接受多個網絡鏈接,使用哪一種事件驅動模型處理請求,每一個工做進程能夠同時支持的最大鏈接數,是否開啓對多工做進程下的網絡鏈接進行序列化等。 worker_connections 1024; #設置單個工做進程能夠接受的最大併發數 } http { #http模塊是nginx 服務器配置中的重要部分,緩存,代理,和日誌格式定義等絕大多數功能和第三方模塊均可以在這設置,http 快能夠包含多個server塊,而一個server 快中又能夠包含多個location 塊,server 塊能夠配置文件引入,MIME-Type 定義,日誌自定義,是否用sendfile 鏈接超時時間和單個鏈接的請求上限等. include 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 logs/access.log main; sendfile on; #是否調用sendfile 函數(zero copy --> 零 copy方式)來輸出文件,普通應用打開,能夠大幅度提高nginx 的讀文件性能,若是文件是下載就須要關閉, #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #長鏈接超時時間,單位是秒 #gzip on; server { #設置一個虛擬主機,能夠包含本身的全局塊,同時也包含多個 locating 模塊,好比本虛擬機監聽的端口,本虛擬機的名稱和ip 配置,多個server 可使用一個端口,必須都使用80 端口提供web 服務. listen 80; #server 全局配置,配置監聽的端口 server_name localhost; #本server 名稱,當訪問此名稱的時候nginx 會調用當前server 內部的配置程序進行匹配. #charset koi8-r; #access_log logs/host.access.log main; location / { #location 實際上是server的一個指令,爲nginx 服務器提供比較多並且靈活的指令,都在location 中體現的,主要基於nginx 接收到的請求字符串,並對用戶的請求URI進行匹配,並對特定的指令進行處理,包括地址重定向,數據緩存和應答控制等功能都是在這部分實現,另外不少第三方模塊的配置也是在location 模塊中配置。 root 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 { #location 處理對應的不一樣錯誤代碼的頁面定義到/50x.html ,這個跟對應其server 中定義的目錄下, root 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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
3.2:配置nginx 主進程的啓動用戶和工做進程數:
user xxxx; #每一條指令都要以分號結尾 worker_processes 1; #能夠指定啓動的固定nginx進程數,或使用auto,auto是啓動與當前CPU 線程相同的進程數,如CPU是四核八線程的就啓動八個進程的Nginx工做進程。
3.3:綁定Nginx 工做進程到不一樣的CPU上:
默認Nginx是不進行保定的,綁定並不能是當前nginx進程獨佔以一核心CPU,可是能夠保證此進程不會運行在其餘核心上,這就極大減小了nginx 工做進程在不一樣cpu上的跳轉,減小了CPU對進程的資源分配與回收,所以能夠有效的提高nginx服務器的性能,配置以下:
[root@Server1 nginx]# grep process /proc/cpuinfo | wc -l #確認CPU的核心數量 4 四個線程CPU的配置: worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; 八個線程CPU的配置: worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
3.4:PID和錯誤日誌文件路徑:
#pid logs/nginx.pid; #能夠指定絕對路徑 #error_log logs/error.log; #指定錯誤日誌路徑 #error_log logs/error.log notice; #指定一個日誌記錄級別 #error_log logs/error.log info; 支持的日誌級別: 語法: error_log file [ debug | info | notice | warn | error | crit ] | [{ debug_core | debug_alloc | debug_mutex | debug_event | debug_http | debug_mail | debug_mysql } ] 日誌級別 = 錯誤日誌級別 | 調試日誌級別; 或者 日誌級別 = 錯誤日誌級別; 錯誤日誌的級別: emerg, alert, crit, error, warn, notic, info, debug, 調試日誌的級別: debug_core, debug_alloc, debug_mutex, debug_event, debug_http, debug_mail, debug_mysql error_log 指令的日誌級別配置分爲錯誤日誌級別和調試日誌級別,錯誤日誌只能設置一個級別,並且錯誤日誌必須書寫在調試日誌級別的前面,另外調試日誌能夠設置多個級別,其餘配置方法可能沒法知足需求。
3.5:配置文件的引入:include
#include 可以優化nginx.cnf 配置文件,使得配置文件條理有序
include file; #file是要導入的文件,支持相對路徑,通常在html目錄裏面 導入一個conf文件,並配置不一樣主機名的頁面,編輯nginx.conf主配置文件: include /usr/local/nginx/conf.d/samsung.conf; #在最後一個大括號裏面加入一項,*是導入任何以conf結尾的配置文件 在/usr/local/nginx/conf.d/建立一個test.conf,內容以下: [root@Server1 nginx]# grep -v "#" conf.d/test.conf | grep -v "^$" server { listen 8080; server_name test.china.cn; location / { root html; index index1.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
http { #include 命令添加在http 模塊中
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
include /usr/local/nginx/nginx.d/test.conf;
#gzip on;