Linux基础32 nginx多虚拟主机,日志,日志目录模块,访问限制模块

发布时间 2023-08-04 22:43:01作者: 战斗小人

虚拟主机

方式一:基于主机多IP方式

基于主机多ip的方式,主机多网卡,多外网ip (一般不使用这种方式)

[root@web01 conf.d]# cat chess.conf
server {
    listen 10.0.0.7:80;
    server_name localhost;

    location / {
    root /code/chess;
    index index.html;
    }
}
[root@web01 conf.d]# cat snake.conf 
server {
    listen 172.16.1.7:80;
    server_name localhost;

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

# 注意: 修改完配置,nginx重启才生效,重新加载没生效。通过 netstat -lntp 判断
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5632/rpcbind        
tcp        0      0 172.16.1.7:80           0.0.0.0:*               LISTEN      11113/nginx: master 
tcp        0      0 10.0.0.7:80             0.0.0.0:*               LISTEN      11113/nginx: master 

 

方式二:基于多端口的配置

一般用于公司内部,测试环境

[root@web01 conf.d]# cat chess.conf 
server {
    listen 81;
    server_name localhost;

    location / {
    root /code/chess;
    index index.html;
    }
}
[root@web01 conf.d]# cat snake.conf 
server {
    listen 80;
    server_name localhost;

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

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

# 检查
[root@web01 conf.d]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5632/rpcbind        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11178/nginx: master 
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      11178/nginx: master

 

方式三:基于多个域名的方式

[root@web01 conf.d]# cat chess.conf 
server {
    listen 80;
    server_name www.chess.com;

    location / {
    root /code/chess;
    index index.html;
    }
}
[root@web01 conf.d]# cat snake.conf 
server {
    listen 80;
    server_name www.snake.com;

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

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

# 修改本地hosts
C:\Windows\System32\drivers\etc\hosts
10.0.0.7 www.chess.com www.snake.com

 

# 注: 可以通过修改访问域名实现
[root@web01 conf.d]# vim chess.conf
server {
    listen 80;
    server_name localhost;

    location / {
        root /code/chess;
        index index.html;
    }
    location /snake {    # 这块不写,10.0.0.7/snake访问/code/chess/snake下的index.html
        root /code;        # 相当于把域名追加到/code后面,等于在/code/snake中找
        index index.html;
    }
}

注意:

一般测试环境使用多端口的方式;
公司内部或者生产环境使用基于域名的方式;

 

nginx日志实战

[root@web01 conf.d]# cat chess.conf 
server {
    listen 80;
    server_name www.chess.com;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
    root /code/chess;
    index index.html;
    }
}
[root@web01 conf.d]# cat snake.conf 
server {
    listen 80;
    server_name www.snake.com;
    access_log  /var/log/nginx/www.snake.com.log  main;

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

[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# cd /var/log/nginx/
[root@web01 nginx]# ll
total 48
-rw-r----- 1 nginx adm   9308 Aug  2 23:47 access.log
-rw-r----- 1 nginx adm  31749 Aug  3 00:43 error.log
-rw-r--r-- 1 root  root   575 Aug  3 00:43 www.chess.com.log
-rw-r--r-- 1 root  root     0 Aug  3 00:42 www.snake.com.log

 

Nginx日志

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

1.log_format语法

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

2.log_format默认日志格式

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

参数可以查询:https://nginx.org/en/docs/http/ngx_http_core_module.html

https://nginx.org/下,右侧 documentation, 在Modules reference下,点击ngx_http_core_module,最下面的参数

3.log_format常用变量

$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.nginx日志切割

使用logrotate切割日志
[root@web01 ~]# cat /etc/logrotate.d/nginx 
# 要切割的日志存放位置
/var/log/nginx/*.log {
        #每天切割日志
        daily
        #日志丢失忽略
        missingok
        #日志保留时间(天)
        rotate 52
        #日志文件压缩
        compress
        #延时压缩
        delaycompress
        #不切割空文件
        notifempty
        #指定日志文件权限 属主nginx 数组adm
        create 640 nginx adm
        #脚本起始
        sharedscripts
        #标注脚本内容
        postrotate
                if [ -f /var/run/nginx.pid ]; then    #判断是否有pid文件
                        kill -USR1 `cat /var/run/nginx.pid` #日志重读(重新生成日志文件)
                fi
        #脚本结束
        endscript
}

 

nginx常用模块

1.目录索引模块

ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
# ngx_http_index_module模块就是配置文件中index配置

1)语法

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

# 常用参数
charset utf-8;
默认中文是乱码,添加该参数可以解决乱码问题(推荐设置在http层,但也可以设置在server层)

autoindex_exact_size off;    # 可设置在http层,或server层
默认为on,显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是KB或者MB或者GB。

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
修改为on,显示的文件时间为文件的最后修改时间。

例:

[root@web01 conf.d]# vim chess.conf
server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
        root /code;
        autoindex on;    # 找不到index配置文件,会改为目录列表显示
        autoindex_exact_size off;
        autoindex_localtime on;
        #index index.html;
    }
}

如目录列表中有中文文件名,显示乱码,修改nginx配置文件中的字符编码

[root@web01 centos]# vim /etc/nginx/nginx.conf    # 这里在http层进行设置
user  www;
...
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    charset utf-8;
    ...
}

 

2.nginx访问控制模块 ngx_http_access_module

1)模块语法

# 允许访问
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

官网示例

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;    # 除了192.168.1.1外,192.168.1这个网段都可以访问
}

2)访问控制示例

# 允许10.0.0.1访问,其他网址不允许
server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
        root /code;
        autoindex on;
        allow 10.0.0.1;    # 配置从上往下读,要先允许在拒绝。如果先deny all,就不会往下读了
        deny all;
    }
}

# 也可以通过其他文件引入访问控制
server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
        root /code;
        autoindex on;
        include /access/*.sh;    # 把要禁止的内容写入该路径下
    }
}
vim /access/1.sh
allow 10.0.0.1;
deny all;

# 拒绝10.0.0.1访问,其他都可以访问
server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
        root /code;
        autoindex on;
        deny 10.0.0.1;
        allow all;
    }
}

# 允许10.0.0.0网段访问,其他网段不允许
server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
        root /code;
        autoindex on;
        allow 10.0.0.0/24;
        deny all;
    }
}

 

3.需求: 访问10.0.0.8,出现游戏页面,访问10.0.0.8/download,出现目录页面,只允许172.16.1.0网段访问

server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/www.chess.com.log  main;

    location / {
        root /code;
        index index.html;
    }
    
    location /download {
        root /code;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        allow 172.16.1.0/24;
        deny all;
    }
}