Nginx系列之三常用模块

##一、ngx_http_access_module模块

功能:实现基于IP的访问控制功能;

配置指令:

allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;

示例:

仅允许192.168.1.0/24网段内主机访问,但是拒绝192.168.1.196访问;

server {
    listen 80;
    server_name 192.168.1.141;
    index index.html index.htm;
    access_log /data/logs/daemon_access.log;

    location / {
        root /data/site/daemon;
        deny 192.168.1.196;
        allow 192.168.1.0/24;
        deny all;
    }
}

在192.168.1.196服务器访问192.168.1.141测试能否访问; 输入图片说明 由于配置了deny所以出现403拒绝访问;使用192.168.1.0网段内其它主机测试如下图:

输入图片说明 输入正确的用户名和密码后可以访问页面内容;

##三、ngx_http_stub_status_module模块

功能:

用于输出nginx的基本状态信息;依赖于安装时的编译参数–with-http_stub_satus_module; 配置格式:

location /nginx_status {
    stub_status;
}

示例:

server {
    listen 80;
    server_name 192.168.1.141;
    index index.html index.htm;
    access_log /data/logs/daemon_access.log;

    location / {
        root /data/site/daemon;
        deny 192.168.1.196;
        allow 192.168.1.0/24;
        deny all;
    }

    location /admin/ {
        root /data/site/daemon;
        auth_basic "Enter username and password.";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    # nginx状态配置
    location /nginx_status {
        stub_status;
    }
}

访问http://192.168.1.141/nginx_stauts结果如下:

输入图片说明

status页面内容解释:

  • Active connections:处于活动状态的客户端连接的数量;
  • accepts:已经接受的客户端连接的总数;
  • handled:已经处理完成的客户端请求的总数;
  • requests:客户端发来的总的请求书;
  • Reading:处于读取客户端请求报文首部的连接数量;
  • Writing:处于向客户端发送响应报文过程中的连接数;
  • Waiting:处于等待客户端发出请求的空闲连接数;

##四、ngx_http_referer_module模块

功能:实现网站防盗链功能;

配置指令:

valid_referers  none | blocked | server_names | string …;
  • none:请求报文首部没有referer首部;
  • blocked:请求报文的referer首部没有值;
  • server_names:其值是主机名;
  • arbitrary sering:直接字符串,可以使用*作为通配符;
  • regular expression:被指定的正则表达是模式匹配到的字符串;要使用~起始; 示例:
server {
    listen 80;
    server_name 192.168.1.141;
    index index.html index.htm;
    access_log /data/logs/daemon_access.log;
    root /data/site/daemon;
    location / {
        root /data/site/daemon;
        deny 192.168.1.196;
        allow 192.168.1.0/24;
        deny all;
    }
    location /admin/ {
        root /data/site/daemon;
        auth_basic "Enter username and password.";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
    location /nginx_status {
        stub_status;
    }
    location ~* \.(gif|jpg|png|)$ {
        valid_referers none blocked server_names *.codegreen.com codegreen.* ~\.codegreen\.;
        if ($invalid_referer) {
            return 403;
        }
    }
}

上面示例的意思是,如果客户端请求不是从.codegreen.com,codegreen.,或者包含codegreen字符串的其中任意一个主机,都返回403。

重载nginx服务后通过在命令行使用curl命令测试:

# 正常访问测试返回状态为200
[root@Bj-1-141 conf.d]# curl -I  http://192.168.1.141/arch.jpg
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Fri, 23 Jun 2017 08:58:05 GMT
Content-Type: image/jpeg
Content-Length: 221528
Last-Modified: Sat, 04 Mar 2017 02:48:33 GMT
Connection: keep-alive
ETag: "58ba2b01-36158"
Accept-Ranges: bytes
# 模拟referer从www.test.com请求过来的返回403错误
[root@Bj-1-141 conf.d]# curl -I -e http://www.test.com http://192.168.1.141/arch.jpg
HTTP/1.1 403 Forbidden
Server: nginx/1.10.2
Date: Fri, 23 Jun 2017 09:00:40 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

You have new mail in /var/spool/mail/root

##五、ngx_http_ssl_module模块

  • Syntax: ssl on | off;
  • Default: ssl off;
  • Context: http, server

是否启用当前虚拟主机的ssl功能;

  • Syntax: ssl_certificate file;
  • Default: —
  • Context: http, server

当前虚拟主机使用PEM格式的证书文件;

  • Syntax: ssl_certificate_key file;
  • Default: —
  • Context: http, server

当前虚拟主机使用的证书文件中的公钥配对儿的私钥文件路径,PEM格式;

  • Syntax: ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
  • Default: ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  • Context: http, server

SSL协议的版本;

  • Syntax: ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
  • Default: ssl_session_cache none;
  • Context: http, server

指明ssl会话缓存机制

  • builtin:使用openssl内建的缓存机制,此为worker独有;
  • shared:由各worker共享的;
  • name:缓存空间的名称;
  • size:字节为单位的缓存空间的大小;每1MB内存空间可缓存4000个会话;
  • Syntax: ssl_session_timeout time;
  • Default: ssl_session_timeout 5m;
  • Context: http, server ssl会话超时时长,指ssl session cache中缓存条目有效时长;

配置示例:

server { listen 443 ssl; server_name www.enzhi.com;

ssl_certificate      /etc/nginx/ssl/nginx.crt;
ssl_certificate_key  /etc/nginx/ssl/nginx.key;

ssl_session_cache    shared:SSL:10m;
ssl_session_timeout  5m;

ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers  on;

location / {
    root    /data/site/enzhi.com/;
    index  index.html index.htm;
}

}

##六、ngx_http_log_module模块

###1、access_log指令

Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_except # buffer=size:设置缓存区大小; # gzip=level:设置压缩等级; # flush-time:保存在缓存区的时长;

###2、log_format指令

Syntax: log_format name [escape=default|json] string ...; Default: log_format combined "..."; Context: http

name:表示格式名称,类似于定义一个跟httpd日志定义中combined

string:表示定义的格式

log_format有一个默认的无需设置的combined日志格式,相当于httpd的combined日志格式,如下所示:

log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';

###3、open_log_file_cache指令

Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; Default: open_log_file_cache off; Context: http, server, location

max=N:最大缓存条目;

inactive=time:非活动时长;

min_uses=N:最少使用次数;

valid=time:验证缓存条目有效性的频率;

配置示例:

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;
open_log_file_cache max=1000 inactive=10s min_uses=2 valid=1m;
##七、ngx_http_rewrite_module模块

###1、rewrite指令

Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if

[flag]:
- last:重写完成后停止对当前URI在当前location中的后续其它重写操作,改为对新URI的新一轮处理;
- break:重写完成后停止对当前URI在当前location中的后续其它重写操作;
- redirect:重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端对新URL进行请求;302
- permanent:重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端对新URL进行请求;301

###2、rewrite_log指令
是否启用重写日志,启用时,日志信息被发往error_log;

Syntax: rewrite_log on | off; Default: rewrite_log off; Context: http, server, location, if

###3、if指令
条件判断机制,在条件满足时,执行配置块中的配置;

Syntax: if (condition) { ... } Default: — Context: server, location

condition:

比较表达式:
    ==,!=:是否相等,是否不相等;
    ~:模式匹配,区分字母大小写;
    ~*:模式匹配,不区分字母大小写;
    !~:模式不匹配,区分字母大小写;
    !~*:模式不匹配,不区分字母大小写;
文件及目录存在性判断:
    -f,!-f:文件是否存在;
    -d,!-d:目录是否存在;
    -e,!-e:文件、目录、符号链接是否存在;
    -x,!-x:文件是否可执行;
###4、return指令

Syntax: return code [text]; return code URL; return URL; Default: — Context: server, location, if

###5、set指令

用户自定义变量;

Syntax: set $variable value; Default: — Context: server, location, if

配置示例:

location / { root html; index index.html index.htm; # 如果URI中请求的是以(.).txt结尾的文件,全部重定向至(.)所匹配到的字符串命名的html文件中 rewrite (.).txt$ $1.html permanent; # 如果用户请求的URI中包含admin字符串就返回403状态; if ($uri ~ .admin.) { return 403; } }

##八、ngx_http_gzip_module模块

过滤器,对执行类型的资源压缩传输以节约带宽;
###1、gzip指令
是否启用gzip压缩响应报文;

Syntax: gzip on | off; Default: gzip off; Context: http, server, location, if in location

###2、gzip_comp_level指令
指定压缩比:1-9,默认为1;

Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http, server, location

###3、gzip_disable指令
对匹配到的客户端浏览器不执行压缩响应;

Syntax: gzip_disable regex ...; Default: — Context: http, server, location

regex:是匹配客户端浏览器类型的模式,表示对匹配到的客户端浏览器不执行压缩响应;

###4、gzip_min_length指令
触发启用压缩功能的响应报文的最小长度;单位是字节;

Syntax: gzip_min_length length; Default: gzip_min_length 20; Context: http, server, location

###5、gzip_http_version指令
设定启用压缩响应功能时,协议的最小版本;

Syntax: gzip_http_version 1.0 | 1.1; Default: gzip_http_version 1.1; Context: http, server, location

###6、gzip_types指令
指定仅执行压缩的资源内容类型;默认为text/html;

Syntax: gzip_types mime-type ...; Default: gzip_types text/html; Context: http, server, location

###7、gzip_proxied指令
对代理的请求基于何种属性判断其是否应该启用压缩功能;

Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; Default: gzip_proxied off; Context: http, server, location

配置示例:

gzip on; gzip_http_version 1.0; gzip_comp_level 6; gzip_disable msie6; gzip_min_length 1024; gzip_types text/plain text/css text/xml application/x-javascript application/xml application/json;

##九、ngx_http_fastcgi_module模块配置
Nginx与PHP结合仅能通过fastcgi方式;编译PHP时支持fpm:—enable-fpm
###1、fastcgi_pass指令
配置格式:

Syntax: fastcgi_pass address; Default: — Context: location, if in location

address:是php-fpm服务监听的地址和端口;

示例:

fastcgi_pass localhost:9000;

###2、fastcgi_index指令
指定fastcgi应用的主页名称;

配置格式:

Syntax: fastcgi_index name; Default: — Context: http, server, location

示例:

fastcgi_index index.php;

###3、fastcgi_param指令
传递给php-fpm服务器的参数及其值;

配置格式:

Syntax: fastcgi_param parameter value [if_not_empty]; Default: — Context: http, server, location

示例:

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;

###4、fastcgi_cache_path指令
设定fastcgi_cache的缓存目录;
配置格式:

Syntax: 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]; Default: — Context: http

path:文件系统路径,用于存储缓存的文件数据;

levels=levels:设置目录层级,比如1:2会生成16*256个字目录;

keys_zone=name:size:name表示定义一个缓存空间的名字;size表示使用多大的内存空间;

inactive=time:表示非活动时长;

max_size=size:定义此路径下的多大空间用于存储缓存数据;

注意:fastcgi_cache_path只能用于http上下文。

配置示例:

fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=cache_zone:10m;

###5、fastcgi_cache指令
是否启用cache,如果启用,数据缓存在哪个cache中;也就是指明用哪个缓存空间;由fastcgi_cache_path指令中keys_zone指明的name的名称;

Syntax: fastcgi_cache zone | off; Default: fastcgi_cache off; Context: http, server, location

配置示例:

fastcgi_cache cache_zone;

###6、fastcgi_cache_key指令
定义fastcgi_cache的key,示例中就以请求的URI作为缓存的key,Nginx会取这个key的md5作为缓存文件,如果设置了缓存哈希目录,Nginx会从后往前取相应的位数做为目录

配置格式:

Syntax: fastcgi_cache_key string; Default: — Context: http, server, location

配置示例:

fastcgi_cache_key $request_uri;

###7、fastcgi_cache_methods指令
缓存哪些类型的请求的相关数据;

配置格式:

Syntax: fastcgi_cache_methods GET | HEAD | POST ...; Default: fastcgi_cache_methods GET HEAD; Context: http, server, location 通常此参数可以不用指定;

8、fastcgi_cache_min_uses指令
在inactive定义的非活动时长内缓存最小使用次数;

配置格式:

Syntax: fastcgi_cache_min_uses number; Default: fastcgi_cache_min_uses 1; Context: http, server, location

###9、fastcgi_cache_valid指令
对不同响应码设定其可缓存时长;不加此参数默认不缓存任何数据;
配置格式:

Syntax: fastcgi_cache_valid [code ...] time; Default: — Context: http, server, location 配置示例:响应码是200和302缓存10分钟,404缓存1分钟;

fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 404 1m;

###10、完整配置示例

配置在http配置段里

fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=cache_zone:10m;

配置在server配置段里

location ~ .php$ { root html; fastcgi_cache cache_zone; fastcgi_cache_key $request_uri; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 404 1m; 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://my.oschina.net/jiangshanlinux/blog/1514446

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值