02.Nginx基础应用

1.Nginx目录索引

1.Nginx默认是不允许列出整个目录浏览下载。

Syntax: autoindex on | off;
Default:    autoindex off;
Context:    http, server, location
# autoindex常用参数
autoindex_exact_size off;
默认为on, 显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。

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

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

2.配置站点目录实现浏览功能

server {
    listen 80;
    server_name  www.lxgyw.com;
    location / {
        root /code/down;
        charset utf-8,gbk;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
    }
}

2.Nginx状态监控

1.ngx_http_stub_status_module用于展示Nginx连接状态信息, 需要*–with-http_stub_status_module*模块支持

Syntax: stub_status;
Default: —
Context: server, location

2.配置Nginx status示例

server {
    listen 80;
    server_name www.lxgyw.com;
    access_log off;
    
    location /nginx_status {
        stub_status;
    }
}

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

Active connections # 当前活动的连接数
accepts # 当前的总连接数TCP
handled # 成功的连接数TCP
requests # 总的http请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了keepalive

注意: 一次TCP的连接,可以发起多次http的请求, 如下配置参数可验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接

3.Nginx访问控制

基于IP的访问控制 http_access_module
基于用户登陆认证 http_auth_basic_module

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
  1. 访问控制配置示例, 拒绝指定的IP, 其他全部允许
server {
    listen 80;
    server_name www.lxgyw.com;
    access_log off;
    
    location /nginx_status {
        stub_status;
        deny 10.0.0.1;
        allow all;   
    }
}
  1. 访问控制配置示例, 只允许谁能访问, 其它全部拒绝
server {
    listen 80;
    server_name www.lxgyw.com;
    access_log off;
    
    location /nginx_status {
        stub_status;
        allow   10.0.0.0/24;
        allow   127.0.0.1;
        deny    all;
    }
}

2.Nginx基于用户登陆认证

  1. 基于用户登陆认证配置语法
//访问提示字符串
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
  1. 基于用户登陆认证配置实践

1.生成账户密码文件

[root@lxgyw ~]# yum install httpd-tools
[root@lxgyw ~]# htpasswd -b -c /etc/nginx/auth_conf lxgyw 123456

2.nginx配置调用

server {
    listen 80;
    server_name www.lxgyw.com;
    access_log off;
    
    location /nginx_status {
        stub_status;
        auth_basic "Auth access Blog 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请求

注:客户端的IP地址作为键。

$remote_addr 变量的长度为7字节到15字节 $binary_remote_addr 变量的长度是固定的4字节

1.Nginx连接限制配置实战

1)Nginx连接限制配置语法

Syntax:  limit_conn_zone key zone=name:size;
Default: —
Context: http

Syntax: limit_conn zone number;
Default: —
Context: http, server, location

2)Nginx连接限制配置实践

# http标签段定义连接限制
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
server {
# 同一时刻只允许一个客户端连接
    limit_conn conn_zone 1; 

    location / {
        root /code;
        index index.html;
    }

3).使用ab工具进行压力测试

[root@lxgyw ~]# yum install -y httpd-tools
[root@lxgyw ~]# ab -n 50 -c 20  http://127.0.0.1/index.html

4).nginx日志结果

2018/10/24 18:04:49 [error] 28656#28656: *1148 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.lxgyw.com, request: "GET / HTTP/1.0", host: "www.lxgyw.com"
2018/10/24 18:04:49 [error] 28656#28656: *1155 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.lxgyw.com, request: "GET / HTTP/1.0", host: "www.lxgyw.com"
2018/10/24 18:04:49 [error] 28656#28656: *1156 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.lxgyw.com, request: "GET / HTTP/1.0", host: "www.lxgyw.com"

2.Nginx请求限制配置实战

  1. 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
  1. Nginx 请求限制配置实战
# http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
    listen 80;
    server_name module.bgx.com;
    # 1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
    limit_req zone=req_zone;
    
    # 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
    limit_req zone=req_zone burst=3 nodelay;
    location / {
        root /code;
        index index.html;
    }
}

3).使用ab工具进行压力测试

[root@lxgyw ~]# yum install -y httpd-tools
[root@lxgyw ~]# ab -n 50 -c 20  http://127.0.0.1/index.html

4).nginx日志结果

2018/10/24 07:38:53 [error] 81020#0: *8 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: www.lxgyw.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
2018/10/24 07:38:53 [error] 81020#0: *9 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: www.lxgyw.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
2018/10/24 07:38:53 [error] 81020#0: *10 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: www.lxgyw.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"

3.Nginx连接限制没有请求限制有效?

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

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

5.Nginx虚拟站点

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

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

1.基于域名虚拟主机配置实战

1.创建web站点目录

[root@lxgyw ~]# mkdir /soft/code/{www,bbs}
[root@lxgyw ~]# echo "www" > /soft/code/www/index.html
[root@lxgyw ~]# echo "bbs" > /soft/code/bbs/index.html

2.配置不同域名的虚拟主机

[root@lxgyw ~]# cat /etc/nginx/conf.d/www.conf
server {
    listen       80;
    server_name  www.lxgyw.com;
    root /soft/code/www;
    index index.html;
    ...
}
[root@lxgyw ~]# cat /etc/nginx/conf.d/bbs.conf
server {
    ...
    listen       80;
    server_name  www.lxgyw.com;
    root /soft/code/bbs;
    index index.html;
}

2.基于端口虚拟主机配置实战

//仅修改listen监听端口即可, 但不能和系统端口出现冲突
server {
    ...
    listen       8001;
    ...
}
 
server {
    ...
    listen       8002;
    ...
}

3.基于虚拟主机别名配置实战

实现用户访问多个域名对应同一个网站, 比如用户访问www.xuliangwei.com和访问xuliangwei.com内容一致

# 1.默认配置方式
[root@lxgyw ~]# vim /etc/nginx/conf.d/www.conf
server {
    listen       80;
    server_name www.bgx.com;
}
server {
    listen       80;
    server_name bgx.com;
}


# 2.使用别名配置方式
[root@lxgyw ~]# vim /etc/nginx/conf.d/www.conf
server {
    listen       80;
    server_name  www.lxgyw.com lxgyw.com;
    ...
}

# 3.测试访问, 带www和不带www是一样的
[root@lxgyw ~]# curl lxgyw.com
Go
[root@lxgyw ~]# curl www.lxgyw.com
Go

6.Nginx日志配置

Nginx有非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式 通过log_format命令定义格式。

1.log_format定义日志格式语法

# 配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http

2.默认Nginx定义语法格式如下

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

3.Nginx日志格式允许包含的内置变量

$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地址和客户端请求的服务器地址。

4.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

5.Nginx Access日志配置实践

#针对Server配置, 接收整个网站的日志
server {
    access_log /var/log/nginx/www.log main;
}

# 针对Location配置, 仅该Location生效
location / {
    access_log /var/log/nginx/www.log main;
}

# 如不需要记录日志, 则可选择关闭
access_log off;

7.Nginx Location

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

1.Location 语法示例

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

2.Location 语法优先级排列

匹配符匹配规则优先级
=精确匹配1
^~以某个字符串开头2
~区分大小写的正则匹配3
~*不区分大小写的正则匹配4
!~区分大小写不匹配的正则5
!~*不区分大小写不匹配的正则6
/通用匹配,任何请求都会匹配到7

3.配置网站验证 Location 优先级

[root@Nginx conf.d]# cat testserver.conf 
server {
    listen 80;
    server_name www.lxgyw.com;
    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效果

# 优先级最高符号=
[root@Nginx conf.d]# curl www.lxgyw.com
location =/

# 注释掉精确匹配=, 重启Nginx
[root@Nginx ~]# curl www.lxgyw.com
location ~/

# 注释掉~, 重启Nginx
[root@Nginx ~]# curl www.lxgyw.com
location /

5.Locaiton应用场景

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

}

# 严格区分大小写,匹配以.php结尾的都走这个location    
location ~ \.php$ {
    fastcgi_pass http://127.0.0.1:9000;
}

# 严格区分大小写,匹配以.jsp结尾的都走这个location 
location ~ \.jsp$ {
    proxy_pass http://127.0.0.1:8080;
}

# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
        rewrite (.*) http://cdn.xuliangwei.com$request_uri;
}

# 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
        default_type text/html;
        return 403 "启用访问控制成功";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭亚望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值