码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx详解(四)模块

时间:2018-03-13 23:52:09      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:nginx   https   fastcgi   

一、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&gt;<br/http://www.nginx.cn/doc/&gt;<br/<http://nginx.org/en/docs/>;

Nginx详解(四)模块

标签:nginx   https   fastcgi   

原文地址:http://blog.51cto.com/hmtk520/2086268

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!