1.nginx介紹
1.1 nginx的特點
nginx的基本特性
- 對靜態資源高速高並發訪問及緩存
- 可使用反向代理加速,並且可進行數據緩存
- 具有簡單負載均衡、節點健康檢查和容錯功能
- 支持遠程FastCGI服務的緩存加速
- 支持FastCGI、Uwsgi、SCGI、Memcache Server的加速和緩存
- 支持SSL、TLS、SNI
- 具有模塊化的架構:過濾器包括gzip壓縮、rangs支持、chunked響應、XSLT、SSI及圖像縮放等功能。在SSI過濾中,一個包含多個SSI的頁面,如果經由FastCGI或反向代理,可被並行處理。
它所具備的其他www服務特性
- 支持基於名字、端口、ip的多虛擬主機站點
- 支持Keepalive和pipelined連接
- 可支持簡單方便靈活的配置和管理
- 支持修改nginx配置,並在代碼上線時,可平滑重啟,不中斷業務訪問
- 可自定義訪問日志格式,臨時緩沖寫日志操作,快速日志輪詢及通過rsyslog處理日志
- 可利用信號控制nginx進程
- 支持3xx-5xxHTTP狀態碼重定向
- 支持rewrite模塊,支持URI重寫及正則表達式匹配
- 支持基於客戶端ip地址和HTTP基本認證的訪問控制
- 支持PUT、DELETE、MKCOL、COPY、MOVE等特殊的HTTP請求方法
- 支持FLV流和MP4流技術產品應用
- 支持HTTP響應速率限制
- 支持同一ip地址的並發連接或請求數限制
- 支持郵件服務代理
面試時可能需要解答如下nginxHTTP服務器的特色及優點
- 支持高並發:能支持幾萬並發連接(特別是靜態小文件業務環境)
- 資源消耗少:在3萬並發連接下,開啟10個nginx線程消耗的內存不到200MB
- 可以做HTTP反向代理及加速緩存,即負載均衡功能,內置對RS節點服務器健康檢查功能,這相當於專業的Haproxy軟件或LVS的功能
- 具備Squid等專業緩存軟件等的緩存功能
- 支持異步網絡I/O時間模型epoll
apache軟件的特點
- Apache2.2版本非常穩定強大,性能好。
- prefork模式取消了進程開銷
- 處理動態業務數據時,因關聯到后端的引擎和數據庫,瓶頸不在Apache上
- 高並發時消耗資源相對多一些
- 基於傳統的select模型,高並發能力有限
- 支持擴展庫,可以通過DSO、apxs方法編譯安裝額外的插件功能,不需要重新編譯Apache
- 功能多,更穩定,更安全,插件也多
- 市場份額逐漸遞減
1.2 nginx和其他web對比(epoll和select)
處理靜態小文件(小於1Mb),nginx比Apache和Lighttpd更有優勢,處理動態文件時Apache更有優勢,但是差距不大。這是因為處理動態數據的能力取決於PHP(java)和后端數據庫的服務能力,也就是說瓶頸不在web服務器上,一般情況下普通的PHP引擎支持的並發連接參考值為300~1000,java引擎和數據庫的並發連接參考值為300~1500。
為什么nginx總體性能比Apache高
nginx使用最新的epoll和kqueue(freebsb)異步網絡I/O模型,而Apache使用的是傳統的select模型
異步的安全性、穩定性沒有同步高,中間容易被人竊取,用戶收不到數據
select和epoll的釋義:
select模型就像保姆照看一群孩子,會詢問每個孩子是否要尿尿,有就帶領去(尿尿比作網絡I/O時間,時間復雜度是O(n))
epoll就是孩子要尿尿,自己主動去規定好的地方,然后保姆帶領去,這樣保姆只需要關心那個地方有沒有孩子 (時間復雜度O(1))
指標 |
select |
epoll |
性能 |
隨着連接數的增加而急劇下降。處理成千上萬的並發連接數時,性能很差。 |
隨着連接數的增加,性能基本沒有下降,處理成千上萬的並發連接數時,性能好 |
連接數 |
連接數 有限制,處理最大連接數不超過1024.如果超過1024那么就要修改FD_SETSIZE宏,並重新編譯。 |
連接數無限制 |
內在處理機制 |
性能輪詢 |
回調callback |
開發復雜性 |
低 |
中 |
表1 Apache select和nginx epoll技術對比
1.3 如何選擇web
靜態業務:
若是高並發場景,盡量采用nginx或Lighttpd,首選nginx
動態業務:
理論上采用nginx和Apache均可,為了避免相同業務服務軟件多樣化,增加維護成本
既有動態又有靜態:
選用nginx
2.安裝nginx
安裝nginx有3種方法:
- rpm –ivh ngin*.rpm(有依賴問題)
- yum(自動解決rpm的安裝依賴問題,安裝簡單化,缺點:不能定制)
- make編譯安裝(./configure(配置),make(編譯),make install(安裝),缺點:復雜,效率低)
更換國內的yum源
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
安裝阿里雲的epol源
1、備份(如有配置其他epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
2、下載新repo 到/etc/yum.repos.d/
epel(RHEL 7)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
epel(RHEL 6)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
epel(RHEL 5)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.rep
配置安裝nginx的yum源
cd /etc/yum.repos.d/
vim nginx.repo,填寫如下內容:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/6/$basearch/ gpgcheck=0 enabled=1
2.1 編譯安裝步驟
1.安裝nginx需要的pcre庫
yum install pcre pcre-devel -y
安裝pcre庫是為了是nginx支持具備URI重寫功能的rewrite模塊,不安裝那就無法使用rewrite模塊的功能。基本上rewrite功能是企業必須的。
2. 安裝openssl-devel
[root@web01 nginx]#yum -y install openssl openssl-devel
3. 開始安裝nginx
[root@web01 nginx]#wget http://nginx.org/download/nginx-1.6.3.tar.gz [root@web01 nginx]#tar xf nginx-1.6.3.tar.gz [root@web01 nginx]#useradd nginx -M -s /sbin/nologin [root@web01 nginx]#cd nginx-1.6.3 [root@web01 nginx]#./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module [root@web01 nginx]#make [root@web01 nginx]#make install [root@web01 nginx]#ls /application/nginx-1.6.3/ conf html logs sbin [root@web01 nginx]#ln -s /application/nginx-1.6.3/ /application/nginx [root@web01 nginx]#/application/nginx/sbin/nginx
2.2 驗證安裝結果
1. 檢查語法
[root@web01 nginx]#/application/nginx/sbin/nginx -t
2. 啟動nginx服務
[root@web01 nginx]#/application/nginx/sbin/nginx
3. 查看服務對應的端口是否成功開啟
[root@web01 nginx]#netstat -tunlp|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7377/nginx [root@web01 nginx]# ps -ef|grep nginx root 7377 1 0 17:38 ? 00:00:00 nginx: master process www 7378 7377 0 17:38 ? 00:00:00 nginx: worker process /application/nginx/sbin/nginx root 7382 4710 0 17:39 pts/0 00:00:00 grep nginx [root@web01 nginx]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 7377 root 6u IPv4 22767 0t0 TCP *:http (LISTEN) nginx 7378 www 6u IPv4 22767 0t0 TCP *:http (LISTEN)
4. 結果
(1)用瀏覽器輸入10.0.0.8看到下圖就成功了
(2)wget 10.0.0.8
(3)curl -I http://nginx.org/
補充
/application/nginx/sbin/nginx -V #顯示編譯參數
/application/nginx/sbin/nginx -h #man幫助
/application/nginx/sbin/nginx -t #檢查語法
編譯安裝目錄結構
[root@localhost ~]# tree /application/nginx-1.6.3/ /usr/local/nginx ├── client_body_temp ├── conf # Nginx所有配置文件的目錄 │ ├── fastcgi.conf # fastcgi相關參數的配置文件 │ ├── fastcgi.conf.default # fastcgi.conf的原始備份文件 │ ├── fastcgi_params # fastcgi的參數文件 │ ├── fastcgi_params.default # 所有結尾為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默認站點目錄 │ ├── 50x.html #錯誤頁面優雅替代顯示文件,例如當出現502錯誤時會調用此頁面 │ └── index.html # 默認的首頁文件 ├── logs # Nginx日志目錄 │ ├── access.log # 訪問日志文件 │ ├── error.log # 錯誤日志文件 │ └── nginx.pid # pid文件,Nginx進程啟動后,會把所有進程的ID號寫到此文件 ├── proxy_temp # 臨時目錄 ├── sbin # Nginx命令目錄 │ └── nginx # Nginx的啟動命令 ├── scgi_temp # 臨時目錄 └── uwsgi_temp # 臨時目錄
yum安裝的nginx的目錄結構:
[root@10.0.0.20 ~]#rpm -qa nginx nginx-1.12.2-1.el6.ngx.x86_64 [root@10.0.0.20 ~]#rpm -ql nginx-1.12.2-1.el6.ngx.x86_64
2.3 編譯安裝nginx參數
編譯安裝nginx軟件時。可以使用./configure--help查看相關參數幫助。
--prefix #設置安裝路徑
--user=USER #進程用戶權限
--group=GROUP #進程用戶組權限
--with-http_stub_status_module #激活狀態信息
--with-http_ssl_module #激活ssl功能
2.4 Nginx http功能模塊
Nginx http 功能模塊 模塊說明
ngx_http_core_module 包括一些核心的http參數配置對應的配置為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基本訪問狀態信息等的模塊
2.5 nginx.conf
[root@web01 conf]# egrep -v '#|^$' nginx.conf worker_processes 1; #work進程的數量 events { #事件區塊開始 worker_connections 1024; #每個worker進程支持的最大連接數 } #事件區塊結束 http { #http區塊開始 include mime.types; #nginx支持的媒體類型庫文件 default_type application/octet-stream; #默認的媒體類型 sendfile on; #開啟高效的傳輸模式 keepalive_timeout 65; #連接超時 include path/*.conf; #如果server標簽過多可以摘出去,到任何路徑下 server { #第一個server區塊開始,表示一個獨立的虛擬主機站點 listen 80; #提供服務的端口,默認是80 server_name localhost; #提供服務的域名主機名 location / { #第一個location區塊開始 root html; #站點的根目錄,相當於nginx安裝目錄 index index.html index.htm; #默認的首頁文件,如果沒有首頁文件報403,報403的除了添加首頁文件的另一個解決方法,在這放置autoindex on } #第一個location區塊結束 error_page 500 502 503 504 /50x.html; #出現對應的http狀態碼時,使用50x.html回應客戶 location = /50x.html { #location區塊開始,訪問50x.html root html; #指定站點目錄html } } } #http區塊結束
3. nginx虛擬主機配置
3.1 虛擬主機的概念
所謂的虛擬主機,在web訪問里就是一個獨立的網站站點,這個站點對應獨立的域名(也可能是IP或端口),具有獨立的程序和目資源錄,可以獨立的對外提供服務供用戶訪問。
這個獨立的站點在配置里由一定的格式的標簽段標記的。在nginx軟件里是使用一個server{}標簽來標識一個虛擬主機,一個web服務里可以有多個虛擬主機,即可以支持多個虛擬主機站點。
3.2 虛擬主機類型
3.2.1 域名(應用層)
企業外網場景,通過不同的域名區分不同的虛擬主機,企業應用最廣的類型
1.配置基於域名的nginx.conf內容
[root@web01 conf]# egrep -v '#|^$' nginx.conf.default >nginx.conf [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; server { listen 80; server_name www.etiantian.org; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2.創建域名對應的站點目錄即文件
mkdir -p /application/nginx/html/www -p echo http://www.etiantian.org >/application/nginx/html/www/index.html
3.檢查語法重新加載
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
4.最后測試配置的訪問結果
echo “10.0.0.8 www.etiantian.org” >>/etc/hosts
注意:
在windows客戶端進行訪問,如果域名沒有做正是DNS解析,可在筆記本的hosts文件添加記錄
3.2.2 端口(傳輸層)
公司后台,測試場景,內部服務
這類虛擬主機主要是針對企業內部網站,一些不希望直接對外提供用戶訪問的網站后台
注意:
一般是先判斷端口號,然后判斷域名,如果端口對應上了,域名沒有找見默認去找這個端口下的第一台主機(也就是第一個server標簽)
[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; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 81; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 82; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
檢查
重啟
訪問
http://www.etiantian.org:80
http://bbs.etiantian.org:81
http://blog.etiantian.org:82
3.2.3 IP(網絡層)
網卡上增加IP
法一:
ip addr add 10.0.0.3/24 dev eth0 label eth0:3 ip addr add 10.0.0.4/24 dev eth0 label eth0:4
法二:
ifconfig eth0:0 10.0.0.101/24 up
查看結果:
[root@web01 conf]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:98:47:1B inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe98:471b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5611 errors:0 dropped:0 overruns:0 frame:0 TX packets:3963 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2142583 (2.0 MiB) TX bytes:579494 (565.9 KiB) eth0:3 Link encap:Ethernet HWaddr 00:0C:29:98:47:1B inet addr:10.0.0.3 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:4 Link encap:Ethernet HWaddr 00:0C:29:98:47:1B inet addr:10.0.0.4 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@web01 conf]# cat nginx.conf.base_port
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.8:80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 10.0.0.3:81; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 10.0.0.4:82; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
3.3 nginx狀態信息功能
也就是新添加一個server標簽打開stub_status on;,編譯安裝nginx的時候的參數--with-http_stub_status_module
[root@web01 extra]# cat extra/status.conf server { listen 80; server_name status.etiantian.org; location / { stub_status on; access_log off; } }
檢查語法,重啟服務,配置hosts解析
第一個server表示nginx啟動到現在共處理了多少個連接;
第二個accepts表示啟動到現在共成功創建了多少多少次握手
第三個handled requests表示處理了多少次請求
reading為nginx讀取到客戶端的header信息數
writing為nginx返回給客戶端的header信息數
waiting為nginx已經處理完正在等候下一次請求指令的駐留連接
3.4 nginx訪問日志
3.4.1 日志配置
[root@web01 conf]# cat >>/xxx/xxx/nginx/conf/extra/02_blog.conf<<EOF server { listen 80; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } access_log logs/access.log [format buffer=size [flush=time]] main; }
EOF
注意
1. 這行配置中的main是http標簽中配置的log_format后面的main對應的格式,可以多個格式,方便使用.
2. 配置行的[]括號內的選項是可選的,配置緩存,防止高並發的大量IO
3. format gzip[=level]
錯誤日志級別
default:error_log logs/error.log level;
關鍵配置 日志文件 錯誤日志級別[debug|info|warn|error|alert|notice|crit|emerg]
生產場景一般用warn|error|crit三個級別
標簽端的配置
main,http,server,location
3.4.2 nginx日志變量說明
日志格式
http { 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; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/*.conf; }
nginx日志變量 |
說明 |
$remote_addr |
記錄訪問網站客戶端地址 |
$remote_user |
遠程客戶端用戶名稱 |
$time_local |
訪問時間和時區 |
$request |
用戶的http請求的起始行信息 |
$status |
http狀態碼,記錄請求返回的狀態 |
$body_bytes_sent |
服務器發送給客戶端的響應body字節數 |
$http_referer |
此鏈接是從哪個連接訪問過來的 |
$http_user_agent |
記錄客戶端的訪問信息 |
$http_x_forwarded_for |
當前有代理服務器時,設置了web節點機理客戶端地址的配置 |
例:
$remote_addr 對應的10.0.0.20,客戶端的IP
$remote_user 對應的是第二個中杠,沒有遠程用戶用-填充
$time_local 對應的時間
$request 對應的是GET/HTTP/1.0
$status304 狀態碼
$body_bytes_sent 對應的是0
$http_referer 這里是-,直接打開的域名瀏覽,所以沒有值
$http_user_agent 那一長串的東西
$http_x_forwarded_for 這里是10.0.0.1
3.4.3 nginx 訪問日志切割
[root@web01 logs]# mv access.log $(date +%F -d "-1day")_access.log
[root@web01 logs]# /application/nginx/sbin/nginx -s reload
重新啟動就會重新生成access_www.log文件,然后寫定時任務。
[root@web01 scripts]# crontab -l
################time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
################
00 00 * * * /bin/sh /server/scripts/cut_log.sh >/dev/null 2>&1
cat cut_log.sh cd /application/nginx/logs &&\ mv access.log $(date +%F -d "-1day")_access.log /application/nginx/sbin/nginx -s reload
Nginx常用日志收集及分析工具有rsyslog、awstats、flume、ELK(Elasticsearch logstash Kibana)、storm等。
4. nginx location
作用:是根據用戶請求的URI來執行不同的應用
語法:location { = | ~ | ~* | ^~ } uri {
…
}
注意: = 精確匹配,~區分大小寫,~*不區分大小寫,!匹配取反,^~在常規字符串檢查之后,不做正則表達式的檢查
URI是關鍵部分,可以是普通字符串地址路徑,或者是正則表達式
location |
{ = | ~ | ~* | ^~ } |
uri |
{…} |
指令 |
匹配標識 |
匹配網站地址 |
配置URI后要執行的配置端 |
location示例:
[root@web01 extra]# cat 02_blog.conf server { listen 80; server_name blog.etiantian.org; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } }
檢查語法
重啟生效
實驗返回狀態碼:
curl -s -o /dev/null -I -w "%{http_code}\n" http://blog.etiantian.org/
curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/
參數解釋:-s靜默,-o不輸出,-I請求頭,-w "%{http_code}\n"狀態碼
順序 |
不用URI即特殊字符匹配 |
匹配說明 |
1 |
“location = / { ” |
精確匹配 |
2 |
“location ^~ /images/ { ” |
匹配常規字符串,不做正則匹配檢查 |
3 |
“location ~* \.(gif/jpg/jpeg)$ { ” |
正則匹配 |
4 |
“location /documents/ { ” |
路徑匹配 |
5 |
“location / { ” |
所有location都不能匹配后的默認匹配 |
表2 URI及特殊字符組合匹配的順序說明
5. nginx rewrite
5.1 rewrite用法
和Apache等的web服務軟件一樣,nginx rewrite的主要功能也是實現URL地址重寫,nginx的rewrite規則需要PCRE軟件的支持,通過Perl兼容正則表達式語法進行規則匹配。
rewrite指令語法:
rewrite regex replacement [flag]
默認值:none 應用位置:server、location、if
例子:rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
server標簽下:
直接rewrite ^/(.*) http://www.etiantian.org/$1permanent;
if匹配:
if ( $http_host ~* "^(.*)\.etiantian\.org$") { set $domain $1; rewrite ^(.*) http://www.etiantian.org/$domain/oldboy.html break; }
5.2 正則表達式
字符 |
描述 |
\ |
將后面的字符標記為一個特殊字符或一個后項引用,例:\\和\n匹配換行符 |
^ |
匹配輸入字符串的起始位置 |
$ |
匹配輸入字符串的結束位置 |
* |
匹配前面字符0次或多次,例:ol*匹配“o”或者“olllll” |
+ |
匹配前面字符1次或多次,例:ol*匹配“ol”或者“olllll” |
? |
匹配前面字符0次或1次 |
. |
匹配除‘\n’之外的任何單個字符 |
(pattern) |
匹配括號內得pattern,並且在后面獲取對應的匹配,會后項通過$1~n引用 |
5.2 flag標記的說明
flag標記符號 |
說明 |
last |
本條規則匹配完成后,繼續向下匹配新的location URI規則 |
break |
本條規則匹配完成即終止,不再匹配后面的任何規則 |
redirect |
返回302臨時重定向,瀏覽器地址欄會顯示跳轉后的URL地址 |
permanent |
返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址 |
rewrite ^(.*)/bbs/ h有關rewrite特殊flag標記last與break的說明:
在根location中或者server標簽中編寫rewrite規則,建議使用last標記,而在普通的location或if{}中編寫rewrite規則,則建議使用break標記。
注意:
- last和break用來URL重寫,瀏覽器的URL地址不變,
- redirect和permanent用來實現URL跳轉,瀏覽器的URL地址顯示跳轉后的地址
6. nginx 訪問認證
示例:
location / { auth_basic "oldboy training"; #這是個提示 auth_basic_user_file /application/nginx/conf/htpasswd; #認證讀取的文件 }
創建賬號密碼:此賬號就是登陸時需要的
[root@web01 extra]# rpm -qf /usr/bin/htpasswd httpd-tools-2.2.15-53.el6.centos.x86_64 [root@web01 extra]# htpasswd -cb /application/nginx/conf/htpasswd oldboy 123456 Adding password for user oldboy [root@web01 extra]# cat /application/nginx/conf/htpasswd oldboy:hkWTQUKYkqLSo
具體操作:
htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456
chmod 400 /application/nginx/conf/htpasswd
chown nginx /application/nginx/conf/htpasswd
檢查語法
重啟生效
7 本章重點回顧
- Nginx的特性優點;
- 主流Web動態靜態性能對比;
- Apache select和Nginx epoll模型區別形象比喻(面試常考);
- 虛擬主機概念及類型分類詳解;
- 基於域名和端口虛擬主機的介紹及搭建;
- Nginx錯誤及訪問日志及訪問日志切割;
- Nginx訪問狀態信息介紹及配置實踐;
- Nginx location介紹及配置實踐;
- Nginx Rewrite介紹及配置實踐;
- Nginx Web訪問認證介紹及配置實踐。
- 403報錯的原因找不到首頁文件,首頁文件權限等
更多常用開源運維工具見http://oldboy.blog.51cto.com/2561410/775056。