nginx是linux上常用的web服務器,功能能多。一般採用編譯安裝,測試使用的版本是nginx1.15.7 http://nginx.org/
一、編譯安裝nginx
安裝Nginx所需的pcre庫。pcre全稱是perl compatible regular expressions,中文名稱爲perl兼容正則表達式,目的是使nginx支持具備URI重寫功能的rewrite模塊。
yum安裝即可
yum install pcre pcre-devel -y
安裝openssl ,openssl-devel 和gcc
openssl使使用https服務時候使用的模塊,如果不安裝則會再nginx安裝過程中會報錯
gcc GCC(GNU Compiler Collection,GNU編譯器套件),即編譯器
yum install openssl openssl-devel gcc -y
安裝ngnix
wget useradd nginx -s /sbin/nologin -M tar xvf nginx-1.15.7.tar.gz cd nginx-1.15.7 ./configure --usr=nginx --group=nginx --prefix/application/nginx-1.15.7/ --with-http_stub_status_module --with-http_ssl_module #添加基礎模塊 make make install ln -s /application/nginx-1.15.7/ /application/nginx #更改爲/application/nginx,工程經驗
完成安裝後啓動並檢查nginx狀態
/application/nginx/sbin/nginx #啓動nginx lsof -i:80 #檢查端口
如果能通過網頁url訪問 則說明nginx安裝成功
二、nginx功能模塊說明
Nginx http 功能模塊 | 模塊說明 |
---|---|
ngx_http_core_module | 包括一些核心的 http 參數配置,對應 Nginx 的配置爲 HTTP 區塊部分 |
ngx_http_access_module | 訪問控制模塊,用來控制網站用戶對 Nginx 的訪問 |
ngx_http_gzip_module | 壓縮模塊,對 Nginx 返回的數據壓縮,屬於性能優化模塊 |
ngx_http_fastcgi_module | FastCGI 模塊,和動態應用相關的模塊,如 PHP |
ngx_http_proxy_module | proxy 代理模塊 |
ngx_http_upstream_module | 負載均衡模塊,可實現網站的負載均衡和節點的健康檢查 |
ngx_http_rewrite_module | URL 地址重寫模塊 |
ngx_http_limit_conn_module | 限制用戶併發連接數以及請求數的模塊 |
ngx_http_limit_req_module | 根據定義的 key 限制 Nginx 請求過程的速率 |
ngx_http_log_module | 訪問日誌模塊,以指定的格式記錄 Nginx 客戶訪問日誌等信息 |
ngx_http_auth_basic_module | Web 認證模塊,設置 Web 用戶通過賬號密碼訪問 Nginx |
ngx_http_ssl_module | ssl 模塊,用於加密的 http 連接,如 https |
ngx_http_stub_status_module | 記錄 Nginx 基本訪問狀態信息等的模塊 |
nginx目錄結構說明
[root@www ~]# tree /application/nginx/ /application/nginx/ |-- client_body_temp |-- conf #這是Nginx所有配置文件的目錄,極其重要 | |-- fastcgi.conf #fastcgi相關參數的配置文件 | |-- fastcgi.conf.default #fastcgi.conf的原始備份 | |-- fastcgi_params #fastcgi的參數文件 | |-- fastcgi_params.default | |-- koi-utf | |-- koi-win | |-- mime.types #媒體類型, | |-- mime.types.default | |-- nginx.conf #這是Nginx默認的主配置文件 | |-- nginx.conf.default | |-- scgi_params #scgi相關參數文件,一般用不到 | |-- scgi_params.default | |-- uwsgi_params #uwsgi相關參數文件,一般用不到 | |-- uwsgi_params.default | `-- win-utf |-- fastcgi_temp #fastcgi臨時數據目錄 |-- html #這是編譯安裝時Nginx的默認站點目錄,類似 Apache的默認站點htdocs目錄 | |--50x.html # 錯誤頁面優雅替代顯示文件,例如:出現502錯誤時會調用此頁面 # error_page 500502503504 /50x.html; | `-- index.html # 默認的首頁文件,首頁文件名字是在nginx.conf中事先定義好的。 |-- logs #這是Nginx默認的日誌路徑,包括錯誤日誌及訪問日誌 | |-- access.log # 這是Nginx的默認訪問日誌文件,使用tail -f access.log,可以實時觀看網站用戶訪問情況信息 | |-- error.log # 這是Nginx的錯誤日誌文件,如果Nginx出現啓動故障等問題,一定要看看這個錯誤日誌 | `-- nginx.pid # Nginx的pid文件,Nginx進程啓動後,會把所有進程的ID號寫到此文件 |-- proxy_temp #臨時目錄 |-- sbin #這是Nginx命令的目錄,如Nginx的啓動命令nginx | `-- nginx #Nginx的啓動命令nginx |-- scgi_temp #臨時目錄 `-- uwsgi_temp #臨時目錄 9 directories,21 files
nginx的主配置文件說明
... #全局塊 events { #events塊 ... } http #http塊 { ... #http全局塊 server #server塊 { ... #server全局塊 location [PATTERN] #location塊 { ... } location [PATTERN] { ... } } server { ... } ... #http全局塊 }
三、nginx簡單運用
3.1多域名虛擬主機
基於域名的虛擬主機,前提使已經搭建好了域名服務器,www.chucloud.com和web.chucloud.com解析爲nginx的服務器地址
修改nginx.conf文件
server{ listen 80; server_name www.chucloud.com; #綁定域名 root html/www; #網站根目錄 index index.htm index.html ; #默認文件 } server{ listen 80; server_name bbs.chucloud.com; #綁定域名 root html/bbs; #網站根目錄 index index.htm index.html ; #默認文件 }
創建新增域名分別對飲的站點目錄及文件
mkdir ../html/www ../html/blog -p echo " > ../html/www/index.html echo " > ../html/bbs/index.html
檢查nginx語法及重新加載
/application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload
3.2多端口虛擬機主機
修改配置文件
server{ listen 80; server_name www.chucloud.com; #綁定域名 root html/www; #網站根目錄 index index.htm index.html ; #默認文件 } server{ listen 81; #修改端口 server_name bbs.chucloud.com; #綁定域名 root html/bbs; #網站根目錄 index index.htm index.html ; #默認文件 }
端口任意修改,只要不和已有服務衝突即可
檢查nginx語法及重新加載
/application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload
3.3 優化配置文件
nginx的主配置文件爲nginx.conf,主配置文件包含的所有虛擬主機子配置文件會統一放入extra目錄中,虛擬主機的配置文件按照網站的域名或者功能取名,例如www.conf bbs.conf blog,conf等。
使用參數include,語法如下
include file |mask;
修改完成的include配置的主配置文件如下
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_usr_agent" "$http_x_forwarded_for"'; sendfile on; keepalive_timeout 65; include extra/www.conf; #定義額外配置路徑,絕對路徑爲/application/nginx/conf/extra include extra/bbs.conf; include extra/blog.conf; include extra/status.conf; }
單個虛擬機主機配置文件,blog.conf如下所示:
server { listen 81; server_name blog.chucloud.com; location / { root html/blog; index index.html index.htm; }
完成配置後,檢查nginx語法,重啓nginx生效
3.4配置nginx status
nginx軟件功能模塊中有一個ngx_stub_status_module模塊,該模塊主要功能是記錄nginx的基本訪問信息,可以瞭解nginx的工作狀態,例如使用信息等。在編譯Nginx 時必須增加http_stub_status_module模塊來支持
在 extra路徑下新建status.conf 文件,配置如下
#status server{ listen 80; server_name status.chucloud.com; location / { stub_status on; #打開狀態信息開關 access_log off; } }
檢查語句並重啓nginx後,訪問status.chucloud.com
說明:
1、server 表示目前處理了38個連接
2、accepts表示目前創建了38次握手
3、handled requests表示處理了60次請求
4、reading爲nginx讀取到客戶端的Header信息數
5、writing爲nginx返回給客戶端的Header信息數
6、waiting爲nginx已處理完正在等候下一次請求指令的駐留連接
3.5 爲nginx增加錯誤日誌(error_log)配置
nginx會把自身運行的故障信息及用戶訪問的日誌信息記錄到制定日誌文件
參數名字爲error_log,可以放在main區塊中全局配置,也可以放置在不同的虛擬機中單獨記錄。
語法格式如下:
error_log file level;
關鍵字 日誌文件 錯誤日誌級別
其中關鍵字error_log不能改變,日誌文件可以制定任意存放日誌的目錄、錯誤日誌級別常見有[debug|info|notice|warn|error|crit|alert|emerg]
默認值爲:
#default: error_log logs/error.log error;
在主配置文件nginx.conf中,錯誤日誌配置如下:
3.6訪問日誌
nginx軟件會把每個用戶訪問網站的日誌信息記錄到制定的日誌文件中,提供給網站分析用戶瀏覽行爲,此功能由ngx_http_log_module模塊負責
參數如下
log_format 用來定義日誌的格式
access_log 用來指定日誌路徑以及何種日誌記錄
日誌格式中默認如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '$http_usr_agent" "$http_x_forwarded_for"';
記錄日誌默認參數如下
access_log logs/access.log main;
日誌變量說明
1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址; 2.$remote_user :用來記錄客戶端用戶名稱; 3.$time_local : 用來記錄訪問時間與時區; 4.$request : 用來記錄請求的url與http協議; 5.$status : 用來記錄請求狀態;成功是200, 6.$body_bytes_sent:發送給客戶端的文件主體內容的大小,比如899,可以將日誌每條記錄中的這個值累加起來以粗略估計服務器吞吐量。 7.$http_referer :用來記錄從那個頁面鏈接訪問過來的; 8.$http_user_agent :記錄客戶端瀏覽器的相關信息; 9.$request : 請求內容 10.$status :請求狀態嗎 11.$http_user_agent: 客戶端機型 12.$http_cookie客戶端的cookie 13. $hostname 本主機服務器主機名 14.$upstream_addr 轉發到哪裏 15.$upstream_response_time : 轉發響應時間 16.$request_time:整個請求的總時間。 17.$server_name:虛擬主機名稱 18.http_x_forwarded_for:客戶端的真實ip,通常web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。 19. $ssl_cipher:交換數據中的算法,比如RC4-SHA
記錄日誌的access_log參數說明
access_log off; #關閉access_log,即不記錄訪問日誌 access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; 說明: buffer=size #爲存放訪問日誌的緩衝區大小 flush=time #爲緩衝區的日誌刷到磁盤的時間 gzip[=level] #表示壓縮級別 [if = condition] #表示其他條件
主配置nginx.conf示例
虛擬機主機配置文件,www.conf示例
3.7 location
location指令是根據用戶請求的URI執行不同的應用,即根據用戶請求的網站的地址URL進行匹配,匹配成功即進行相關操作
語法規則: location [=|~|~*|^~] /uri/ { … }
= 開頭表示精確匹配
^~ 開頭表示uri以某個常規字符串開頭,理解爲匹配 url路徑即可。nginx不對url做編碼,因此請求爲/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。
~ 開頭表示區分大小寫的正則匹配
~* 開頭表示不區分大小寫的正則匹配
!~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配 的正則
/ 通用匹配,任何請求都會匹配到。
示例:
location = / { #規則A } location = /login { #規則B } location ^~ /static/ { #規則C } location ~ \.(gif|jpg|png|js|css)$ { #規則D }
blog.conf配置如下
server { listen 82; #監聽端口 server_name blog.chucloud.com; location / { root html/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /documents/{ return 403; } location ^~ /images/ { #匹配任何以/images/開頭查詢並且停止搜索。任何正則表達式匹配將不會檢查 return 404; #^~作用是常規字符串匹配之後,不做正則表達式的檢查,即如果最明確的那個字符串匹配location配置中有此前綴,那麼不會做正則表達式的檢查 } location ~* \.(gif|jpg|jpeg)$ { #匹配任何gif、jpg或者jpeg結尾的請求 return 500; } access_log logs/access_www.log main gzip buffer=32k flush=5s; }
在nginx的location和配置中location的順序沒有太大關係。正location表達式的類型有關。相同類型的表達式,字符串長的會優先匹配。
以下是按優先級排列說明:
等號類型(=)的優先級最高。一旦匹配成功,則不再查找其他匹配項。
^~類型表達式。一旦匹配成功,則不再查找其他匹配項。
正則表達式類型(~ ~*)的優先級次之。如果有多個location的正則能匹配的話,則使用正則表達式最長的那個。
常規字符串匹配類型。按前綴匹配。
3.8nginx rewrite
該功能主要是實現url地址重寫。
指令語法:
rewrite regex replacement [falg];
默認值:none
應用值:server,location,if
根據regex部分內容,重定向到replacement部分,結尾是flag標記,簡單例子如下
rewrite ^/(.*) /$1 permanent;
^/(.*) 正則表達式,表示匹配素有,匹配成功後跳轉到http://bbs.chulcoud.com/$1 ,$1參數代表括號中部分 結尾permanent是永久301重定向
正則表達式
字符 | 描述 |
\ | 將後面接着的字符標記爲一個特殊字符或一個原義字符或一個向後引用。如“\n”匹配一個換行符,而“\$”則匹配“$” |
^ | 匹配輸入字符串的起始位置 |
$ | 匹配輸入字符串的結束位置 |
* | 匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll” |
+ | 匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“oll”,但不能匹配“o” |
? | 匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,"?"等效於"{0,1}" |
. | 匹配除“\n”之外的任何單個字符,若要匹配包括“\n”在內的任意字符,請使用諸如“[.\n]”之類的模式。 |
(pattern) | 匹配括號內pattern並可以在後面獲取對應的匹配,常用$0...$9屬性獲取小括號中的匹配內容,要匹配圓括號字符需要\(Content\) |
參數flag標記說明:
last #本條規則匹配完成後,繼續向下匹配新的location URI規則
break #本條規則匹配完成即終止,不再匹配後面的任何規則
redirect #返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址
permanent #返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址
nginx rewrite 301跳轉
實現www.chucloud.com和chucloud.com訪問同一個地址
www.conf配置如下
server { listen 80; server_name chucloud.com; rewite ^/(.*) http://www.chucloud.com/$1 permanent; #當用戶訪問chucloud.com及下面任意內容的時候,都會通過這條rewrite跳轉到www.chulcoud.com對應的地址 location / { root html/www; index index.html index.htm; } access_log logs/access_www.log main gzip buffer=32k flush=5s; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
實現不同域名的url跳轉
實現http://blog.chucloud.com 跳轉到http://www.chucloud.com/blog/blog.html
blog.conf 配置文件如下
server { listen 80; server_name blog.chucloud.com; location / { root html/blog; index index.html index.htm; } if ($http_host ~* "^(.*)\.chucloud\.com$") { set $domain $1; rewrite ^(.*) http://www.chucloud.com/$domain/blog.html break; }
在/html/blog/下創建blog.html
cat >blog.html<<EOF www.chucloud.com/blog/blog.html well done EOF
檢查nginx語法重啓
可以看到重定向生效,但是瀏覽器阻止了
3.9 nginx訪問認證
網站後臺和一些關鍵網頁需要設置網頁認證,只有擁有賬號的用戶才能訪問網站內容。
配置示例
location / { auth_basic "clsed site" auth_basic_user_file conf/htpasswd; }
參數說明:
auth_basic
語法:auth_basic string | off;
默認值:auth_basic off;
使用位置:http、server、location 、limit_except
auth_basic_user_file
默認值:————
使用位置:http,server,location,limit_except
auth_basic_user_file 參數接認證密碼文件,file 內容如下
#comment name1:password1 name2:password2 name3:password3
bbs,conf 配置文件如下
server { listen 80; server_name bbs.chucloud.com; location / { root html/bbs; index index.html index.htm; auth_basic "====>chucloud<===="; auth_basic_user_file /application/nginx/conf/htpasswd; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
生成賬號密碼步驟如下
yum install httpd-tools #安裝htppasswd
創建賬號密碼
[root@nginx1 extra]# htpasswd -bc /application/nginx/conf/htpasswd yangchao 123456 Adding password for user yangchao [root@nginx1 extra]# chomd 400 /application/nginx/conf/htpasswd [root@nginx1 extra]# chmod 400 /application/nginx/conf/htpasswd [root@nginx1 extra]# chown nginx /application/nginx/conf/htpasswd
查看密碼文件,發現是加密過了的
重啓nginx生效