一、Nginx簡介
Nginx(engine X)是一個高性能的HTTP和反向代理服務器,Nginx是模塊化設計、較好的擴展性、高可靠、低內存消耗、支持熱部署、支持事件驅動、AIO、mmap等特性。
二、Nginx功能
從功能上來Nginx分爲基本功能 和擴展功能:
基本功能:
(1)靜態資源的Web服務器,能緩存打開的文件描述符;
(2)http、smtp、pop3協議的反向代理服務器;
(3)緩存加速、負載均衡;
(4)支持FastCGI(fpm ,LNMP)、uWSGI(python)等;
(5)模塊化(非DSO機制)、過濾器zip、SSI及圖像的大小調整;
(6)支持SSL;
擴展功能:
(1)基本名稱和IP的虛擬主機;
(2)支持Keepalive
(3)支持平滑升級
(4)定製訪問日誌、支持使用日誌緩存區提供日誌存儲性能;
(5)支持url rewrite
(6)支持路徑別名
(7)支持基本IP及用戶的訪問控制
(8)支持速率限制、支持併發數限制
三、Nginx工作模式
Nginx是非阻塞、事件驅動、由一個master進程,生成一個或多個worker進行;每個worker響應多個請求;
Nginx由衆多的模塊組成分別有核心模塊、Standard HTTP Module、Options HTTP Module、Mail Module、3rd party modules;
四、Nginx安裝
Nginx支持編譯安裝和製作好的程序包兩種方式安裝。
4.1、rpm包安裝
rpm包安裝配置好epel源以後可以直接用yum進行安裝即可
4.2、編譯安裝
1)首先安裝編譯環境
yum groupinstall "Developmenttools" "Server Platform Development" -y
2)下載nginx程序包
[root@node-3 ~]# mkdir source [root@node-3 ~]# cd source/ [root@node-3 source]# wget http://nginx.org/download/nginx-1.6.3.tar.gz --2015-05-24 20:18:24-- http://nginx.org/download/nginx-1.6.3.tar.gz Resolving nginx.org... 206.251.255.63 Connecting tonginx.org|206.251.255.63|:80... connected. HTTP request sent, awaiting response... 200OK Length: 805253 (786K)[application/octet-stream] Saving to: ?.ginx-1.6.3.tar.gz? 100%[================================================================>]805,253 71.0K/s in 12s 2015-05-24 20:18:37 (66.1 KB/s) -?.ginx-1.6.3.tar.gz?.saved [805253/805253]
3)編譯安裝
[root@node-3 source]# tar xfnginx-1.6.3.tar.gz [root@node-3 source]# cd nginx-1.6.3 [root@node-3 nginx-1.6.3]# useradd -r nginx [root@node-3 nginx-1.6.3]# ./configure --prefix=/usr/local/nginx--conf-path=/etc/nginx/nginx.conf \ --user=nginx --group=nginx--error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock--with-http_ssl_module --with-http_stub_status_module \ --with-http_gzip_static_module--with-http_flv_module --with-http_mp4_module \ --http-client-body-temp-path=/var/tmp/nginx/client--http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi [root@node-3 nginx-1.6.3]# make &&make install [root@node-3 nginx-1.6.3]# mkdir -pv/var/tmp/nginx/{client,proxy,fastcgi,uwsgi} 安裝選項解釋: --prefix=/usr/local/nginx #Nginx安裝路徑。如果沒有指定,默認爲 /usr/local/nginx --conf-path=/etc/nginx/nginx.conf #在沒有給定-c選項下默認的nginx.conf的路徑。如果沒有指定,默認爲<prefix>/conf/nginx.conf。 --user=nginx #在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的用戶。如果沒有指定,默認爲 nobody。 --group=nginx #在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的組。如果沒有指定,默認爲 nobody。 --error-log-path=/var/log/nginx/error.log#nginx.conf中沒有指定error_log指令的情況下,默認的錯誤日誌的路徑。如果沒有指定,默認爲 <prefix>/logs/error.log。 --http-log-path=/var/log/nginx/access.log #在nginx.conf中沒有指定access_log指令的情況下,默認的訪問日誌的路徑。如果沒有指定,默認爲<prefix>/logs/access.log。 --pid-path=/var/run/nginx/nginx.pid #在nginx.conf中沒有指定pid指令的情況下,默認的nginx.pid的路徑。如果沒有指定,默認爲<prefix>/logs/nginx.pid。 --lock-path=/var/lock/nginx.lock #nginx.lock文件的路徑。 --with-http_ssl_module #開啓HTTP SSL模塊,使NGINX可以支持HTTPS請求。這個模塊需要已經安裝了OPENSSL --with-http_stub_status_module #啓用 "server status" 頁 --with-http_gzip_static_module #用戶gzip壓縮 --with-http_flv_module #啓用 ngx_http_flv_module --with-http_mp4_module #啓用ngx_http_mp4_moduel --http-client-body-temp-path=/var/tmp/nginx/client#Set path to the http client request body temporary files --http-proxy-temp-path=/var/tmp/nginx/proxy#Set path to the http proxy temporary files --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi#Set path to the http fastcgi temporary files --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #set path to store http uwsgi temporary files
4)編譯報錯
checking for PCRE library in /usr/local/... not found checking for PCRE library in/usr/include/pcre/ ... not found checking for PCRE library in /usr/pkg/ ...not found checking for PCRE library in /opt/local/... not found ./configure: error: the HTTP rewrite modulerequires the PCRE library. You can either disable the module by using--without-http_rewrite_module option, or install the PCRE library intothe system, or build the PCRE library statically from the source with nginx byusing --with-pcre=<path> option.
解決方法:
[root@node-3 nginx-1.6.3]# yum installpcre-devel -y
五、Nginx配置解釋
[root@node-3 ~]# egrep -v"^$|^.*#" /etc/nginx/nginx.conf worker_processes 1; #Nginx工作的worker進行數 events { #定義event模式工作特性 worker_connections 1024; #設定單個worker進程所能夠處理的最大併發連接數數量,(最大連接=連接數x進程數) } http { #定義http協議相關的配置 include mime.types; default_type application/octet-stream; sendfile on; #開啓高效文件傳輸模式 keepalive_timeout 65; #長鏈接超時時間 server { #定義一個虛擬機 listen 80; #指定監聽的地址和端口 server_name localhost; #指定一個主機名 location / { #允許根據用戶請求的URI來匹配定義的各location:匹配到時,此請求將被相應的location配置塊中的配置所處理,例如做訪問控制等功能; root html; #設置資源路徑映射,用於指明請求的URL所對應的資源所在的文件系統上的起始路徑; index index.html index.htm; #指定默認主頁面 } error_page 500 502 503 504 /50x.html; #根據http響應碼來指明特用的錯誤頁面 location = /50x.html { root html; } } }
六、Nginx簡單配置
6.1啓動Nginx
[root@node-3 ~]#/usr/local/nginx/sbin/nginx [root@node-3 ~]# lsof -n -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 4735 root 6u IPv4 14326 0t0 TCP *:http (LISTEN) nginx 4736 nginx 6u IPv4 14326 0t0 TCP *:http (LISTEN)
在瀏覽器中輸入http://Server_ip
6.2新建一個虛擬主機
新建一個虛擬主機端口設置成8080,主機名www.wuday.com,站點文件存放在/web/wuday.com
6.2.1 nginx.conf配置
在http{}標籤中添加server{}標準;
[root@node-3 ~]# cd /etc/nginx/ [root@node-3 nginx]# vim nginx.conf server { listen 8080; server_name www.wuday.com; location / { root "/web/wuday"; index index.html; }
6.2.2提供站點文件
[root@node-3 ~]# mkdir /web/wuday -p [root@node-3 ~]# vim /web/wuday/index.html [root@node-3 ~]# cat /web/wuday/index.html <h1>www.wuday.com</h1>
6.2.3 檢查配置文件並重新加載配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.2.4 使用瀏覽器進行訪問
6.3 爲6.2提供一個錯誤優雅顯示頁面
6.3.1 配置nginx.conf文件
在nginx.conf文件中6.2的server{}標籤添加如下內容
error_page 500 502 503 504 =200/50x.html; location = /50x.html { root "/web/wuday/error"; } error_page 400 402 403 404=200 /40x.html; location = /40x.html { root "/web/wuday/error"; }
6.3.2 創建錯誤目標及頁面
[root@node-3 ~]# mkdir /web/wuday/error [root@node-3 ~]# cd /web/wuday/error [root@node-3 error]# cat 40x.html <h1>Server is Move...Plz mail [email protected]</h1> <h1>40x</h1> [root@node-3 error]# cat 50x.html <h1>Server is fle.....</h1> <h1>50x</h1>
6.3.3 檢查語法並重新加載配置文件
[root@node-3 ~]# /usr/local/nginx/sbin/nginx-t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.3.4 在瀏覽器中進行訪問測試
在http://Server_ip:8080後輸入一個不存在的頁面,可以看出狀態碼爲200,並不是403
6.4 設計站點基於IP的訪問控制
6.4.1 配置nginx.conf文件
站點不能讓172.16.0.0/16網段的用戶訪問,其它用戶都可以隨意的訪問。
在nginx.conf文件中6.2的server{}標籤添加如下內容
deny 172.16.0.0/16; allow all;
6.4.2 檢查語法並重新加載配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.4.4 在瀏覽器中進行訪問測試
在http://Server_ip:8080進行測試,根據測試來看爲什麼沒有被定位至錯誤頁面呢?
6.5 基本用戶的訪問控制
6.5.1 配置nginx.conf文件
註釋上面設置基於IP的訪問控制,並添加以下內容
#deny 172.16.0.0/16; #allow all; auth_basic "Plz Username and Password..."; auth_basic_user_file "/etc/nginx/user/.ngpasswd";
6.5.2 創建密碼文件
[root@node-3 ~]# mkdir /etc/nginx/user [root@node-3 ~]# htpasswd -c/etc/nginx/user/.ngpasswd wuday New password: Re-type new password: Adding password for user wuday
6.5.3檢查語法並重新加載配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.5.4在瀏覽器中進行訪問測試
在http://Server_ip:8080進行測試,提示輸入用戶名和密碼。輸入正確驗證通過後就可以訪問站點;
6.6 爲www.wuday.com提供https服務
6.6.1 配置nginx.conf
在nginx.conf文件註釋前面的用戶認證,在文件後添加如下內容:
server { listen 443 ssl; server_name localhost; ssl_certificate "/etc/nginx/ssl/httpd.crt"; ssl_certificate_key "/etc/nginx/ssl/httpd.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root "/web/wuday"; index index.html index.htm; } }
6.6.2 創建私鑰文件
這裏就不給出創建步驟,請參考我的相關博文。
6.6.3 修改本地的hosts文件
在本地電腦上的hosts文件中添加一行用於解析www.wuday.com
172.16.9.35 www.wuday.com
6.6.4檢查語法並重新加載配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.6.5在瀏覽器中進行訪問測試
在https://www.wuday.com進行測試,在沒有把根證書添加到受信任的根證書頒發機構時,會出現以下提示,點“繼續瀏覽此網站(不推薦)。”就能查看訪問站點了。
添加CA的證書到受信任的根證書頒發機構後,訪問的效果如下:
6.7 爲站點提供狀態頁
6.7.1 配置nginx.conf
站點的狀態面應該是隻能特定的用戶訪問。在server {}的8080端口的那個server {}標籤後面添加如下內容:
location /status { stub_statuson; allow172.16.0.0/16; denyall; }
6.7.2檢查語法並重新加載配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.7.3 在瀏覽器中進行訪問測試
在http://Server_ip:8080/status進行測試
狀態信息解析:
Active connections: 1 #當前所有處於打開狀態的連接數;
server accepts handled requests
85 85 236
#第一個數:已經接受的連接數;
#第二個數:已經處理過的連接數;
#第三個數:已經處理過的請求數,在“保持連接”模式下,請求數量可能會多於連接數量;
Reading: 0 Writing: 1 Waiting: 0
Reading:正處於接收請求狀態的連接數;
Writing:請求已經接收完成,正處於處理請求或發送響應的過程中的連接數;
Waiting:保護連接模式,且處於活動狀態的連接數;
6.8 搭建一個LNMP平臺
在Nginx上搭建一個LNMP平臺,對於php和MySQL的安裝這裏就不說明了,請參考我相關的博文。
6.8.1配置nginx.conf
在index中添加一個index.php的主頁,並放至前面。
index index.php index.html; location ~ \.php$ { root "/web/wp"; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
6.8.2 創建用於測試的index.php
<?php phpinfo(); ?>
6.8.3 檢查語法並重新加載配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.8.4 在瀏覽器中進行訪問測試
在http://Server_ip:8080/進行測試,只能出現以下測試頁面,說明php就沒有問題了。