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

image
在物理主機(jī)中添加nds解析后訪問該域名,默認(rèn)為80端口,注意清空防火墻策略

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

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

image

image
3.ngx_http_auth_basic_module模塊實現(xiàn)基于用戶的訪問控制,使用basic機(jī)制進(jìn)行用戶認(rèn)證
實現(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ā)送響應(yīng)報文過程中的連接數(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;
啟用壓縮功能的響應(yīng)報文大小閾值;
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作為代理服務(wù)器接收到從被代理服務(wù)器發(fā)送的響應(yīng)報文后,在何種條件下啟用壓縮功能的;
off:對代理的請求不啟用
no-cache, no-store,private:表示從被代理服務(wù)器收到的響應(yīng)報文首部的Cache-Control的值為此三者中任何一個,則啟用壓縮功能;
7、gzip_types mime-type ...;
壓縮過濾器,僅對此處設(shè)定的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所描述的模式進(jìn)行檢查,而后完成替換;

image

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

image
1、rewrite regex replacement [flag]
將用戶請求的URI基于regex所描述的模式進(jìn)行檢查,匹配到時將其替換為replacement指定的新的URI;
注意:如果在同一級配置塊中存在多個rewrite規(guī)則,那么會自下而下逐個檢查;被某條件規(guī)則替換完成后,會重新一輪的替換檢查,因此,隱含有循環(huán)機(jī)制;[flag]所表示的標(biāo)志位用于控制此循環(huán)機(jī)制;
如果replacement是以http://或https://開頭,則替換結(jié)果會直接以重向返回給客戶端;
301:永久重定向;
[flag]:
last:重寫完成后停止對當(dāng)前URI在當(dāng)前l(fā)ocation中后續(xù)的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重啟新一輪循環(huán);
break:重寫完成后停止對當(dāng)前URI在當(dāng)前l(fā)ocation中后續(xù)的其它重寫操作,而后直接跳轉(zhuǎn)至重寫規(guī)則配置塊之后的其它配置;結(jié)束循環(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ù),其可以有值作為主機(jī)名或主機(jī)名模式;
arbitrary_string:直接字符串,但可使用*作通配符;
regular expression:被指定的正則表達(dá)式模式匹配到的字符串;要使用~打頭,例如 ~.*\.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.
向由代理服務(wù)器響應(yīng)給客戶端的響應(yīng)報文添加自定義首部,或修改指定首部的值;
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默認(rèn)配置.
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; }
設(shè)定查看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;
}
}
}