第1章 常用的軟體
Apache :這是中小型Web服務的主流,Web伺服器中的老大哥,
Nginx :大型網站Web服務的主流,曾經Web伺服器中的初生牛犢,現已長大。
Nginx 的分支 Tengine ( http://tengine.taobao.org/)目前也在飛速發展•
Lighttpd :這是一個不溫不火的優秀 Web軟體,社群不活躍,靜态解析效率很高.在 Nginx 流行前,它是大并發靜态業務的首選,國内百度貼吧、豆瓣等衆多網站都有Lighttpd奮鬥的身彩"
PHP ( FastCGI ):大中小型網站都會使用,動态網頁語言PHP程式的解析容器。它可配合Apache解析動态程式,不過,這裡的PHP不是FastCGI守護程序橫式,而是mod_php5.so ( module)也可配合Nginx解析動态程式,此時的PHP常用FastCGI守護程序模式提供服務。
Tomcat :中小企業動态Web服務主流,網際網路Java容器主流(如jsp、do )
Resin :大型動态Web服務主流,網際網路Java容器主流(如jsp、do )
IIS ( Internet information services ):微軟 windows 下的 Web 服務軟體(如 asp、aspx )
如果你聽說或使用過 Apache軟體,那麼很快就會熟悉 Nginx軟體,與 Apache軟體類似, Nginx ( “engme x")是一個開源的,支援高性能、高并發的 WWW伺服器和代理服務軟體。它是由俄羅斯人 Igor Sysoev開發的,最初被應用在勘羅斯的大型網站 www.rambler.ru 上,後來作者将源代碼以類BSD許可證的形式開源出來供全球使用。
Nginx因具有高并發(特别是靜态資源)占用系統資源少等特性,且功能豐富而逐漸流行起來。
在功能應用發面,Nginx不但是一個優秀的Web服務軟體,還具有反向代理負載均衡功能和緩存服務功能。在反向代理負載均衡功能方面,它類似于大名鼎鼎的LVS負載均衡及Haproxy等專業代理軟體,但是Nginx部署起來更為簡單、友善;在緩存服務功能方面,它又類似于Squid等專業的緩存服務軟體。
Nginx 可以運作在 UNIX、Linux、BSD、Mac 0S X、Solaris,以及 Microsoft Windows 等作業系統中。随着Nginx在國内很多大型網站中的穩定高效運作,近兩年它也逐漸被越來越多的中小型網站所使用。目前流行的Nginx Web組合被稱為LNMP或LEMP(即Linux Nginx MySQL PHP),其中 LNMP 裡的 N 取自Nginx ( "engine x" )
Nginx 的官方介紹見 http://nginx.org/en
u 支援高并發:能支援幾萬并發連接配接(特别是靜态小檔案業務環境)
u 資源消耗少:在3萬并發連接配接下,開啟10個Nginx線程消耗的記憶體不到200MB
u 可以做HTTP反向代理及加速緩存、即負載均衡功能,内置對RS節點伺服器健康檢查功能,這相當于專業的Haproxy軟體或LVS的功能。
u 具備Squid等專業緩存軟體等的緩存功能。
u 支援異步網絡I/O事件橫型epoll(Linux2.6+)
Ø web網站服務
Ø 反向代理負載均衡(nginx /lvs /haproxy)
Ø nginx緩存服務(memcache /redis /mongodb)
業務類型
應用方案
靜态業務
若是高并發場景,盡量采用Nginx或Lighttpd,二者首選Nginx
動态業務
理論上采用Nginx和Apache均可,建議選擇Nginx,為了避免相同業務的服務軟體多樣化,增加額外維護成本。動态業務可以由Nginx兼做前端代理,再根據頁面元素的類型或目錄,轉發到後端相應的伺服器處理程序。
---首選tomcat
既有靜态業務又有動态業務
采用Nginx
利用nginx軟體是無法處理動态業務請求,要讓nginx結合php軟體處理動态業務請求,在加上mysql 即 LNMP架構
先來看看Apache軟體的特點,如下
² Apache2.2版本非常穩定強大,據官方說,Apache2.4版本性能更強。
² Prefork模式取消了程序建立開銷,性能很高。
² 處理動态業務資料時,因關聯到後端的引擎和資料庫,瓶頸不在Apache上。
² 高并發時消耗系統資源相對多一些。
² 基于傳統的select模型,高并發能力有限。
² 支援擴充庫,可通過DSO、apxs方法編譯安裝額外的插件功能,不需要重新編譯Apache
² 功能多,更穩定,更安全,插件也多。
² 市場佔有率在逐年遞減
再來看看Nginx軟體的特點,如下:
n 基于異步網結I/O模 型(epollk kqueue)
n 具備支援高性能,高并發的特性,并發連接配接可達數萬。
n 對小檔案(小于1 MB的靜态檔案)高并發支援很好,性能很高
n 不支援類似 Apache的DSO模式、擴充庫必須編譯進主程式(缺點)
n 程序占用系統資源比較低。
n 支援Web、反向Proxy、Cache三大重點功能,幷且都很優秀。
n 市場佔有率在逐年快速增加。
最後是Lighttpd的特點,如下:
² 基于異步網絡 I/ O模型,性能、并發都與 Nginx相近。
² 擴充庫是 SO模式,比Nginx靈活
² 目前國内的使用率比較低,安全性沒有 Apache和Nginx好。
² 通過插件(mod_secdownload)可實作檔案 URL位址加密(優點)
² 社群不活躍,市場佔有率較低,
NGINX 使用的是epoll 和Kqueue 異步網絡I/O模型,而apache使用的是傳統的select模型
比喻:
第一個比喻:
假設你在大學讀書,住的宿舍樓有很多房間,你的朋友要來找你。select版宿管大媽就會帶着你的朋友到各房間挨個去找,直到找到你為止。而epoll版宿管大媽會先記下每位入住同學的房間号,你的朋友來找你時,隻需告訴你的朋友你住在哪個房間即可,不用親自帶着你的朋友滿宿舍樓找人了。如果同時來了100個人,都要找自己住這棟樓的同學,select版和epoll版宿管大媽,誰的效率更高,就很明顯了。
第二個比喻:
select的調用複雜度是線性的,即O(n)。舉個例子,一個保姆照看照看一群孩子,如果把孩子是否需要尿尿比作網絡I/O事件,select的作用就好比這個保姆挨個詢問每個孩子"你要尿尿嗎?”如果孩子回答是,保姆則把孩子領出來放到另外一個地方。當所有孩子詢問完之後,保姆領着這些要尿尿的孩子去上廁所(處理網絡I/O事件).在epoll機制下,保姆不再需要挨個詢問每個孩子是否需要尿尿。取而代之的是,如果孩子需要尿尿,他就自己主動站到事先約定好的地方,而保姆的職責就是檢視事先約定好的地方是否有孩子。如果有小孩,則領着孩子去上廁所(網絡事件處理)。是以,epoll的這種機制,能夠高效地處理成千上萬的并發連接配接,并且性能不會随着連接配接數増加而下降太多。
名額
select
epoll
性能
随着連接配接數的增加性能急劇下降。處理成千上萬的并發連接配接數,性能很差
随着連接配接數的增加,性能基本上沒有下降。處理成千上萬連接配接時性能很好
連接配接數
連接配接數有限制,處理的最大連接配接數不超過1024,如果要處理的連接配接數超過1024個,則需要修改FD_SETSIZE宏,并重新編譯
連接配接數無限制
内在處理機制
線性輪詢
回調callback
開發複雜性
低
中
[root@web01 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@web01 ~]# uname -r
2.6.32-696.el6.x86_64
yum install -y pcre-devel openssl-devel
pcre:相容perl語言正規表達式,perl compatible regular expressions
rewirte子產品 參數資訊(perl方式定義正規表達式)
openssl:ssh---openssh/openssl---https
總結:所有安裝依賴軟體,後面都要加上-devel
wget http://nginx.org/download/nginx-1.10.2.tar.gz
說明:軟體很小,用心檢視一下
解壓軟體
tar xf nginx-1.10.2.tar.gz
useradd -M -s /sbin/nologin www
軟體編譯安裝步驟
a>軟體解壓配置(将軟體程式安裝到哪個目錄中 開啟nginx軟體的哪些功能)
b>軟體編譯過程
c>軟體編譯安裝過程
注意順序,順序不對軟體安裝會出錯
1、配置軟體,在軟體的解壓目錄中
[root@web01 nginx-1.10.2]# ./configure --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
編譯參數說明:
--prefix 表示指定軟體安裝到哪個目錄中,指定目錄不存在會自動建立
--user/--group nginx工作程序由哪個使用者運作管理
--with-http_stub_status_module 啟動nginx狀态子產品功能(使用者通路nginx的網絡資訊)
--with-http_ssl_module 啟動https功能子產品
通過軟體編譯過程中的傳回值是否正确,确認配置是否正确
[root@web01 nginx-1.10.2]# echo $?
2、編譯軟體
[root@web01 nginx-1.10.2]# make
3、編譯安裝
[root@web01 nginx-1.10.2]# make install
[root@web01 application]# ln -s /application/nginx-1.10.2/ /application/nginx
[root@web01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
[root@web01 application]# /application/nginx/sbin/nginx
[root@web01 application]#
檢查是否啟動
[root@web01 application]# ps -ef |grep nginx
root 26548 1 0 20:13 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
www 26549 26548 0 20:13 ? 00:00:00 nginx: worker process
root 26551 23431 3 20:13 pts/0 00:00:00 grep --color=auto nginx
檢查端口資訊
[root@web01 application]# netstat -lntup |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26548/nginx
服務部署完成
至此軟體安裝完畢!
nginx指令簡化方法
echo 'export PATH=/application/nginx/sbin:$PATH'>>/etc/profile
source /etc/profile
which nginx
[root@web01 nginx]# ll
total 36
drwxr-xr-x 2 root root 4096 Oct 21 19:34 conf #配置檔案儲存目錄
drwxr-xr-x 2 root root 4096 Oct 21 19:34 html #站點目錄
drwxr-xr-x 2 root root 4096 Oct 21 20:26 logs #nginx 服務相關日志檔案儲存目錄(錯誤日志通路日志)
drwxr-xr-x 2 root root 4096 Oct 21 19:34 sbin # 服務指令目錄(隻有一個nginx檔案)
這樣的配置檔案是通過精簡化配置檔案得到!
[root@web01 conf]# cat nginx.conf
worker_processes 1; ← worker 程序數量
events { ←事件區塊
worker_connections 1024; ←每個worker程序可以處理的連接配接數
} ←事件區塊結束
http { ← HTTP 區塊
include mime.types; ←支援的媒體檔案
default_type application/octet-stream;←預設的媒體類型
sendfile on; ←高效傳輸模式
keepalive_timeout 65; ←逾時時間
server { ← server 區塊
listen 80; ←端口
server_name localhost; ←域名
location / { ←第一個location區塊
root html; ←站點目錄
index index.html index.htm; ←首頁檔案
} ←第一個location區塊結束
error_page 500 502 503 504 /50x.html; ← 錯誤資訊配置
location = /50x.html { 檔案位置
root html; 在哪找:路徑
}
} ← server 區塊結束
} ← HTTP 區塊結束
軟體依賴包未正确安裝問題---PCRE依賴包沒有安裝
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解決方法:yum install pcre pcre-devel -y
軟體依賴包未正确安裝問題---OPENSSL依賴包沒有安裝
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
解決方法:yum install openssl openssl-devel -y
nginx軟體重複啟動産生的錯誤資訊
[root@web01 nginx-1.10.2]# /application/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
解決方法:
nginx軟體已經啟動無需反複啟動,如果需要重新啟動需要停止nginx程序或者用reload方式進行重新開機
解答這是因為沒有對應的Nginx服務使用者,執行useradd nginx-s/sbin/no丨ogin-M建立 Nginx
使用者即可。為了讓讀者了解問題,重制上述錯誤過程,指令如下:
[root@web01 tools]# pkill nginx
[root@web01 tools]# userdel nginx
[root@web01 tools]# /application/nginx/sbin/nginx
nginx: [emerg] getpwnam(Mnginx") failed
[root@web01 tools]# useradd nginx -s /sbin/nologin -M
報錯資訊如下:
[root@gjlin2 pcre-8.30]# make && make install
make all-am
make[l] : Entering directory 7home/gjlin/tools/pcre-8.30'
CXX pcrecpp.lo
libtool : compile : unrecognized option '-DHAVE_CONFIG_H'
libtool : compile : Try 'libtool --help* for more information.
make[l】:*** [pcrecpp.lo】錯誤 1
make[l] : Leaving directory Vhome/gjlin/tools/pcre-8.30'
make : *** [all]錯誤 2
解答:執行“yum -y install gcc-c++”指令安裝gcc-c++依賴包。
說明:編譯安裝步驟不對(配置 編譯 編譯安裝生成/appliation)
a 在用戶端上ping伺服器端IP,檢查鍊路是否通暢
b 在用戶端上telnet伺服器端IP、端口,檢查鍊路通路是否通暢
c 在用戶端上wget檢測模拟頁面通路是否正常
01. 服務阻止用戶端通路
02. 服務端站點目錄中,沒有指定首頁檔案資訊
[root@web01 application]# /application/nginx/sbin/nginx -s stop
[root@web01 application]# /application/nginx/sbin/nginx -s reload
[root@web01 application]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
[root@web01 application]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.10.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
①. nginx軟體文法檢查方法:
nginx -t
②. nginx軟體通路測試過程:
curl -v www.baidu.com
③. nginx軟體編譯參數檢視:
nginx -V <--- 檢視原有的編譯參數資訊
編寫配置檔案
[root@web02 www]# cat ../../conf/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.zinx.top;
location / {
root html/www;
index oldboy.html index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
編寫靜态通路頁面檔案資訊
[root@web1 www]# cat oldboy.html
<html>
<meta charset="utf-8">
<head>
<title>TEST</title>
</head>
<body>
慘綠少年
<table border=1>
<tr> <td>01</td> <td> </td> </tr>
<tr> <td>02</td> <td> </td> </tr>
<tr> <td>03</td> <td> </td> </tr>
</table>
<a href="http://blog.znix.top">
<img src="znix.png" />
</a>
</body>
</html>
1、參看已安裝服務的配置參數資訊
[root@web01 sbin]# /application/nginx/sbin/nginx -V
-V 參數能顯示軟體的詳細詳細,安裝配置參數
2、按照配置參數進行部署
[root@web01 nginx]# /application/nginx/sbin/nginx -t
通過配置 autoindex on; 參數
使用 autoindex參數,nginx能識别的直接顯示,不識别的直接下載下傳
配置完 autoindex on; 參數以後 會顯示站點下的檔案資訊
對于nginx可以解析的資源會解析相應的内容
對于nginx不可以解析的資源會直接下載下傳
[root@web02 ~]# echo 'web01 www' > /application/nginx/html/www/index.html
<- 在虛拟主機指定的站點目錄中建立首頁檔案
[root@web02 ~]# curl www.etiantian.org
<- 利用curl指令本地檢測nginx配置是否成功;已經存在首頁檔案,測試成功
1)修改配置檔案
[root@web01 www]# cat ../../conf/nginx.conf
listen 10.0.0.8:80;
server_name www.etiantian.org;
autoindex on;
2)重新開機服務
[root@web01 conf]# /application/nginx/sbin/nginx -t
[root@web01 conf]# /application/nginx/sbin/nginx -s reload
3)通路測試
虛拟主機使用的是特殊的軟硬體技術,它把一台運作在網際網路上的伺服器主機分成一台台“虛拟”的主機,每台虛拟主機都可以是一個獨立的網站,可以具有獨立的域名,具有完整的Intemet伺服器功能(WWW、FTP、Email等),同一台主機上的虛拟主機之間是完全獨立的。從網站通路者來看,每一台虛拟主機和一台獨立的主機完全一樣。
利用虛拟主機,不用為每個要運作的網站提供一台單獨的Nginx伺服器或單獨運作一組Nginx程序。虛拟主機提供了在同一台伺服器、同一組Nginx程序上運作多個網站的功能。
所謂虛拟主機,在Web服務裡就是一個獨立的網站站點,這個站點對應獨立的域名(也可能是ip或端口.具有獨立的程式及資源目錄,可以獨立地對外提供服務供使用者通路。
這個獨立的站點在配置裡是由一定格式的标簽段标記的,對于Apache軟體來說,一個虛拟主機的标簽段通常被包含在以的此<VirtualHost></VirtualHost>,而Nginx軟體則使用一個server{}标簽來标示一個虛拟主機。一個Web服務裡可以有多個虛拟主機标簽對,即可以同時支援多個虛拟主機站點。
常見的虛拟主機類型有如下幾種
1)基于域名的虛拟主機
所謂基于域名的虛拟主機,意思就是通過不同的域名區分不同的虛拟主機,基于域名的虛拟主機是企業應用最廣的虛拟主機類型,幾乎所有對外提供服務的網站使用的都是基于域名的虛拟主機,例如: www.znix.top。
2)基于端口的虛拟主機
同理,所謂基于端口的虛拟主機,意思就是通過不同的端口來區分不同的虛拟主機,此類虛拟主機對應的企業應用主要為公司内部網站,例如:一些不希望直接對外提供使用者通路的網站背景等,通路基于端口的虛拟主機,位址裡要帶有端口,例如:http://blog.znix.top:80
3)基于IP的虛拟主機
所謂基于IP的虛拟主機,意思是通過不同的IP區分不同的虛拟主機,
1)增加一個完整的server标簽段到結尾處。注意,要放在http的結束大括号前,也就是将server标簽段放入http标簽。
2)更改server_name及對應網頁的root根目錄,如果需要其他參數,可以增加或修改。
3)建立Seever_name域名對應網頁的根目錄,并且建立測試檔案,如果沒有index首頁,通路會出現403錯誤。
如果是apache軟體,沒有首頁檔案,預設會把站點目錄下面的資訊顯示出來
nginx出403錯誤解決方式:http://oldboy.blog.51cto.com/2561410/1633952
autoindex on;#<==當找不到首頁檔案時,會展示目錄結構,這個功能一般不要用除非有需求。
PS:顯示的目錄結構中,有些資訊點選就是下載下傳,有的點選就是顯示,因為擴充名稱不一樣
根本在于nginx軟體是否能夠進行解析
nginx是否解析:
1.htmljpg認識顯示出内容
2.不認識不解析便直接下載下傳
4)檢査Nginx配置檔案文法,平滑重新開機Nginx服務,快速檢査啟動結果。
5)在用戶端對server_name處配置的域名做host解析或DNS配置,并檢査(ping域名看傳回的IP是否正确)。
6)在Win32浏覽器中輸入位址通路,或者在Linux用戶端做hosts解析,用wget或curl接位址通路。
7)在服務重新開機或關閉之前先進行一次配置檔案檢查 /application/ngnix/sbin/nginx -t
Nginx虛拟主機的官方幫助網址為:http://Nginx.org/en/docs/http/requestj3rocessing.html
修改配置檔案
[root@web01 ~]# cat /application/nginx/conf/nginx.conf
index index.html index.htm;
server_name bbs.etiantian.org;
root html/bbs;
server_name blog.etiantian.org;
root html/blog;
檢查配置資訊是否正确
重新開機服務
注意:服務沒有啟動的時候不能使用平滑重新開機
建立站點目錄
[root@web01 conf]# for name in www blog bbs ;do mkdir ../html/$name -p ;done
建立首頁檔案
[root@web01 conf]# for name in www blog bbs ;do echo "web01 $name" >../html/$name/index.html ;done
檢查首頁内容資訊
[root@web01 conf]# for name in www blog bbs ;do cat ../html/$name/index.html ;done
web01 www
web01 blog
web01 bbs
修改主機hosts檔案
[root@web01 ~]# vim /etc/hosts
172.16.1.8 web01 www.etiantian.org blog.etiantian.org bbs.etiantian.org
測試
[root@web01 ~]# curl www.etiantian.org
[root@web01 ~]# curl blog.etiantian.org
[root@web01 ~]# curl bbs.etiantian.org
修改配置檔案内容
[root@web01 conf]# vim nginx.conf
listen 81;
"nginx.conf" 46L, 1098C written
[root@web01 conf]# netstat -lntup |grep ng
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 40110/nginx
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 40110/nginx
測試通路
[root@web01 ~]# curl bbs.etiantian.org:81
注意:
采用基于IP的虛拟主機,配置檔案修改後要重新開機(-s stop)
配置和IP位址配置相關的都要采用(-s stop)重新開機,不能夠使用軟重新開機的方式
檢查配置檔案格式
重新開機服務,注意使用的是直接重新開機的方式
隻要nginx配置檔案中涉及到IP位址的更改隻能正真的重新開機才生效
[root@web01 conf]# /application/nginx/sbin/nginx -s stop
[root@web01 conf]# /application/nginx/sbin/nginx
tcp 0 0 10.0.0.8:80 0.0.0.0:* LISTEN 40592/nginx
通路測試
[root@web01 conf]# pwd
/application/nginx/conf
[root@web01 conf]# mkdir extra
[root@web01 conf]# sed -n '10,21p' nginx.conf > extra/www.conf
[root@web01 conf]# sed -n '22,33p' nginx.conf > extra/bbs.conf
[root@web01 conf]# sed -n '34,45p' nginx.conf > extra/blog.conf
[root@web01 conf]# cat nginx.conf
include extra/*;
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 40714/nginx
[root@web01 logs]# /application/nginx/sbin/nginx -T
參數說明:
-T : test configuration, dump it and exit
測試配置檔案,并且加載一遍,并顯示加載的順序
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
這樣的配置能夠讓使用者通過IP通路的時候,通路到的網站是www的網站。
在配置檔案中添加别名
[root@web01 conf]# vim extra/www.conf
server_name www.etiantian.org ett.org;
修改hosts 進行通路測試
[root@web01 www]# curl ett.org
修改配置檔案,添加三status子產品
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
}
include extra/www.conf;
檢查配置檔案是否正确
說明:
以上頁面内容資訊主要會被zabbix監控服務調取,形成圖像資訊
根據圖形資訊,進而判斷nginx網站服務使用者通路量情況
參數
參數說明
Active connections
目前的活動用戶端連接配接數量
accepts
接受用戶端連接配接的總數
handled
處理的連接配接總數
requests
用戶端請求的總數
Reading
nginx正在讀請求頭的目前連接配接數。
Writing
nginx正在将響應寫回用戶端的目前連接配接數。
Waiting
目前空閑用戶端連接配接數等待一個請求。
nginx的兩種日志種類
錯誤日志:記錄nginx運作錯誤情況資訊
通路日志:記錄使用者通路日志資訊
官方說明
http://nginx.org/en/docs/ngx_core_module.html#error_log
系統預設配置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
配置錯誤日志,修改主配置檔案
[root@web01 logs]# vim ../conf/nginx.conf
error_log logs/error.log error;
server{
[root@web01 logs]# /application/nginx/sbin/nginx -t
[root@web01 logs]# /application/nginx/sbin/nginx -s reload
檢視錯誤日志資訊
[root@web01 logs]# tail error.log
2017/10/25 11:41:55 [error] 40842#0: *7 open() "/application/nginx-1.10.2/html/www/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: www.etiantian.org, request: "GET /favicon.ico HTTP/1.1", host: "www.etiantian.org", referrer: "http://www.etiantian.org/"
#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;
修改通路日志配置--修改主配置檔案
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
修改通路日志配置--修改虛拟主機配置檔案
[root@web01 www]# cat /application/nginx/conf/extra/www.conf
access_log logs/access_www.log main;
日志内容
10.0.0.1 - - [22/Oct/2017:16:04:54 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"
配置檔案
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
日志資訊說明
含義
$remote_addr
10.0.0.1
用戶端ip位址
-
$remote_user
顯示遠端通路者使用者資訊
[$time_local]
[22/Oct/2017:16:04:54 +0800]
顯示通路時間
$request
GET / HTTP/1.1"
請求行資訊
$status
304
狀态碼
$body_bytes_sent
響應封包主體内容大小
$http_referer
$http_user_agent
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
用戶端浏覽網頁工具資訊
$http_x_forwarded_for
反向代理轉發
詳細日志詳細說明
Nginx日志變量
說明
記錄通路網站的用戶端位址;即源IP位址
目前端有代理伺服器時,設定web節點記錄用戶端位址的配置,此參數生效的
前提是代理伺服器上也進行了相關的x_forwarded_for設定
可以記錄使用者真實的IP位址資訊
遠端用戶端使用者名稱
$time_local
記錄通路時間與時區
使用者的http請求起始行資訊
http狀态碼,記錄請求傳回的狀态,例如:200 , 404 , 301等
$body_bytes_sents
伺服器發送給用戶端的響應body位元組數
記錄此次請求是從哪個連結通路過來的,可以根據referer進行防盜鍊設定
即表示是哪個網站介紹過來的
記錄用戶端通路資訊,例如:浏覽器、手機用戶端等
在沒有特殊要求的情況下,采用預設的配置即可,更多可以設定的記錄日志資訊的變量見:
http://nginx.org/en/docs/http/ngx_httpJog_module.html1
切割日志方式
01:利用腳本實作日志切割
簡單腳本
[root@web01 logs]# cat /server/scripts/log_cut.sh
#!/bin/bash
mv /application/nginx/logs/access_www.log /application/nginx/logs/access_www_`date +%F`.log
/application/nginx/sbin/nginx -s reload
包含判斷的腳本
[root@web01 logs]# cat /server/script/cut_nginx_log.sh
# ! /bin/sh
Dateformat='date +%Y%m%d'
Basedir= "/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[-d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[-f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
02.利用系統自帶切割軟體進行切割
cat /etc/logrotate.conf
1、搭建好一台nginx的web伺服器,配置好内網網卡位址與外網網卡位址
2、web伺服器的網站域名為www.etiantian.org 站點目錄為 html/www
3、要求内網可以通路 www.etiantian.org/AV 資源
4、要求外網不可以通路 www.etiantian.org/AV 資源
1、定位需要控制的資源
location == if
[root@web01 extra]# cat www.conf
location /AV {
root html/www;
index index.html index.htm;
allow 172.16.1.0/24;
deny all;
location 指令的作用是根據使用者請求的URI來執行不同的應用。
locationn使用的文法為
location [=|~|~*|^~] uri {
....
location 文法說明表
location
[=|~|~*|^~]
uri
{....}
指令
比對辨別
比對的網站位址
比對URI後要執行的配置段
~ 與~* 的差別
u ~ 比對内容區分大小寫
u ~* 比對内容不區分的小寫
u !~ 取反
u ^~ 但多個比對同時存在,優先比對 ^~比對的内容;不做正規表達式的檢查 (優先處理)
location = / {
[ configuration A ]
location / {
[ configuration B ]
location /documents/ {
[ configuration C ]
location ^~ /images/ {
[ configuration D ]
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
"/"請求将比對配置A,
"/index.html"請求将比對配置B,
"/documents/document.html"請求将比對配置C,
"/images/1.gif"請求将比對配置D,
"/documents/1.jpg"請求将比對配置E.
不同uri及特殊字元組合比對的順序說明
順序
不用URI及特殊字元組合比對
比對說明
1
location = / {}
精确比對 /
2
location ^~ /image/{
比對正常字元串,不做正規表達式比對檢查
3
正則比對
4
比對正常字元串,如果有正則,則優先比對正則
5
所有location 都不能比對後的預設比對
定義不同的location傳回不同的數值
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
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;
根據請求不同uri的傳回值驗證 location的配置。
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents
401
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org
402
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents/ss.jpg
500
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents/
403
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/images/1.jpg
404
将位址資訊進行重寫
rewrite 文法格式
rewrite regex replacement [flag]
rewrite應用标簽:server、location、if
1. 實作網站位址資訊跳轉
2. 實作僞靜态
if ($host ~* "^etiantian.org$") {
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
access_log logs/access_www.log main;
[root@web01 www]# curl etiantian.org -L
server {
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
可以調整使用者浏覽的URL,使其看起來更規範,合乎開發及産品人員的需求。
為了讓搜尋引擎收錄網站内容,并讓使用者體驗更好,企業會将動态URL位址僞裝成靜态位址提供服務。
網站換新域名後,讓舊域名的通路跳轉到新的域名上,例如:讓京東的360buy換成了jd.com。
根據特殊變量、目錄、用戶端的資訊進行URL跳轉等。
說明:開源軟體類似wordpress的,官方都會對僞靜态配置進行說明
vim extra/www.conf
root html/www;
index index.html index.htm;
auth_basic "oldboy training";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
注: 這裡使用的htpasswd指令預設是沒有的,需要通過yum install httpd-tools -y 安裝
[root@web01 extra]# htpasswd -c /application/nginx/conf/htpasswd oldboy
New password:
Re-type new password:
Adding password for user oldboy
-c Create a new file.
建立一個新的密碼檔案
-b Use the password from the command line rather than prompting for it.
采用免互動的方式輸入使用者的密碼資訊
htpasswd參數說明
-c
建立一個新檔案。
-n
不更新檔案; 顯示結果。
-m
強制MD5密碼加密。
-d
強制CRYPT加密密碼(預設)。
-p
不加密密碼(明文)。
-s
強制SHA加密密碼。
-b
使用指令行中的密碼,而不是提示。(免互動)
-D
删除指定的使用者。
[root@web01 extra]# chmod 400 /application/nginx/conf/htpasswd
[root@web01 extra]# chown -R www.www /application/nginx/conf/htpasswd
[root@web01 extra]# cat /application/nginx/conf/htpasswd
oldboy:e3OfMiJThE0Qg
[root@web01 extra]# /application/nginx/sbin/nginx -t
[root@web01 extra]# /application/nginx/sbin/nginx -s reload
使用互動式輸入密碼
[root@web01 www]# curl www.etiantian.org -uoldboy
Enter host password for user 'oldboy':123456
使用免互動輸入密碼
[root@web01 www]# curl www.etiantian.org -uoldboy:123456
401錯誤說明: 需要認證,但是沒有認證