1 server { ... } 配置一個虛擬主機;
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
在/etc/nginx/conf.d/vhost.conf中定義一個虛擬主機

image
在物理主機中添加nds解析后訪問該域名,默認為80端口申眼,注意清空防火墻策略

image
2 ngx_http_access_module模塊,基于ip的訪問控制模塊

image
重載nginx的配置文件后訪問

image

image
3.ngx_http_auth_basic_module模塊實現(xiàn)基于用戶的訪問控制蝉衣,使用basic機制進行用戶認證
實現(xiàn)該功能模塊前需安裝httpd-tools包括尸,需要使用htpasswd工具。

image
htpasswd -c -m /etc/nginx/.ngxpasswd kbq-------定義一個kbq用戶可訪問病毡,使用md5加密福侈,存放于.ngxpasswd文件中奕塑。

image
4.ngx_http_stub_status_module模塊;用于輸出nginx的基本狀態(tài)信息

image

image
顯示簡述
Active connections: 活動狀態(tài)的連接數(shù);
accepts:已經(jīng)接受的客戶端請求的總數(shù);
handled:已經(jīng)處理完成的客戶端請求的總數(shù)闯两;
requests:客戶端發(fā)來的總的請求數(shù);
Reading:處于讀取客戶端請求報文首部的連接的連接數(shù)拌倍;
Writing:處于向客戶端發(fā)送響應報文過程中的連接數(shù)勋又;
Waiting:處于等待客戶端發(fā)出請求的空閑連接數(shù);
5.ngx_http_log_module模塊八拱,必須在1.10版本之后才能使用

image
6.ngx_http_gzip_module:
The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.
1阵赠、gzip on | off;
Enables or disables gzipping of responses.
2、gzip_comp_level level;
Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.
3肌稻、 gzip_disable regex ...;
Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.
4清蚀、 gzip_min_length length;
啟用壓縮功能的響應報文大小閾值;
5爹谭、gzip_buffers number size;
支持實現(xiàn)壓縮功能時為其配置的緩沖區(qū)數(shù)量及每個緩存區(qū)的大屑闲啊;
6诺凡、gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
nginx作為代理服務器接收到從被代理服務器發(fā)送的響應報文后东揣,在何種條件下啟用壓縮功能的药薯;
off:對代理的請求不啟用
no-cache, no-store,private:表示從被代理服務器收到的響應報文首部的Cache-Control的值為此三者中任何一個救斑,則啟用壓縮功能童本;
7、gzip_types mime-type ...;
壓縮過濾器脸候,僅對此處設定的MIME類型的內(nèi)容啟用壓縮功能穷娱;
示例:
gzip on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_proxied any;
gzip_types text/xml text/css application/javascript;
7.ngx_http_ssl_module模塊

image

image
8.ngx_http_rewrite_module模塊:將用戶請求的URI基于regex所描述的模式進行檢查,而后完成替換运沦;

image

image
自動跳轉(zhuǎn)為:

image
1泵额、rewrite regex replacement [flag]
將用戶請求的URI基于regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI携添;
注意:如果在同一級配置塊中存在多個rewrite規(guī)則嫁盲,那么會自下而下逐個檢查;被某條件規(guī)則替換完成后烈掠,會重新一輪的替換檢查羞秤,因此,隱含有循環(huán)機制左敌;[flag]所表示的標志位用于控制此循環(huán)機制瘾蛋;
如果replacement是以http://或https://開頭,則替換結果會直接以重向返回給客戶端矫限;
301:永久重定向哺哼;
[flag]:
last:重寫完成后停止對當前URI在當前l(fā)ocation中后續(xù)的其它重寫操作,而后對新的URI啟動新一輪重寫檢查叼风;提前重啟新一輪循環(huán)取董;
break:重寫完成后停止對當前URI在當前l(fā)ocation中后續(xù)的其它重寫操作,而后直接跳轉(zhuǎn)至重寫規(guī)則配置塊之后的其它配置无宿;結束循環(huán)茵汰;
redirect:重寫完成后以臨時重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發(fā)起請求懈贺;不能以http://或https://開頭经窖;
permanent:重寫完成后以永久重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發(fā)起請求梭灿;
##
2画侣、return
return code [text];
return code URL;
return URL;
Stops processing and returns the specified code to a client.
3、 rewrite_log on | off;
是否開啟重寫日志堡妒;
4配乱、 if (condition) { ... }
引入一個新的配置上下文 ;條件滿足時,執(zhí)行配置塊中的配置指令搬泥;server, location桑寨;
condition:
比較操作符:
==
!=
~:模式匹配,區(qū)分字符大小寫忿檩;
~*:模式匹配尉尾,不區(qū)分字符大小寫;
!~:模式不匹配燥透,區(qū)分字符大小寫沙咏;
!~*:模式不匹配,不區(qū)分字符大小寫班套;
文件及目錄存在性判斷:
-e, !-e
-f, !-f
-d, !-d
-x, !-x
5肢藐、set $variable value;
用戶自定義變量 ;
9.ngx_http_referer_module模塊
The ngx_http_referer_module module is used to block access to a site for requests with invalid values in the “Referer” header field.
1吱韭、valid_referers none | blocked | server_names | string ...;
定義referer首部的合法可用值吆豹;
none:請求報文首部沒有referer首部;
blocked:請求報文的referer首部沒有值理盆;
server_names:參數(shù)痘煤,其可以有值作為主機名或主機名模式;
arbitrary_string:直接字符串熏挎,但可使用*作通配符速勇;
regular expression:被指定的正則表達式模式匹配到的字符串;要使用~打頭坎拐,例如 ~.*\.magedu\.com;
配置示例:
valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;
if($invalid_referer) {
return http://www.magedu.com/invalid.jpg;
}
10.ngx_http_proxy_module模塊:

image
proxy_pass只能置于lication中

image

image

image
proxy_cache_path:

image
11.ngx_http_headers_module模塊
The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
向由代理服務器響應給客戶端的響應報文添加自定義首部养匈,或修改指定首部的值哼勇;
1、add_header name value [always];
添加自定義首部呕乎;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
2积担、expires [modified] time;
expires epoch | max | off;
用于定義Expire或Cache-Control首部的值;
-
PHP 腳本請求全部轉(zhuǎn)發(fā)到 FastCGI處理. 使用FastCGI默認配置.
location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; }
設定查看Nginx狀態(tài)的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
禁止訪問 .htxxx 文件
location ~ /\.ht {
deny all;
}
}
}