No.5 Nginx急速入門

nginx 的功能
http web 服務器
proxy (反向代理)
安裝
源碼安裝
./configure \
--prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-
path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-
path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-
http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --
http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-
temp-path=/var/tmp/nginx/scgi \ --with-pcre make && make install
rpm 安裝
yum install nginx
Log 配置
默認位置/var/log/nginx ,須要重定向log的位置
/etc/logrotate.d/nginx, 修改 屬組爲nginx
配置文件
主配置文本 /etc/nginx/nginx.conf
片斷配置文件 /etc/nginx/conf.d/*.confnginx

主配置文件(全局配置)
No.5 Nginx急速入門
GeoIP的用法
No.5 Nginx急速入門
虛擬主機
No.5 Nginx急速入門
基於用戶的訪問控制web

  1. 創建口令文件
    No.5 Nginx急速入門
  2. 實現認證
    No.5 Nginx急速入門
    Nginx的狀態
    No.5 Nginx急速入門
    Active connections: 22 當前 nginx 正處理的活動的鏈接數
    server accepts handled requests 27 27 434 說明: 27 nginx 從啓動到如今總共處理的鏈接數 27 nginx 從啓動
    到如今成功建立的三次握手次數(若是與前一個值相同,說明中間過程沒有失敗鏈接) 434 nginx 共處理的請求數
    (requests);以上說明nginx每次成功鏈接後都處理了多個(434/27)請求 connection 鏈接數,tcp鏈接 request http
    請求(GET/POST/DELETE/HEAD等各類方法的請求)
    Reading: 2 Writing: 1 Waiting: 19 說明: Reading: 2 nginx讀取客戶端Header的個數(nginx正在讀取客戶端請求
    首部的個數) Writing: 1 返回給客戶端的header的個數(nginx正在讀取客戶端請求主體部分的個數、正處理着其請求
    內容的請求的個數或者正在向其客戶發送響應的個數) --響應頭 Waiting: 19 等待的請求數(長鏈接模式的保持鏈接個
    數)c
    proxy代理
    一、基本反向代理
    No.5 Nginx急速入門
    其中 op 包括: = 路徑精確匹配,只包含當前路徑,不包含其它任何路徑。 ^~ 不作正則表達式元字符匹配,作逐個
    字符搜索匹配 ~* 不區分大小寫,元字符匹配 ~ 區分大小寫,元字符匹配
    例1:
    No.5 Nginx急速入門
    若是location的URI前沒有任何操做符[op],上游服務器的URL中必須以"/"結尾或明確指定URI;不然nginx會將客
    戶端輸入的URI追加到上游服務器的URL上,致使訪問異常。 若是location的URI是經過模式匹配定義的,其URI將直
    接被傳遞至上游服務器,而不能爲上游服務器指定轉換後的URI; 並且後端服務器URL中不能包含任何URI信息。
    例2:
    No.5 Nginx急速入門
    例3:
    No.5 Nginx急速入門
    二、upstream 定義後端服務器組
    Syntax: upstream name { ... } Default: — Context: http
    upstream模塊經常使用的指令有: ip_hash:基於客戶端IP地址完成請求的分發,它能夠保證來自於同一個客戶端的請
    求始終被轉發至同一個upstream服務器; keepalive:每一個worker進程爲發送到upstream服務器的鏈接所緩存的
    個數; least_conn:最少鏈接調度算法; server:定義一個upstream服務器的地址,還可包括一系列可選參數,
    如: weight:權重; max_fails:最大失敗鏈接次數,默認爲1,失敗鏈接的超時時長由fail_timeout指定;
    fail_timeout:等待請求的目標服務器發送響應的時長; backup:用於fallback的目的,全部服務均故障時才啓動
    此服務器,相似於LVS Sorry Server; down: 暫停對該服務器的調度 upstream模塊的負載均衡算法主要有三
    種,輪調(round-robin)、ip哈希(ip_hash)和最少鏈接(least_conn)三種。注:當使用ip_hash時能夠解決client
    session問題,但服務器狀態不可以使用weight和backup。 fair: 能夠根據請求頁面的大小和加載時間長短進行調
    度,使用第三方的upstream_fair模塊 url_hash: 按請求的url的hash進行調度,從而使每一個url定向到同一服務
    器,使用第三方的hash模塊 例1:
    No.5 Nginx急速入門
    例2:
    No.5 Nginx急速入門
    例3:
    No.5 Nginx急速入門No.5 Nginx急速入門

例4:
upstream模塊也能爲非http類的應用實現負載均衡,以下面的示例定義了nginx爲memcached服務實現負載均衡
之目的。
No.5 Nginx急速入門
傳送client IP
No.5 Nginx急速入門
Rewrite相關指令
Nginx Rewrite相關指令有if、rewrite、set、return等。 一、if指令 if 的語法 if (condition) { ... } 應用於server和
location環境內(if 與條件之間必須有空格) if 能夠支持以下條件判斷匹配符號 ~ 爲區分大小寫匹配 ~ 爲不區分大
小寫匹配 !~和!~
分別爲區分大小寫不匹配及不區分大小寫不匹配 -f 和!-f 用來判斷是否存在文件 -d 和!-d 用來判斷
是否存在目錄 -e 和!-e 用來判斷是否存在文件或目錄 -x 和!-x 用來判斷文件是否可執行
在匹配過程當中能夠引用一些Nginx的全局變量,更多的變量請參考 http://wiki.nginx.org/NginxHttpCoreModule
的 Variables 部分 $args 請求中的參數; $document_root 針對當前請求的根路徑設置值; $host 請求信息中
的"Host",若是請求中沒有Host行,則等於設置的服務器名; $limit_rate 對鏈接速率的限制; $request_method 請
求的方法,好比"GET"、"POST"等; $remote_addr 客戶端地址; $remote_port 客戶端端口號; $remote_user 客戶
端用戶名,認證用; $request_filename 當前請求的文件路徑名(帶root指定的路徑,即網站的主目錄)
$request_uri 當前請求的文件路徑名(不帶root指定的路徑)
與 args相同;
$scheme 所用的協議,比
如http或者是https $server_protocol 請求的協議版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr 服務器地址,如
果沒有用listen指明服務器地址,使用這個變量將發起一次系統調用以取得地址(形成資源浪費); $server_name 請
求到達的服務器名;$server_port 請求到達的服務器端口號;
與 uri同樣,URI地址;
例1 匹配訪問的url地址是不是個目錄
if (-d $request_filename) {
...;
}
例2 匹配訪問的地址是否以www開頭
if ($host ~ ^www) {
...;
}
例3 防盜鏈
No.5 Nginx急速入門
referer指令 Syntax: valid_referers none | blocked | server_names | string ...; Default: — Context: server,
location none the「Referer」 field is missing in the request header; (即直接在客戶端瀏覽器地址欄中輸入的)
blocked the「Referer」 field is present in the request header, but its value has been deleted by a firewall or
proxy server; such values are strings that do not start with 「http://」 or 「https://」; server_names the 「Referer」
request header field contains one of the server names; arbitrary string defines a server name and an optional
URI prefix. A server name can have an 「
」 at the beginning or end. During the checking, the server’s port in the
「Referer」 field is ignored; regular expression the first symbol should be a 「~」. It should be noted that an
expression will be matched against the text starting after the 「http://」 or 「https://」
Rewrite 指令 Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if flag: last stops
processing the current set of ngx_http_rewrite_module directives and starts a search for a new location
matching the changed URI; 至關於Apache裏的[L]標記,表示完成rewrite break stops processing the current set
of ngx_http_rewrite_module directives as with the break directive; 本條規則匹配完成後,終止匹配,再也不匹配後
面的規則 redirect returns a temporary redirect with the 302 code; used if a replacement string does not start
with 「http://」 or 「https://」; 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址 permanent returns a
permanent redirect with the 301 code.返回301永久重定向,瀏覽器地址會顯示跳轉後URL地址
last和break標記的區別在於,last標記在本條rewrite規則執行完後,會對其所在的server { ... } 標籤從新發起請
求,而break標記則在本條規則匹配完成後,中止匹配,再也不作後續的匹配。另有些時候必須使用last,好比在使用
alias指令時,而使用proxy_pass指令時則必須使用break。
例1:
No.5 Nginx急速入門
例2:
No.5 Nginx急速入門
例3:
No.5 Nginx急速入門正則表達式

相關文章
相關標籤/搜索