Nginx文檔地址:http://nginx.org/en/docs/
Nginx中文文檔地址:https://www.nginx.cn/doc/
一、Nginx簡介與安裝
1、簡介
Nginx是一個高性能WEB服務器,除它之外Apache、Tomcat、Jetty、IIS,它們都是Web服務器,或者叫做WWW(World Wide Web)服務器,相應地也都具備Web服務器的基本功能;
2、編譯與安裝
2.1 環境的准備
(1)linux 內核2.6及以上版本;只有2.6之后才支持epool,在此之前使用select或pool多路復用的IO模型,無法解決高並發壓力的問題。通過命令uname -a 即可查看linux 內核。
(2)GCC編譯器: GCC(GNU Compiler Collection)可用來編譯C語言程序。Nginx不會直接提供二進制可執行程序,只能下載源碼進行編譯。
(3)PCRE庫:PCRE(Perl Compatible Regular Expressions,Perl兼容正則表達式)是由Philip Hazel開發的函數庫,目前為很多軟件所使用,該庫支持正則表達式。
(4)zlib庫:zlib庫用於對HTTP包的內容做gzip格式的壓縮,如果我們在nginx.conf里配置了gzip on,並指定對於某些類型(content-type)的HTTP響應使用gzip來進行壓縮以減少網絡傳輸量。
(5)OpenSSL開發庫:如果我們的服務器不只是要支持HTTP,還需要在更安全的SSL協議上傳輸HTTP,那么就需要擁有OpenSSL了。另外,如果我們想使用MD5、SHA1等散列函數,那么也需要安裝它。
上面幾個庫都是Nginx 基礎功能所必需的,為簡單起見我們可以通過yum 命令統一安裝;
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
2.2 安裝
(1)下載並解壓:下載nginx 最新穩定版本;(http://nginx.org/en/download.html )
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz #解壓
(2)將臨時文件目錄指定為/var/temp/nginx,需要在/var下創建temp及nginx目錄;
mkdir -p /var/temp/nginx
(3)進入到解壓的目錄,使用cofigure命令創建一個makeFile文件
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-file-aio \ --with-http_realip_module
說明:--prefix=/usr/local/nginx 表示軟件安裝到/usr/local/nginx下面。
(4)編譯和安裝
make & make install
(5)進入安裝位置 /usr/local/nginx 查看目錄結構下有 html、sbin、conf 目錄;
其中html是里面首頁html文件。conf里面是配置文件。sbin里面只執行文件。
(6)啟動nginx
進入sbin目錄,執行命令;
./nginx
(7) 查看 nginx 的啟動
ps -aux | grep nginx
2.3 控制命令
(1)查看命令幫助
./nginx -?
(2)啟動方式
./nginx #默認方式啟動 ./nginx -c /tmp/nginx.conf #指定配置文件啟動 ./nginx -p /usr/local/nginx/ #指定nginx程序目錄啟動
(3)停止方式
./nginx -s stop #強制停止 ./nginx -s quit #優雅停止
(4)檢查一下配置文件
./nginx -t
(5)重新加載配置文件
./nginx -s reload
(6)重新打開日志文件
./nginx -s reopen
(7)設置全局命令,如下表示設置啟動用戶為root
./nginx -g "user root;"
二、架構說明
Nginx啟動后,會產生一個主進程,主進程執行一系列的工作后會產生一個或者多個工作進程;(CPU為幾核就設置幾個工作進程,效率最高);使用的是非阻塞的模型;用戶通信是和 Master 進程去通信,然后交由工作進程去處理;
架構說明
(1)nginx啟動時,會生不處理網絡請求,主要負責調度工作進程,也就是圖示的三項:加載配置、啟動工作進程及非停升級。所以,nginx啟動以后,查看操作系統的進程列表,我們就能看到至少有兩個nginx進程。
(2)服務器實際處理網絡請求及響應的是工作進程(worker),在類unix系統上,nginx可以配置多個worker,而每個worker進程都可以同時處理數以千計的網絡請求。
(3)模塊化設計。nginx的worker,包括核心和功能性模塊,核心模塊負責維持一個運行循環(run-loop),執行網絡請求處理的不同階段的模塊功能,如網絡讀寫、存儲讀寫、內容傳輸、外出過濾,以及將請求發往上游服務器等。而其代碼的模塊化設計,也使得我們可以根據需要對功能模塊進行適當的選擇和修改,編譯成具有特定功能的服務器。
(4)事件驅動、異步及非阻塞,可以說是nginx得以獲得高並發、高性能的關鍵因素,同時也得益於對Linux、Solaris及類BSD等操作系統內核中事件通知及I/O性能增強功能的采用,如kqueue、epoll及event ports。
Nginx大量使用多路復用和事件通知,並且給不同的進程分配不同的任務。數量有限的工作進程(Worker)使用高效的單線程循環處理連接。每個worker進程每秒可以處理數千個並發連接、請求。
Nginx模塊可分為:核心、事件模塊,階段處理器,協議、變量處理器,過濾器,上游和負載均衡器等。
三、Nginx的配置和使用
1、配置文件的語法格式
配置塊:名稱開頭用大口號包裹其對應屬性;
屬性:基於空格切分屬性名與屬性值,屬性值可能有多個項,以空格進行切分, 如:access_log logs/host.access.log main
參數:其配置在塊名稱與大括號之間,其值如果有多個也是通過空格進行拆;
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;
# 配置一個具體的站點 server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /nginx_status { stub_status on; access_log off; } }
server {
listen 80;
server_name www.baidu.com www.qq.com *.baidu.com www.baidu.*;
}
}
上面配置中的 events、http、server、location、upstream等屬於配置項塊。而worker_processes 、worker_connections、include、listen 屬於配置項塊中的屬性,一個屬性名可以有多個屬性值,中間用空格分隔。 /nginx_status 屬於配置塊的特定參數。其中server塊嵌套於http塊,其可以直接繼承訪問Http塊當中的參數。
http 語句塊只能有一個,http 中可以有多個 server (注意server和大括號之間有一個空格), server_name 配置的域名;
listen 一樣,按照 server_name去匹配:匹配的優先級一樣,哪個在前就先匹配哪個;若都沒有匹配到,就按照最前面的匹配;
location為路徑;
Nginx的實踐
(1)nginx的配置文件,如下:
worker_processes 1; pid /usr/local/nginx/logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #gzip on;
server { listen 80; server_name www.yufeng.com; root /usr1/nginx/yufeng/; location / { index index.html index.htm; } location /old { alias /usr1/nginx/old_yufeng/; index index.html; } location = /old/baidu { proxy_pass http://www.baidu.com/;
} location ~ \.(png|jpg|gif|avi|mp4|css|js)$ { root /usr1/nginx/yufeng/static; } } server { listen 80; server_name 192.168.172.20; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
(2)目錄及文件的實戰
a. 創建/usr1/nginx/yufeng/index.html文件,內容為;Hello, yufeng.com!!
b. 創建 /usr1/nginx/yufeng/user/user.html 文件,內容為:Hello,User!
c. 創建 /usr1/nginx/old_yufeng/index.html文件,內容為;Hello, old -- yufeng.com!!
d. 創建 /usr1/nginx/old_yufeng/user/user.html文件,內容為;Hello,OLd -- User!
root:站點根目錄,絕對匹配,location下的root優先級高於server下的優先級;
alias:站點別名,可以去除location中對應的前綴,只能配置在location下面;
2、location
語法:location [=|~|~*|^~|@]/uri/{……}
配置塊 server
- =表示把URI作為字符串,以便與參數中的uri做完全匹配。(優先級最高)
- ~表示正則匹配URI時是字母大小寫敏感的。
- / 基於uri目錄匹配
- ~表示正則匹配URI時是字母大小寫敏感的。
- ~*表示正則匹配URI時忽略字母大小寫問題。
- ^~表示正則匹配URI時只需要其前半部分與uri參數匹配即可。
(1) = 示例:
http { server { listen 80; server_name www.yufeng.com; root /usr1/nginx/yufeng/; location / { index index.html index.htm; } location /old { alias /usr1/nginx/old_yufeng/; index index.html; } location = /old/baidu { proxy_pass http://www.baidu.com/; } } }
(2)~ 正則匹配(大小寫敏感)
在nginx服務器上創建:/usr1/nginx/yufeng/static/st.css 文件;
基於正則動靜分離的匹配
location ~ \.(png|jpg|gif|avi|mp4|css|js)$ { root /usr1/nginx/yufeng/static; }
匹配優先級: = > 正則匹配 > 前綴最大匹配 > 配置靠前匹配
(3)防盜鏈配置演示
location ~ \.(png|jpg|gif|avi|mp4|css|js)$ { root /usr1/nginx/yufeng/static; #防盜鏈的配置(只允許*.yufeng.com的域名訪問) valid_referers none blocked *.yufeng.com; if ($invalid_referer) { return 403; }
}
(4)下載限速
location /download { limit_rate 1m; //限制每S下載速度 limit_rate_after 30m; // 超過30 之 后在下載 }
(5)創建 IP 黑名單
#封禁指定IP deny 192.168.0.1; allow 192.168.0.1;
#開放指定IP 段 allow 192.168.0.0/24;
#封禁所有 deny all;
#開放所有 allow all;
# 創建黑名單文件 echo 'deny 192.168.0.132;' >> balck.ip
#http 配置塊中引入 黑名單文件 include black.ip;
(6)日志格式配置(worker進程要有權限)
nginx 的變量:http://nginx.org/en/docs/varindex.html
日志格式: 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; #基於域名打印日志 access_log logs/$host.access.log main;
error日志的設置
語法:error_log /path/file level; 默認:error_log logs/error.log error; level是日志的輸出級別,取值范圍是debug、info、notice、warn、error、crit、alert、emerg;
針對指定的客戶端輸出debug級別的日志
語法:debug_connection[IP|CIDR] events { debug_connection 192.168.0.147; debug_connection 10.224.57.0/200; }
注意:debug 日志開啟 必須在nginx安裝時 添加 --with-debug (允許debug)