1. 能夠實現高併發訪問處理,消耗資源小html
2. 軟件知識功能不少(web服務功能 反向代理功能 緩存功能)linux
3. 利用異步網絡IO模型,實現快速處理用戶請求(epoll)nginx
1)下載解壓軟件(nginx.org)web
2)安裝依賴軟件shell
openssl-devel pcre-develapache
3)建立出一個worker進程管理用戶vim
4)進行nginx軟件編譯安裝windows
a 進行軟件配置 ./configurecentos
b 進行軟件編譯 make瀏覽器
c 進行軟件編譯安裝 make install
5)建立程序目錄軟連接
6)啓動nginx服務,利用curl命令或瀏覽器進行訪問測試
conf --- 軟件配置文件保存目錄
html --- 軟件服務站點目錄
logs --- 軟件服務日誌默認保存目錄
sbin --- 軟件服務管理命令服務(nginx -t -s reload stop)
規範一:大括號要成對出現
規範二:每行信息結尾要注意有分號 ;
規範三:相應參數指令只能放置在指定區塊
所謂虛擬主機,在Web服務裏就是一個獨立的網站站點,這個站點對應獨立的域名(也多是IP或端口), 虛擬主機配置上有三種類型
[root@web01 conf]# vim nginx.conf 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 www.wuhuang.com; root html/www; index index.html index.htm; } server { listen 80; server_name bbs.wuhuang.com; root html/bbs; index index.html index.htm; } server { listen 80; server_name blog.wuhuang.com; root html/blog; index index.html index.htm; } } |
mkdir /application/nginx/html/{www,bbs,blog} -p
for name in www bbs blog;do echo "10.0.0.7 web01 $name" >/application/nginx/html/$name/index.html;done
[root@web01 conf]# mkdir /application/nginx/html/{www,bbs,blog} -p [root@web01 conf]# for name in www bbs blog;do echo "10.0.0.7 web01 $name" >/application/nginx/html/$name/index.html;done [root@web01 conf]# ll ../html/ total 20 -rw-r--r-- 1 root root 537 Feb 5 19:53 50x.html drwxr-xr-x 2 root root 4096 Feb 5 20:03 bbs drwxr-xr-x 2 root root 4096 Feb 5 20:03 blog -rw-r--r-- 1 root root 612 Feb 5 19:53 index.html drwxr-xr-x 2 root root 4096 Feb 5 20:03 www |
[root@web01 conf]# for name in www bbs blog;do cat /application/nginx/html/$name/index.html;done 10.0.0.7 web01 www 10.0.0.7 web01 bbs 10.0.0.7 web01 blog |
[root@web01 conf]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful [root@web01 conf]# /application/nginx/sbin/nginx -s reload [root@web01 conf]# netstat -lntup |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8337/nginx |
須要對虛擬主機域名進行解析(編寫hosts文件-linux裏面hosts文件 windows裏面hosts)
[root@web01 conf]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.5 lb01 172.16.1.6 lb02 172.16.1.7 web01 www.wuhuang.com bbs.wuhuang.com blog.wuhuang.com |
[root@web01 conf]# for name in www bbs blog;do curl $name.wuhuang.com;sleep 1;done 10.0.0.7 web01 www 10.0.0.7 web01 bbs 10.0.0.7 web01 blog |
當客戶端訪問nginx服務端,返回的狀態碼信息爲304時,表示進行讀取緩存處理
[root@web01 conf]# vim nginx.conf 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 www.wuhuang.com; root html/www; index index.html index.htm; } server { listen 81; --- 將默認80端口號改成81 server_name bbs.wuhuang.com; root html/bbs; index index.html index.htm; } server { listen 80; server_name blog.wuhuang.com; root html/blog; index index.html index.htm; } } |
[root@web01 conf]# vim /etc/profile [root@web01 conf]# tail -1 /etc/profile export PATH=$PATH:/application/nginx/sbin/ |
[root@web01 conf]# nginx -t nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful [root@web01 conf]# nginx -s reload |
[root@web01 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 10.0.0.7:80; server_name www.wuhuang.comg; root html/www; index index.html index.htm; } # server { # listen 81; # server_name bbs.wuhuang.com; # root html/bbs; # index index.html index.htm; # } # server { # listen 80; # server_name blog.wuhuang.com; # root html/blog; # index index.html index.htm; # } } |
強調說明(*****):當nginx配置文件中,涉及到IP地址信息改動,都須要重啓nginx服務,
不能採用平滑重啓,不然配置不生效
[root@web01 conf]# nginx -s stop [root@web01 conf]# nginx [root@web01 conf]# netstat -lntup|grep nginx tcp 0 0 10.0.0.7:80 0.0.0.0:* LISTEN 8440/nginx |
[root@web01 conf]# curl 10.0.0.7 10.0.0.7 web01 www [root@web01 conf]# curl 172.16.1.7 curl: (7) couldn't connect to host |
[root@web01 conf]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.7 web01 bbs.wuhuang.com blog.wuhuang.com 10.0.0.7 www.wuhuang.com 注意:同一個IP地址能夠對應多個域名,但一個域名只能對應一個IP地址 |
[root@web01 conf]# curl www.wuhuang.com 10.0.0.7 web01 www |
[root@web01 conf]# vim nginx.conf 13 root html/www; 14 index index.html index.htm; 15 } 16 server { 17 listen 80; 18 server_name bbs.wuhuang.com; 19 root html/bbs; 20 index index.html index.htm; 21 } 22 server { 23 listen 80; 24 server_name blog.wuhuang.com; 25 root html/blog; 26 index index.html index.htm; 27 } 28 } |
[root@web01 conf]# mkdir extra |
[root@web01 conf]# sed -n '10,15p' nginx.conf >extra/www.conf [root@web01 conf]# sed -n '16,21p' nginx.conf >extra/bbs.conf [root@web01 conf]# sed -n '22,27p' nginx.conf >extra/blog.conf [root@web01 conf]# cat extra/blog.conf server { listen 80; server_name blog.wuhuang.com; root html/blog; index index.html index.htm; } |
[root@web01 conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; } |
[root@web01 html]# nginx -t nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful [root@web01 html]# nginx -s reload [root@web01 html]# for name in www bbs blog;do curl $name.wuhuang.com;sleep 1;done 10.0.0.7 web01 www 10.0.0.7 web01 bbs 10.0.0.7 web01 blog |
[root@web01 extra]# cat bbs.conf server { listen 80; server_name bbs.wuhuang.com bbs.com; root html/bbs; index index.html index.htm; } 說明:添加別名信息須要設置到hosts解析文件中 [root@web01 extra]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.5 lb01 172.16.1.6 lb02 172.16.1.7 web01 www.wuhuang.com bbs.wuhuang.com blog.wuhuang.com bbs.com |
驗證
[root@web01 extra]# nginx -t nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful [root@web01 extra]# nginx -s reload [root@web01 extra]# curl bbs.com 10.0.0.7 web01 bbs |
爲何在編譯安裝時,須要配置狀態模塊?
能夠查看nginx運行狀態信息。
官方參考連接:http://nginx.org/en/docs/http/ngx_http_stub_status_module.html#stub_status
[root@web01 conf]# cat extra/state.conf server{ listen 80; server_name state.wuhuang.com; location / { stub_status on; access_log off; } } |
[root@web01 conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; include extra/state.conf; } |
說明:以上信息配置,將狀態模塊域名配置到windows系統的hosts文件中和/etc/hosts中。
[root@web01 conf]# nginx -t nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful [root@web01 conf]# nginx -s reload [root@web01 conf]# curl state.wuhuang.com Active connections: 1 server accepts handled requests 9 9 9 Reading: 0 Writing: 1 Waiting: 0 |
狀態模塊說明: Active connections: 1 ——當前客戶端的鏈接數量(包含waiting鏈接數) server accepts handled requests ——accepts:接收客戶端鏈接的總數(只接受http協議信息) 9 9 9 —— handled:處理鏈接的總數(處理的是accepts接收的數據) —— requests:客戶端請求的總數(包括TCP創建接連) Reading: 0 Writing: 1 Waiting: 0 —— Reading:監控請求頭的鏈接數 —— writing:監控迴應客戶端的鏈接數 ——waiting:監控空閒客戶端的鏈接請求等待數 |
error_log file level
關鍵字 日誌文件 錯誤日誌級別
日誌信息錯誤級別分爲8種:
0 EMERG(緊急):會致使主機系統不可用的狀況
1 ALERT(警告):必須立刻採起措施解決的問題
2 CRIT(嚴重):比較嚴重的狀況
3 ERR(錯誤):運行出現錯誤
4 WARNING(提醒):可能會影響系統功能的事件
5 NOTICE(注意):不會影響系統但值得注意
6 INFO(信息):通常信息
7 DEBUG(調試):程序或系統調試信息等
error_log的默認值爲:
Default: error_log logs/error.log error;
能夠放置的標籤段爲:
Context: main, http, mail, stream, server, location
[root@web01 data]# cat /application/nginx/conf/nginx.conf worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; include extra/state.conf; } |
[root@web01 data]# cat /application/nginx/conf/nginx.conf worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } 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; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; include extra/state.conf; } |
[root@web01 extra]# cat bbs.conf server { listen 80; server_name bbs.wuhuang.com; location / { access_log on; } root html/bbs; index index.html index.htm; } |
Nginx記錄日誌的默認參數配置:
access_log logs/access.log main; --- 調用定義格式信息,生成訪問日誌
$remote_addr 10.0.0.1 --- 訪問客戶端的源地址信息
$remote_user - --- 訪問客戶端認證用戶信息
[$time_local] --- 顯示訪問時間
$request GET / HTTP/1.1 --- 請求行信息
$status 304 --- 狀態碼信息(304狀態碼利用緩存顯示頁面信息)
$body_bytes_sent --- 服務端響應客戶端的數據大小信息
$http_referer --- 記錄連接到網站的域名信息
$http_user_agent --- 用戶訪問網站客戶端軟件標識信息(例:瀏覽器、手機客戶端)
用戶利用客戶端瀏覽器測試訪問時,win10默認瀏覽器會有異常問
$http_x_forwarded_for --- 反向代理
官方連接:http://nginx.org/en/docs/http/ngx_http_log_module.html #access_log
[root@web01 scripts]# cat cut_log.sh #!/bin/bash
data_info=$(date +%F-%H:%M)
mv /application/nginx/logs/access.log /application/nginx/logs/access.log.$data_info /application/nginx/sbin/nginx -s reload
# cut nginx log cron * */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null |
寫入定時任務 # cut nginx log cron * */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null |
做用:logrotate的配置文件是/etc/logrotate.conf,一般不須要對它進行修改。日誌文件的輪循設置在獨立的 配置文件中,它們放在/etc/logrotate.d/目錄下。
logrotate全局配置參數說明:
vim /etc/logrotate.conf
weekly ###日誌文件將按月輪循
rotate 4 ###一次將最多存儲4個歸檔日誌
create ###在生成輪詢日誌後,會自動建立一個新的文件
dateext ###生成的輪詢日誌後面加上時間格式
compress ###生成的輪詢日誌是否壓縮,默認被註釋
include /etc/logrotate.d ###存放日誌文件的輪詢配置
vim /etc/logrotate.d/log-file /var/log/log-file { monthly -rotate 5 compress delaycompress missingok create 644 root root postrotate /usr/bin/killall -HUP rsyslogd endscript } |
搭建好一臺nginx的web服務器。配置好內網卡地址與外網卡地址
web服務的網站域名爲www.etiantian.org,站點目錄爲html/www
要求內網用戶能夠訪問網站http://www.etiantian.org/AV資源信息
要求外網用戶禁止訪問網站http://www.etiantian.org/AV資源信息
location / { allow 172.16.1.0/24; ——容許172.16.1.0網段訪問 deny all; ——拒絕全部訪問 } |
定位/AV資源不能隨意訪問 location 進行資源定位 ——至關於if 判斷,知足什麼作什麼 格式: location /AV {
} |
建立測試站點目錄 mkdir /application/nginx/html/www/AV |
curl -H host:www.wuhuang.com 10.0.0.7/lalala.html --- 表示指定訪問nginx服務端哪個虛擬主機 |
[root@nfs01 ~]# curl -H host:www..wuhuang.com 172.16.1.7/AV/lalala.html wuhuang01 ——內網能夠訪問 |
[root@nfs01 ~]# curl -H host:www.etiantian.org 10.0.0.7/AV//lalala.html <html> <head><title>403 Forbidden</title></head> ——403拒絕訪問 <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> |
Syntax: location [ = | ~ | ~*|^~ ] uri { ... } ——uri就是域名後面的信息
-eq 等於
-ne 不等於
-le 小於等於
-lt 小於
-ge 大於等於
-gt 大於
= ——精確匹配(不要多餘的) 1
~ ——區分大小寫匹配 3
~* ——不區分大小寫匹配(grep -i) 3
^~ ——優先匹配(優先級) 2
/AV ——指定要匹配的目錄資源 3
/ ——指定匹配站點目錄 默認匹配
! ——表示取反匹配
第一步:編寫測試配置文件
[root@web01 extra]# cat www.conf server { listen 80; server_name www.wuhuang.com; root html/www; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } access_log logs/access_www.log main; } |
第二步:根據返回狀態碼,確認優先級
總結取狀態碼方法:
1)curl -I www.wuhuang.com/wu1/ 2>/dev/null|awk 'NR==1{print $2}'
2)curl -I www.wuhuang.com/wu1/ -s|awk 'NR==1{print $2}'
3)curl -s -I www.wuhuang.com/wu1/ -w "%{http_code}\n" -o /dev/null
做用:編譯之後進行網站運行狀態監控
總結curl命令參數:
-v --- 顯示用戶訪問網站詳細報文信息(請求報文 響應報文)
-I --- 顯示響應報文起始行和響應頭部信息
-H host: --- 修改請求報文host字段信息
-L --- 進行訪問跳轉追蹤
-u user:password --- 指定認證用戶信息
-o --- 將輸出內容能夠指定到空
-w --- 指定須要輸出顯示的信息
-s --- 不顯示錯誤輸出,將錯誤信息追加到空
1. 將地址信息進行重寫,實現域名地址信息跳轉
2. 用於作僞靜態
rewrite應用標籤:server、location、if
如何實現相似百度重寫域名的功能?
baidu.com ===> www.baidu.com
etiantian.org ===> bbs.etiantian.org
[root@web01 extra]# cat bbs.conf server { listen 80; server_name bbs.wuhuang.com bbs.com; rewrite ^/(.*) http://bbs.wuhaung.com/$1 permanent; root html/bbs; index index.html index.htm; } [root@web01 extra]# curl -L etiantian.org --- 進行訪問跳轉追蹤 curl: (47) Maximum (50) redirects followed [root@web01 extra]# curl -Lv etiantian.org --- 顯示無限循環過程 說明:以上配置進入了無限循環狀態 |
[root@web01 extra]# cat bbs.conf server { listen 80; server_name etiantian.org; rewrite ^/(.*) http://bbs.wuhuang.com/$1 permanent; } server { listen 80; server_name bbs.wuhuang.com bbs.com; root html/bbs; index index.html index.htm; } |
說明:以上狀態不會循環,跳轉成bbs,後再次從新匹配時,是以bbs的域名查找,這時候就不匹配第一個server_name了,直接進入下一個server區塊 |
[root@web01 extra]# cat bbs.conf server { listen 80; server_name bbs.wuhuang.com bbs.com; if ($host ~* "^wuhuang.com$") { ——if這裏至關於location rewrite ^/(.*) http://bbs.wuhuang.com/$1 permanent; } root html/bbs; index index.html index.htm; } |
說明:$host就是主機頭信息,~*:不區分大小寫,後面的是以wuhuang開頭,com結尾。通過rewite就跳轉爲bbs, 而後在從新訪問,這個時候就不知足if語句的條件了,那麼就繼續讀取下一個location |
Ø 能夠調整用戶瀏覽的URL,使其看起來更規範,合乎開發及產品人員的需求。
Ø 爲了讓搜索引擎收錄網站內容,並讓用戶體驗更好,企業會將動態URL地址假裝成靜態地址提供服務。
Ø 網站換新域名後,讓舊域名的訪問跳轉到新的域名上,例如:讓京東的360buy換成了jd.com。
Ø 根據特殊變量、目錄、客戶端的信息進行URL跳轉等。
auth_basic "wuhuang training";
auth_basic_user_file /application/nginx/conf/htpasswd;
[root@web01 extra]# cat bbs.conf server { listen 80; server_name bbs.wuhuang.com bbs.com; auth_basic "wuhuang training"; auth_basic_user_file /application/nginx/conf/htpasswd; if ($host ~* "^wuhuang.com$") { rewrite ^/(.*) http://bbs.wuhuang.com/$1 permanent; } root html/bbs; index index.html index.htm; } |
利用htpasswd命令生成密文密碼信息
rpm -qf `which htpasswd` httpd-tools-2.2.15-59.el6.centos.x86_64 ——安裝apache(須要用其中的htpasswd命令) htpasswd -bc /application/nginx/conf/htpasswd wuhuang 123456 ---生成認證文件 chmod 400 /application/nginx/conf/htpasswd chown www.www /application/nginx/conf/htpasswd |
說明: -c #建立一個新的密碼文件 -b #採用免交互的方式輸入用戶的密碼信 |
1. 401 Authorization Required --- 要求用戶進行認證 2. 500 --- worker進程沒法讀取用戶請求的問題,權限問題 |
[root@web01 extra]# curl bbs.wuhuang.com -u wuhuang Enter host password for user 'wuhuang': 10.0.0.7 web01 bbs [root@web01 extra]# curl bbs.etiantian.org -u wuhuang:123456 10.0.0.7 web01 bbs |
-v --- 顯示用戶訪問網站詳細報文信息(請求報文 響應報文)
-I --- 顯示響應報文起始行和響應頭部信息
-H host: --- 修改請求報文host字段信息
-L --- 進行訪問跳轉追蹤
-u user:password --- 指定認證用戶信息
-o --- 將輸出內容能夠指定到空
-w --- 指定須要輸出顯示的信息
-s --- 不顯示錯誤輸出,將錯誤信息追加到空