一 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/>;
来源: http://www.bubuko.com/infodetail-2526644.html