Nginx詳解(四)模塊
二、Nginx之log模塊
三、Ning之gzip模塊
四、Nginx之https服務
五、Nginx之fastCGI模塊
一、配置Nginx提供目錄瀏覽功能
1.修改nginx配置文件
server { listen 80; server_name www.nginx.com; location / { autoindex on; autoindex_exact_size on; autoindex_localtime on; root /data/www; index 123.html; } }
2.瀏覽器打開驗證
autoindex_exact_size off;默認為on,顯示出文件的確切大小,單位是bytes。改為off後,顯示出文件的大概大小,單位是kB或者MB或者GB。
autoindex_localtime on;默認為off,顯示的文件時間為GMT時間。改為on後,顯示的文件時間為文件的服務器時間。
二、Nginx之log模塊
ngx_http_log_module
1.access_log path [format [buffer=size] [gzip[=level]] [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]]; access_log off //關閉 例如:access_log logs/access.log combined;
2.log_format name [escape=default|json] string ...;
//定義日誌格式,是nginx內建的
$bytes_sent //發送字節數
$connection //連接序列號
$connection_requests //連接請求
$msec //毫秒解析
$pipe //管道
$request_length //請求長度
$request_time //請求時間
$status //狀態
$time_iso8601 //日期格式
$time_local //本地時間
http://nginx.org/en/docs/http/ngx_http_core_module.html
//nginx有內置的log_format,不需要自定義變量進行使用
例如:log_format combined ‘$remote_addr - $remote_user [$time_local] ‘
‘"$request" $status $body_bytes_sent ‘
‘"$http_referer" "$http_user_agent"‘;
3.open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
//加快日誌查找,緩存日誌
open_log_file_cache off; //日誌文件需要大量寫入,頻繁查找並打開浪費大量資源,必要時開啟
max:緩存多少條目
inactive:失效時間
min_uses:在指定時間內最少使用的次數
valid:多長時間檢查一次有效性
off:關閉
三、Ning之gzip模塊
ngx_http_gzip_module //顧慮器,對指定類型的資源壓縮以節約資源
1.gzip on|off //是否啟用,並不是所有的瀏覽器都支持壓縮
gzip_buffers
2.gzip_comp_level //默認級別為1,
3.gzip_disable regex.. //匹配瀏覽器類型,對其不進行壓縮
msie6
MSIE [4-6].
4.gzip_min_length LENGTH //觸發啟用壓縮功能的響應報文的最小長度
5.gzip_http_version 1.0|1.1 //設定啟用壓縮響應功能時,http協議版本最小版本
6.gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...
對代理的請求基於何種屬性判斷其是否應該啟用壓縮功能;
off:所有代理請求都不壓縮
expired:
no-cache:
7.gzip_types MIME-TYPE //指定僅執行壓縮的資源內容類型,默認為text/html;
示例:
gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_disable msie6;
gzip_min_length 2;
gzip_types txt/plain txt/xml application/json application/java-script;
四、Nginx之https服務
1.ngx_http_ssl_module模塊簡介
ssl
ssl:位於傳輸層和應用層之間的半層
1.建立tcp三次會話
2.s發送證書給客戶端,以及s支持的各種加密算法
3.驗證{s證書有效性,受信任的CA頒發的,證書主體,證書完整性}對比特征碼,並發送給s自己支持的算法
4.從證書中獲取的是對方的公鑰,公鑰加密算法性能不佳,因此進行密鑰交換,DH算法,IKE算法等
為了防止別人插入廣告:做全棧https
ssL通過握手建立鏈接
ssl拆除:在tcp斷開之前
ssl建立:在tcp建立鏈接之後 //一般ssl:建立鏈接對cpu壓力很大
ngx_http_ssl_module
1.ssl on | off; //是否啟用
2.ssl_certificate file; //pem格式,本地證書
3.ssl_certificate_key file; //私鑰pem格式
//Context http,server
c-->請求https服務,發送http請求
註:在tcp層和ssl層使用的都是基於ip的通信,沒有用到FQDN
域名只有在http請求報文的首部的時候會使用到,而http請求報文實在ssl內部完成的,
也就是說在http會話的時候首部的主機名,基本都沒有發生作用
ssl建立的過程中,雙方身份的識別,是基於ip地址進行的
因此:單ip只提供一個https虛擬主機
4.ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
//ssl v2已經不安全了,因此建議使用tls,v1.1,v1.2版本
5.ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
指定ssl緩存機制,應該開啟,提高性能
none:委婉的拒絕或同意
builtin:使用openssl庫內建的緩存機制,每個worker之間的緩存是不共享的
worker獨立自主管理session,命中率不高{進程第一次被第一個worker服務,第二次可能被第二個服務}
shared:各worker共享的緩存,由nginx進程管理該空間,默認單位bytes,1M大多存儲4000個會話,cache名字相同可以被多個server共用
name:緩存空間的名稱
例如:ssl_session_cache builtin:1000 shared:SSL:10m;
6.ssl_ciphers ciphers
Default:ssl_ciphers HIGH:!aNULL:!MD5; //openssl所支持的加密算法
ssl_ciphers ALL:!aNULL:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
openssl ciphers //獲取ssl支持的算法
7.ssl_session_timeout time;//重新使用cache中的session
ssl會話超時時長,指ssl session cache中緩存條目的緩存時長
ssl_session_timeout 5m; //默認5min,調大有助於提高服務器性能
8.ssl_prefer_server_ciphers on|off //server傾向於
sslv3和TLS,優先使用server端使用的加密算法
2.配置實現
實驗1:配置ssl實現 //自己同時兼任nginx和ca
1.nginx server:
cd /etc/nginx/ssl/
(umask 077;openssl genrsa -out nginx.key 2048)
openssl req -new -key nginx.key -out nginx.csr -days 365 //生成請求和自簽用的都是-key
//www.mt.com
2.CA端
cd /etc/pki/CA/
(umask 077;openssl genrsa -out private/cakey.pem 2048)
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
//ca.mt.com
touch serial index.txt
echo 01 > serial
cd /etc/nginx/ssl
openssl ca -in nginx.csr -out nginx.crt -days 365
nginx -t
ningx -s reload
//雖然不受信任,但是通訊過程式加密的
3.配置文件
server {
listen 443 ssl;
server_name www.mt.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root /web/www/;
index index.html index.htm;
location /status {
stub_status;
}
}
五、Nginx之fastCGI模塊
1.LNMP概述
LAMP(fpm):
httpd+php:
modules;
cgi:
fcgi:
proxy_fastcgi_module
lNMP:
ngx_http_fastcgi_module
nginx+php: //只有一種選擇
nginx+fastcgi
MySQL的連接器有多個:c,php等各不相同
clinet==>nginx--->fpm--->MySQL
php:編譯時,支持fpm;./configure --enable-fpm
php-fpm工作方式,類似於httpd的prefork
listen = 127.0.0.1:9000
listen.allow_clients = //允許訪問的主機
pm = dynamic|static
pm.start_servers:啟動fpm進程時啟動的工作進程數量
pm.min_spare_server:最小空閑進程數
pm.max_spare_servers:最大空閑進程數
pm.max_children:最大工作進程數
user = USERNAME
group = GROUPNAME
2.ngx_http_fastcgi_module
yum info php-fpm
yum install php-fpm
yum install php-mysql php-mbstring php-gd php-xml -y
vim /etc/php-fpm.d/www.conf
systemctl start php-fpm
1.vim nginx.conf //動靜分離案例
location ~ \.php$ {
root /web/www/;
fastcgi_pass 127.0.0.1:9000; //傳送給誰
fastcgi_index index.php; //主頁
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; //把後面的參數保存在SCRIPT_FILENAME這個變量中
include fastcgi_params; /etc/nginx/fastcgi_params該文件中定義了很多params
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
root /usr/share/nginx/html;
#cache
expires 3d;
}
}
2.mkdir -pv /usr/local/nginx/html //php文件真實位置
3.https://www.mt.com/info.php //
對應SCRIPT_FILENAME:/usr/local/nginx/html/info.php
$fastcgi_script_name對應的是php.info
然後發送給127.0.0.1:9000
模塊指令:
1.fastcgi_pass address: //address是fpm服務器監聽的地址和端口
fastcgi_pass localhost:9000;
2.fastcgi_index index.php;
3.fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
4.fastcgi_cache zone |off //是否啟用cache,如果啟用 ,數據使用哪個緩存空間名稱
5.fastcgi_cache_path path [levels=levels] [use_temp_path=on|off]
keys_zone=name:size [inactive=time] [max_size=size]
[manager_files=number] [manager_sleep=time] [manager_threshold=time]
[loader_files=number] [loader_sleep=time] [loader_threshold=time]
[purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
//定義緩存空間,以及如何存儲數據,Context:http
path:文件系統路徑,用於存放緩存的文件數據
levels=#[:#][:#]層級,最多三級結構 // levels=1:2
keys_zone=name:size //定義內存中用於緩存kv映射關系的空間名稱及大小
inactive=time:非活動時間
max_size=size:緩存空間上限
例如:fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fcgicache:10m;
fastcgi_cache fcgicache
註:只能用於http
6.fastcgi_cache_key 127.0.0.1:9000$request_uri //把什麽當做key,緩存到內存中
//定義要使用的緩存鍵key,例如 fastcgi_cache $request_uri
7.fastcgi_cache_methods GET|HEAD|POST..; //緩存哪些類型的請求
8.fastcgi_cache_min_uses number//最少使用次數
9.fastcgi_cache_valid [code..] time;//哪一種響應碼,緩存多長時間
默認不給緩存,對不同響應碼,設定其緩存
fastcgi_cache_valid 200 302 10m;
nginx有緩存,fpm也有緩存
nginx如何為fpm緩存
key-value //nginx緩存的是元數據,以及目錄結構,錯誤查找結果等
N級子目錄: 2 1 1//一級目錄256個,每一級目錄16個子目錄,每二級目錄16個子目錄
key:中保存的是文件的路徑信息
例如基於md5提取校驗碼後存放的,分級存放,加快查找速度
實驗:fcgi緩存功能的實現
註:調用緩存時,至少應該制定三個參數
fastcgi_cache //指定緩存空間名稱
fastcgi_cache_key //把什麽當做key
fastcgi_cache_valid //緩存的對象,根據響應碼
vim /etc/nginx/nginx.conf
http {
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fcgicache:10m; //緩存的對應fs
server {
location ~ \.php$ {
root /web/www/;
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 300 10m;
fastcgi_cache_valid 301 1h;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
https://www.mt.com/info.php
ls /var/cache/nginx/fastcgi //會有很多目錄,緩存
參考博客:
http://blog.51cto.com/freeloda/1288553><br/http://www.nginx.cn/doc/><br/<http://nginx.org/en/docs/>
Nginx詳解(四)模塊