Nginx常用模块

1、Nginx目录索引

syntax:autoindex on | off;

Default: autoinde off;

Context: http,server,location

autoindex_exact_size off;

默认值on,显示出文件的确切大小,单位是bytes

修改为off,显示出文件的大概大小,单位是MB或者GB。

autoindex_localtime on;

默认值off,显示的文件时间为GMT时间。

修改为on,显示的文件时间为服务器的时间。

charset utf-8,gbk;

默认中文目录乱码,添加上解决乱码。

Examples:

[root@web02 conf.d]# vim /etc/nginx/conf.d/nfzl.conf

server {

        listen 80;

        server_name nfzl.com;

        location / {

                root /code;

                index index.html;

        }

        location /download {

                root /code;

                autoindex on;

                charset utf-8,gbk;

                autoindex_exact_size off;

                autoindex_localtime on;

        }

}

2、Nginx状态监控

1)ngx_http_stub_status_moduld用于展示Nginx连接状态信息,需要--with-http_stub_status_module模块支持

Syntax: stub_status;

Default: -

Context:server,location

2) 配置Nginx status

location /nginx_status {

stub_status;

access_log off;

}

3) 使用浏览器访问http://IP/nginx_status访问后得到的结果

Active connections: 2

server accepts handled requests

2             2         2

Reading: 0 Writing: 1 Waiting: 1

Active connections: 2  #当前活动的连接数

accepts  2           #当前的总连接数TCP

handled             #成功的连接数TCP

requests            #总的http请求数

注意:如果使用systemctl restart nginx 会清空连接数

3、Nginx访问控制

基于IP的访问控制http_access_modul

基于用户登录认证http_auth_basic_modul

1)Nginx基于IP的访问控制

//允许配置语法

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

Default: -

Context: http,server,location,limit——except

//拒绝配置语法

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

Default: -

Context: http,server,location,limit——except

Examples:

示例1、拒绝192.168.1.218访问download,其他全部允许

[root@web02 conf.d]# cat nfzl.conf

server {

        listen 80;

        server_name 192.168.1.174;

        location / {

                root /code;

                index index.html;

        }

        location /nginx_status {

                stub_status;

                access_log off;

                allow 192.168.1.218;

                deny all;

        }

        location /download {

                root /code;

                autoindex on;

                charset utf-8,gbk;

                autoindex_exact_size off;

                autoindex_localtime on;

                deny 192.168.1.218;

                allow all;

        }

}

示例2、只允许192.168.1.218访问nginx_status,其他全部拒绝

[root@web02 conf.d]# cat nfzl.conf

server {

        listen 80;

        server_name 192.168.1.174;

        location / {

                root /code;

                index index.html;

        }

        location /nginx_status {

                stub_status;

                access_log off;

                allow 192.168.1.218;

                deny all;

        }

        location /download {

                root /code;

                autoindex on;

                charset utf-8,gbk;

                autoindex_exact_size off;

                autoindex_localtime on;

                deny 192.168.1.218;

                allow all;

        }

}

2) 基于用户登录认证http_auth_basic_modul

//配置语法

Syntax:auth_basic string| off

Default: auth_basic off;

Context: http,server,location,limit——except

//用户密码记录配置文件

Syntax:auth_basic_user_file file;

Default: -

Context: http,server,location,limit——except

//需要安装依赖组件

[root@web02 ~]#yum install httpd-tools

[root@web02 ~]# htpasswd -b -c /etc/nginx/auth_conf superman talent  #用户名superman,密码talent

//可在httpd,server,location下添加如下信息

auth_basic "Auth access Download Input your Passwd!";

auth_basic_user_file /etc/nginx/auth_conf;

Examples:

[root@web02 conf.d]# cat nfzl.conf

server {

        listen 80;

        server_name 192.168.1.174;

        location / {

                root /code;

                index index.html;

        }

        location /nginx_status {

                stub_status;

                access_log off;

                allow 192.168.1.218;

                deny all;

        }

        location /download {

                root /code;

                autoindex on;

                charset utf-8,gbk;

                autoindex_exact_size off;

                autoindex_localtime on;

                auth_basic "Auth access Download Input your Passwd!";

                auth_basic_user_file /etc/nginx/auth_conf;

        }

}

4、Nginx访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个IP的连接数,并发数进行限制

ngx_http_limit_conn_module 模块可以根据定义的KEY来限制每个键值的连接数,如同一个IP来源的连接数

limit_conn_module 连接频率限制

limit_req_module  请求频率限制

http协议的连接与请求

HTTP是建立在TCP,在完成http请求需要先建立TCP三次握手(称为TCP连接),在连接的基础上HTTP请求.

HTTP请求建立在一次TCP连接基础上,一次TCP请求至少产生一次HTTP连接。

1)nginx连接限制配置实战

//nginx连接限制语法

Syntax:limit_conn_zone key zone=name:size;

Default: -

Context: http

Syntax:limit_conn zone number;

Default: -

Context: http,server,location

//nginx连接限制实战

http {

//http段配置连接限制,同一时刻只允许一个客户端IP连接

limit_conn_zone $binary_remote_addr zone=conn_zone:10m;

...

server {

...

location / {

//同一时刻只允许一个客户端IP连接

limit_conn conn_zone 1;

}

}

}

变量:

$binary_remote_addr  #变量的长度是固定的4个字节

$remote_addr #变量的长度7-15字节

建议使用$binary_remote_addr,因一个IP地址=32bit=4字节

10M=10*1024K=10*1024*1024B/4  可存储IP数量

//示例:http段配置连接限制,同一时刻只允许一个客户端IP连接

[root@web02 conf.d]# cat nfzl.conf

limit_conn_zone $binary_remote_addr zone=conn_zone:10m;

server {

listen 80;

server_name 192.168.1.174;

location / {

root /code;

index index.html;

limit_conn conn_zone 1;

}

location /nginx_status {

stub_status;

access_log off;

allow 192.168.1.218;

deny all;

}

location /download {

root /code;

autoindex on;

charset utf-8,gbk;

autoindex_exact_size off;

autoindex_localtime on;

auth_basic "Auth access Download Input your Passwd!";

auth_basic_user_file /etc/nginx/auth_conf;

}

}

//使用ab工具进行压力测试

[root@web02 ~]#yum install httpd-tools

[root@web02 ~]#ab -n 50 -c 20 http://127.0.0.1/index.html

2)nginx请求限制配置实战

//nginx请求限制语法

Syntax:limit_req_zone key zone=name:size rate=rate;

Default: -

Context: http

Syntax:limit_conn zone number [burst=number] [nodelay];

Default: -

Context: http,server,location

//nginx请求限制实战

http {

//http段配置请求限制,rate限制速率,限制一秒钟最多一个IP请求

limit_req_zone $binary_remote_addr zone=conn_zone:10m rate=1r/s;

...

server {

...

location / {

//1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端

limit_req zone=conn_zone;

//请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量,多余的请求返回503错误

#limit_req zone=conn_zone burst=3 nodelay;

}

}

}

//示例:http段配置请求限制,限制一秒钟最多一个IP请求

[root@web02 conf.d]# cat nfzl.conf

#limit_conn_zone $binary_remote_addr zone=conn_zone:10m;

limit_req_zone $binary_remote_addr zone=conn_zone:10m rate=1r/s;

server {

listen 80;

server_name nfzl.com;

location / {

root /code;

index index.html;

# limit_conn conn_zone 1;

limit_req zone=conn_zone burst=8 nodelay;

}

location /nginx_status {

stub_status;

access_log off;

allow 192.168.1.218;

deny all;

}

location /download {

root /code;

autoindex on;

charset utf-8,gbk;

autoindex_exact_size off;

autoindex_localtime on;

auth_basic "Auth access Download Input your Passwd!";

auth_basic_user_file /etc/nginx/auth_conf;

}

}

//使用ab工具进行压力测试

[root@web02 ~]#yum install httpd-tools

[root@web02 ~]#ab -n 50 -c 20 http://127.0.0.1/index.html

//思考:Nginx连接限制没有请求限制有效吗?

我们前面说过,多个请求可以建立一次的TCP连接之上,那么我们对请求的精度限制,当然比对一个连接的限制会更加有效

因为同一时刻只允许一个连接请求进入。但是同一时刻多个请求可以通过一个连接进入。所以请求限制才是比较优的解决方案。

5、Nginx日志配置

//配置语法

Syntax:log_format name [escape=default|json] string ...;

Default: log_format combined "...";

Context:http

//默认Nginx定义日志语法

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

//Nginx日志格式允许包含的变量

  参数     说明

$remote_addr 记录客户端地址

$remote_user 记录客户端用户

$time_local 记录访问时间和时区

$time_iso8601 记录ISO8601标准格式下的本地时间

$request 记录请求的方法以及请求的HTTP协议

$status                 记录HTTP请求状态

$body_bytes_sent     发送给客户端文件内容大小

$bytes_sent         发送给客户端的总字节数

$msec 日志写入世间。单位为秒,精度是毫秒

$http_referer         记录从那个页面链接访问过来的

$http_user_agent     记录用户终端浏览器等信息

$http_x_forwarded_for    记录客户端IP地址

$request_length     请求的长度(包括请求行,请求头和请求正文)

$request_time         整个请求的总时间

//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

Example:

server {

...

access_log /var/log/nginx/www_log.log main;

...

}

//示例 修改日志格式$request_time(请求时间),$time_iso8601(修改日志显示时间)

    log_format  main  '$remote_addr - $remote_user [$time_iso8601] $request_time "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

6、Nginx虚拟站点

所谓虚拟主机,及在一台服务器上配置多个网站

如:公司主页、博客、论坛看是三个网站,实则可以运行在一台服务器上

需求:公司需要有三个站点,分别是www,bbs,blog

1)创建web站点目录

[root@web02 ~]# mkdir -p /code/{www,bbs,blog}

[root@web02 ~]# echo www > /code/www/index.html

[root@web02 ~]# echo bbs > /code/bbs/index.html

[root@web02 ~]# echo blog > /code/blog/index.html

示例一:基于IP的虚拟主机

[root@web02 ~]#vim /etc/nginx/conf.d/www.conf

server {

listen 192.168.1.174:80;

server_name www.nfzl.com;

location / {

root /code/www;

index index.html;

}

}

[root@web02 ~]#vim /etc/nginx/conf.d/bbs.conf

server {

listen 192.168.1.175:80;

server_name bbs.nfzl.com;

location / {

root /code/bbs;

index index.html;

}

}

[root@web02 ~]#vim /etc/nginx/conf.d/blog.conf

server {

listen 192.168.1.176:80;

server_name blog.nfzl.com;

location / {

root /code/blog;

index index.html;

}

}

示例二:基于端口的虚拟主机

[root@web02 ~]#vim /etc/nginx/conf.d/www.conf

server {

listen 80;

server_name www.nfzl.com;

location / {

root /code/www;

index index.html;

}

}

[root@web02 ~]#vim /etc/nginx/conf.d/bbs.conf

server {

listen 81;

server_name bbs.nfzl.com;

location / {

root /code/bbs;

index index.html;

}

}

[root@web02 ~]#vim /etc/nginx/conf.d/blog.conf

server {

listen 82;

server_name blog.nfzl.com;

location / {

root /code/blog;

index index.html;

}

}

示例三:基于域名的虚拟主机

[root@web02 ~]#vim /etc/nginx/conf.d/www.conf

server {

listen 80;

server_name www.nfzl.com;

location / {

root /code/www;

index index.html;

}

}

[root@web02 ~]#vim /etc/nginx/conf.d/bbs.conf

server {

listen 80;

server_name bbs.nfzl.com;

location / {

root /code/bbs;

index index.html;

}

}

[root@web02 ~]#vim /etc/nginx/conf.d/blog.conf

server {

listen 80;

server_name blog.nfzl.com;

location / {

root /code/blog;

index index.html;

}

}

2)语法测试并重启nginx

[root@web02 conf.d]# nginx -t

[root@web02 conf.d]# systemctl restart nginx

3)写hosts解析

192.168.1.174 www.nfzl.com bbs.nfzl.com blog.nfzl.com

4)访问测试

http://www.nfzl.com

http://bbs.nfzl.com

http://blog.nfzl.com

5) 增加需求:实现每个站点日志独立

[root@web02 conf.d]#mkdir -p /code/log

[root@web02 conf.d]# vim www.conf

access_log  /code/log/www.nfzl.com_access.log  main;

server {

        listen 80;

        server_name bbs.nfzl.com;

        access_log  /code/log/bbs.nfzl.com_access.log  main;

        location / {

                root /code/bbs;

                index index.html;

        }

}

~   

[root@web02 conf.d]#vim bbs.conf

access_log  /code/log/bbs.nfzl.com_access.log  main;

[root@web02 conf.d]#vim blog.conf

access_log  /code/log/blog.nfzl.com_access.log  main;


7、Nginx Location

使用Nginx Location可以控制访问网站的路径,但是一个server可以有多个location配置,多个location的优先级该如何区分

1)Location语法示例

location [=|~|~*|^~|!~*|!~|/] /uri/ { … }

2)Location语法优先级排序

匹配符        匹配规则                              优先级

=               精确匹配                                     1

^~              以某个字符串开头                      2

~               区分大小写的正则匹配                 3

~*              不区分大小写的正则匹配             4

!~           区分大小写不匹配的正则              5

!~*         不区分大小写不匹配的正则           6

/              通用匹配,任何请求都会匹配到     7

3)配置网站验证location优先级

[root@web02 conf.d]# cat test.conf

server {

listen 80;

server_name www.nfzl.com;

access_log  /code/log/www.nfzl.com_access.log  main;

location / {

default_type text/html;

return 200 "location /";

}

        location =/ {

                default_type text/html;

                return 200 "location =/";

        }

        location ~ / {

                default_type text/html;

                return 200 "location ~/";

        }

      # location ^~ / {

      #        default_type text/html;

      #        return 200 "location ^~";

      # }

}

4)location应用场景

#通用匹配,任何请求都会匹配到

location / {

}

#严格区分大小写,匹配以.php结尾的都走这个location

location ~ \.php$ {

fastcge_pass http://127.0.0.1:9000;

}

#严格区分大小写,匹配以.jsp结尾的都走这个location

location ~ \.jsp$ {

fastcge_pass http://127.0.0.1:8080;

}

#不区分大小写,只要用户访问.jpg,gif,png,js,css都走这条location

location ~* .*\.(jpg|gif|png|js|css)$ {

rewrite (.*)http://cdn.nfzl.com$request_uri;

}

#不区分大小写匹配

location ~* "\.(sql|bak|tar.gz|tgz|.git)$" {

default_type text/htm;

return 403 "启动访问控制成功";

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,548评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,069评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,985评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,305评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,324评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,030评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,639评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,552评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,081评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,194评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,327评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,004评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,688评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,188评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,307评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,667评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,337评论 2 358

推荐阅读更多精彩内容