Nginx常用模板

一:目录索引

作用:可以做简单的文件下载服务器
参数:

Syntax:
autoindex on | off;           #on表示开启索引

autoindex_exact_size off      #默认为 on,显示出文件的确切大小,单位是 bytes。修改为 off,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB。

autoindex_localtime on;       #默认为 off,显示的文件时间为 GMT 时间;修改为 on,显示的文件时间为文件的服务器时间。

charset utf-8,gbk;            #默认中文目录乱码,添加上解决乱码

1.1 配置文件

[root@web02-8 conf.d]# cat cxy.conf 
server {
    listen 80;
    server_name game.cxytest.com;
    location / {
    root /cxy_code/www/;
    index index.html;
    }
        
    location /download {
    root /cxy_code/www/;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    charset utf-8,gbk;
    }       

}

1.2 创建下载目录

[root@web02-8 conf.d]# mkdir /cxy_code/www/download/cxy{1..10} -p

1.3 检查语法,重载nginx

[root@web02-8 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02-8 conf.d]# systemctl reload nginx

1.4 测试访问

image.png

二:基于ip的访问控制

2.1 拒绝某个ip访问

[root@web02-8 conf.d]# vim cxy.conf 
server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/www/;
        index index.html;
        deny 10.0.0.1;
        allow all;
        }
}

2.2 通过日志查看规则生效

[root@web02-8 conf.d]# tailf /var/log/nginx/error.log 
image.png

2.2 只允许某个ip访问

[root@web02-8 conf.d]# vim cxy.conf 
server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/www/;
        index index.html;
        allow 10.0.0.1;
        deny all;
        }
}

三:基于用户的访问控制

参考官网的语法


image.png

3.1 需要安装http-tools工具生成用户名和密码

[root@web02-8 conf.d]# yum install -y httpd-tools
[root@web02-8 conf.d]# htpasswd -b -c /cxy_code/www/auth_conf www www
Adding password for user www

3.2 配置文件

[root@web02-8 conf.d]# vim cxy.conf 
server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/www/;
        index index.html;
        auth_basic "Input your user&&passwd";
        auth_basic_user_file /cxy_code/www/auth_conf;
        }
}

3.3 访问测试

image.png

四:访问限制

4.1 连接限制(tcp连接限制)

参考官网语法


image.png

4.1.1 配置文件

root@web02-8 conf.d]# vim cxy.conf 
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/xiaoniao/;
        index index.html;
        limit_conn conn_zone 1;    #同一时间只允许一个ip连接
        }
}     
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
参数说明:
$binary_remote_addr           变量长度是固定的4个字节
$remote_addr                  变量长度固定是7-15个字节

而一个ip地址=32个bit=4个字节
这里设置成con_zone:10M代表
10M=(10*1024K=10*1024*1024KB)/4=2621440
大概可以存储2621440个ip

4.2 请求限制(http请求限制,使用请求限制效果最佳)

参考官网语法


image.png

4.2.1 配置文件

[root@web02-8 conf.d]# cat cxy.conf 
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;      #一秒钟最多允许ip一个连接
server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/xiaoniao/;
        index index.html;
    limit_req zone=one burst=5;     #突发量为5,超过5个之后访问错误代码
       # limit_conn conn_zone 1;
        }
}

4.2.2 使用ab工具测试

[root@web01 ~]# yum install httpd-tools -y
[root@web03-9 conf.d]# ab -n 50 -c 20 http://game.cxytest.com/index.html     #-n 50代表发送50个请求;-c 20代表并发数,表示同时有20个用户发送请求
Concurrency Level:      20
Time taken for tests:   5.001 seconds
Complete requests:      50         #发起50次请求
Failed requests:        44         #失败了44次

五`:状态监控模板

参照官网说明


image.png

5.1 配置文件

server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/xiaoniao/;
        index index.html;
    #limit_req zone=one burst=5;
       # limit_conn conn_zone 1;
        }
        location = /basic_status {
    
        stub_status;
                access_log off;
    }
}

5.2 测试访问

image.png
Active connections: 1           #当前活动的连接数
accepts     207                 #当前的总连接数TCP                      
handled     207                 #成功的连接数TCP
requests    664                 #总的HTTP请求数

通过这个页面可以监控tcp长连接和短连接

修改主配置文件里的keepalive的时间为0
[root@web02-8 conf.d]# vim /etc/nginx/nginx.conf 
keepalive_timeout  0;

再去查看监控状态页面会发现,每刷新一次网页,accepts和handled都会增加一次,这就是tcp短连接

六:日志模块

6.1 日志参数说明

$remote_addr # 记录客户端 IP 地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录 ISO8601 标准格式下的本地时间
$request # 记录请求的方法以及请求的 http 协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端 IP 地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果 Nginx 位于负载均衡器, nginx 反向代理之后, web 服务器无法直接获取到客 户端真实的 IP 地址。
# $remote_addr 获取的是反向代理的 IP 地址。 反向代理服务器在转发请求的 http 头信息中,
# 增加 X-Forwarded-For 信息,用来记录客户端 IP 地址和客户端请求的服务器地址。

系统默认的日志格式是

http {

    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  /var/log/nginx/access.log  main;

6.2 如果有多个server模块,建议区别不同目录的log日志

server {
        listen 80;
        server_name game.cxytest.com;
        location / {
        root /cxy_code/xiaoniao/;
        access_log /cxy_code/log/xiaoniao_access.log main;            #可以直接在对应的location里配置log文件以及目录
        index index.html;
       }

}

七:虚拟主机

环境:在一个台服务器上实现多个站点
基于ip的虚拟主机: ip不一样(不常用),不做配置
基于端口的虚拟主机: ip都一样,端口不一样
基于域名的虚拟主机: ip和端口都一样,域名不一样

7.1 基于端口的虚拟主机

[root@web02-8 conf.d]# cat www.conf 
server {
    listen       83;
    server_name  game.cxytest.com;
    location / {
        root /cxy_code/test/www/;
        index index.html;
        access_log /cxy_code/test/log/www/access.log main;
    }

}

[root@web02-8 conf.d]# cat bbs.conf
server {
    listen       81;
    server_name  game.cxytest.com;
    location / {
        root /cxy_code/test/bbs/;
        index index.html;
        access_log /cxy_code/test/log/bbs/access.log main;
    }
}

[root@web02-8 conf.d]# cat blog.conf 
server {
    listen       82;
    server_name  game.cxytest.com;
    location / {
        root /cxy_code/test/blog/;
        index index.html;
        access_log /cxy_code/test/log/blog/access.log main;
    }
}

#域名可以保持一样,也可以不一样

7.2 基于域名的虚拟主机

server {
    listen       80;
    server_name  www.cxytest.com;
    location / {
        root /cxy_code/test/www/;
        index index.html;
        access_log /cxy_code/test/log/www/access.log main;
    }

}

server {
        listen       80;
        server_name  bbs.cxytest.com;
        location / {
                root /cxy_code/test/bbs/;
                index index.html;
                access_log /cxy_code/test/log/bbs/access.log main;
        }

}

server {
        listen       80;
        server_name  blog.cxytest.com;
        location / {
                root /cxy_code/test/blog/;
                index index.html;
                access_log /cxy_code/test/log/blog/access.log main;
        }

}

八:location规则

8.1 location语法

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

8.2 location优先级

image.png

常用的location是
~ 区分大小写正则匹配
~* 不区分大小写正则匹配
/ 通用匹配,任何请求都会匹配

8.3 location实际应用场景

8.3.1 通用匹配

location / {

}

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

location ~ \.php$ {
               fastcgi_pass http://172.0.0.1:9000;
}

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

location ~ \.jsp$ {
              proxy_pass http://127.0.0.1:8000;
}

8.3.4 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这个location

location ~* .*\.(jpg|gif|png|js|css)$ {
                   rewrite (.*) http://cdn.cxy.com$request_uri;
}

8.3.5 不区分大小写匹配

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

推荐阅读更多精彩内容