Nginx虚拟主机、日志排错、模块配置

x33g5p2x  于2022-01-05 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(148)


Nginx虚拟主机、日志排错、模块如何配置

Nginx虚拟主机

三种方式

1. 基于多IP的方式

# 防止其他配置文件影响,将所有配置文件压缩
[root@web01 conf.d]# gzip Mario.conf 
[root@web01 conf.d]# gzip chess.conf.gz 
[root@web01 conf.d]# gzip default.conf.gz 
# 编辑配置文件
[root@web01 ~]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim game.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
	root /opt/Super_Mario;
        index index.html;
    }
}
server {
    listen 80;
    server_name 172.16.1.7;
    location / {
        root /opt/chess;
        index index.html;
    }
}

# 测试配置文件
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfu
# 重启服务
[root@web01 conf.d]# systemctl restart nginx

2. 基于多端口的方式

# 编辑配置文件
[root@web01 conf.d]# vim game1.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/Super_Mario;
        index index.html;
    }
}
server {
    listen 81;
    server_name 192.168.15.7;
    location / {
        root /opt/chess;
        index index.html;
    }
}
~

3. 基于多域名的方式

# 编辑配置文件
[root@web01 conf.d]# vim game2.conf
server {
    listen 80;
    server_name www.Super_Mario.com;
    location / {
        root /opt/Super_Mario;
        index index.html;
    }
}
server {
    listen 80;
    server_name www.chesss.com;
    location / {
        root /opt/chess;
        index index.html;
    }
}
# 注意域名不能用chess

2和3其余步骤和1一样

Nginx日志

  • nginx日志文件目录:/var/log/nginx/

  • 排错方式:

  • 网站排错

  • 查看错误日志

Nginx配置文件配置项

  • 相关文件:/etc/nginx/nginx.conf
  • 主要配置项
# 配置详解
 $remote_addr:客户端IP
 - :代表登录用户(没有就是-)
 $remote_user [$time_local] :访问时间
 $request:请求方式、类型
$status :状态码
$body_bytes_sent :访问文件大小
$http_referer:访问域名 
$http_user_agent:客户端标识
$http_x_forwarded_for:真实的客户端IP(在反向代理中生效)

Nginx模块

Nginx访问控制模块

👉[官网模块介绍](nginx documentation)

  1. [ngx_http_access_module](Module ngx_http_access_module (nginx.org))

使用范围:http,server,location,limit_except(http,server常用)

# 配置项:deny和allow,拒绝或者允许某些ip访问
deny:拒绝
allow:允许
# 语法:
Syntax:	deny address | CIDR | unix: | all;
Syntax:	allow address | CIDR | unix: | all;
# 官网示例:
location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}
# 示例1:
允许192.168.15.1访问,不允许其他IP访问
# 修改game2.conf文件中的server
server {
    listen 80;
    server_name www.Super_Mario.com;
    allow 192.168.15.1;
    deny all;
    location / {
        root /opt/Super_Mario;
        index index.html;
    }
}

# 示例2:
允许192.168.15.0这个网段访问,不允许其他网段访问
allow 192.168.15.0/24;
deny all;
# 示例3:
只允许通过VPN来访问
allow 172.16.1.81; 
deny all;
  1. ngx_http_auth_basic_module

使用范围:http,server,location,limit_except

语法:auth_basic string | off;(默认关闭)
# 官网示例:
location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
}

# 示例:访问之前需要登录
# 安装httpd-tools
[root@web01 conf.d]# yum install httpd-tools -y
# 生成用户名密码文件
[root@web01 conf.d]# htpasswd -c /etc/nginx/auth hammer
New password: 
Re-type new password: 
Adding password for user hammer
#  查看
[root@web01 conf.d]# cat /etc/nginx/auth 
hammer:$apr1$fOHr21Vf$zpI/MVxQ452KzP0p10QI10
# 将密码文件路径加入配置
server {
    listen 80;
    server_name www.Super_Mario.com;
    auth_basic "hello nginx";
    auth_basic_user_file /etc/nginx/auth;
    location / {
        root /opt/Super_Mario;
        index index.html;
    }
}
[root@web01 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@web01 conf.d]# systemctl restart nginx

  1. ngx_http_autoindex_module
    作用是目录索引~

使用范围:http,server,location

语法:autoindex on | off(默认关闭);
# 官网示例
location / {
    autoindex on;
}

# 示例:指定目录索引展示
[root@web01 games]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim index.conf

Nginx状态监控模块

# stub_status需要一个独立的location
# 监控Nginx运行状态。
[root@web01 conf.d]# vim index.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        stub_status;
    }
}

访问连接控制模块

1、控制Nginx连接数

	1、安装ab测试命令
	yum install httpd-tools -y 

	2、ab 参数
		-n : 总共需要访问多少次
		-c : 每次访问多少个

[root@web01 conf.d]# vim game5.conf 
# limit_req_zone $remote_addr zone=one:10m rate=1r/s;
limit_conn_zone $remote_addr zone=addr:10m;
server {
    listen 80;
    server_name 192.168.15.7;
    # limit_req zone=one burst=5;
    limit_conn addr 1;
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
}

2、控制Nginx访问量

	1、连接池
		limit_req_zone $remote_addr zone=one:10m rate=1r/s;
		声明连接池       变量          名称  连接池的大小  速率
	2、限制数

案例1:要求每秒只能有一个访问。
[root@web01 conf.d]# vim game5.conf 
limit_req_zone $remote_addr zone=one:10m rate=1r/s;
server {
    listen 80;
    server_name 192.168.15.7;
    limit_req zone=one burst=5;
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
}

相关文章