内容概要

  • Nginx 虚拟主机

    • 基于多 IP 的方式
    • 基于多端口的方式
    • 基于多域名的方式
  • 日志配置(日志格式详解)
  • Nginx 访问控制模块(可以去 Nginx.org 文档 documentation 查找)
  • Nginx 访问认证模块
  • Nginx 状态监控模块

内容详细

一、Nginx 虚拟主机

搭建完 Nginx 服务之后,可以通过多种方式来访问网站

1、基于多 IP 的方式

[root@web01 conf.d]# cat game2.conf
server {
listen 80;
server_name 192.168.15.7; < -- 使用不同的 IP 地址访问进入不同的网站根目录
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 80;
server_name 172.16.1.7; < -- 使用不同的 IP 地址访问进入不同的网站根目录
location / {
root /opt/tank;
index index.html;
}
}

2、基于多端口的方式

[root@web01 conf.d]# cat game3.conf
server {
listen 80; < -- 使用不同的 端口 访问进入不同的网站根目录
server_name 192.168.15.7;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 81; < -- 使用不同的 端口 访问进入不同的网站根目录
server_name 192.168.15.7;
location / {
root /opt/tank;
index index.html;
}
}

3、基于多域名的方式(需要配置 解析文件 hosts)

[root@web01 conf.d]# cat game4.conf
server {
listen 80;
server_name game.maro.com;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 80;
server_name game.chinese_chess.com;
location / {
root /opt/tank;
index index.html;
}
} # 配置解析文件
windows
C:\Windows\System32\drivers\etc\hosts

二、Nginx 日志详解

# 企业中 Nginx 日志监控指标(日志作用)
网站状态码是500的比例:
比例高表示网站稳定性不好
网站的访问来源
查看恶意访问 IP ,短时间发起大量请求
网站排错
查看 Nginx 报错位置
systemctl status nginx.service -l 或查看日志 cat /var/log/nginx/error.log $remote_addr : 客户端IP
$http_x_forwarded_for : 真实的客户端IP(在反向代理中生效)

Nginx 日志格式配置文件 : /etc/nginx/nginx.conf

    log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"service":"nginxTest",'
'"trace":"$upstream_http_ctx_transaction_id",'
'"log":"log",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log /var/log/nginx/access.log json ;

简约版本

    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;

日志格式详解

搭配日志输出结果了解日志变量意思:

tail -f : 实时查看日志文件

[root@web01 ~]# tail -f /var/log/nginx/access.log

日志变量

$remote_addr : 客户端 IP (上一层访问的 IP ,如果是通过 VPN 访问,显示跳板机 IP)
$http_x_forwarded_for : 真实的客户端IP(在反向代理中生效) $remote_addr : 192.168.15.1
$remote_user [$time_local] : [04/Jan/2022:16:17:51 +0800]
$request : "GET / HTTP/1.1" (请求方式为 GET ) $status : 404 (状态码)
$body_bytes_sent : 555 (文件大小)
$http_referer : "http://192.168.15.7/" (访问的域名)
$http_user_agent : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" (请求载体的身份标识)

三、日志模块

Nginx 配备了很多日志模块,具体使用方法查看官方文档 : https://nginx.org/en/docs/

1、Nginx 访问控制模块

查看该模块原始启动状态

ngx_http_access_module

允许或者拒绝某些 IP 的访问
deny : 拒绝 (阻止某个访问者的 IP)
allow : 允许 案例1:允许192.168.15.1访问,不允许其他IP访问(执行顺序自上而下)
allow 192.168.15.1;
deny all; 案例2:允许192.168.15.0这个网段访问,不允许其他网段访问
allow 192.168.15.0/24;
deny all; 案例3:只允许通过VPN来访问
allow 172.16.1.81;
deny all;

ngx_http_auth_basic_module

设置访问之前需要进行登录

1、安装httpd-tools
[root@web01 ~]# yum install httpd-tools -y
2、生成用户名密码文件
[root@web01 nginx]# htpasswd -c /etc/nginx/auth elijah
New password:
Re-type new password:
Adding password for user elijah (在 /etc/nginx 路径下创建认证文件 auth,登录用户名为 elijah) [root@web01 nginx]# cat auth
elijah:$apr1$H9Mw0K6b$pOATFh4sydoHKIG8ciGYI1 3、将文件路径加入Nginx配置
[root@web01 conf.d]# cat game.conf
server {
listen 80;
server_name game.maro.com;
auth_basic "welcome to login"; < -- 将登录认证文件添加到 server 模块
auth_basic_user_file /etc/nginx/auth;
location / {
root /opt/super_Maro;
index index.html;
}
} 4、重启Nginx
[root@web01 conf.d]# systemctl restart nginx

ngx_http_autoindex_module

展示目录索引

autoindex on; 				(打开索引)
autoindex_exact_size on; (显示文件大小)
autoindex_localtime on; (显示文件时间,当地时间)
autoindex_format json; (页面展示的格式 默认 html)
[root@web01 conf.d]# vim game.conf
[root@web01 conf.d]# cat game.conf
server {
listen 80;
server_name game.maro.com;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
autoindex_format html;
location / {
root /tmp/nginx-1.20.2;
index index.html;
}
}

补充:

nginx 如果是编译安装,想要执行 nginx 命令,需要输入完整目录
/usr/local/nginx/sbin/nginx -t 太麻烦了,需要添加到环境变量才方便使用
临时添加:
PATH=$PATH:/usr/local/nginx/sbin
永久添加:
vim /root/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
source .bash_profile (使环境变量不用重启就能让里面新增的变量生效)

2、Nginx 状态监控模块

监控 Nginx 运行状态

[root@web01 conf.d]# cat game5.conf
server {
listen 80;
server_name 192.168.15.7;
location / {
stub_status;
}
}

3、访问连接控制模块(控制访问速率)

1、控制 Nginx 连接数

ngx_http_limit_conn_module

1、安装 ab 测命令(压力测试)
yum install httpd-tools -y 2、ab 参数
-n : 总共需要访问多少次
-c : 每次访问多少个(请求数) limit_conn_zone $remote_addr zone=addr:10m;
server {
listen 80;
server_name 192.168.15.7;
limit_conn addr 1;
location / {
root /opt/super_Maro;
index index.html;
}
}

ngx_http_limit_req_module

2、控制 Nginx 访问量

1、创建连接池(可以用来记录被访问的次数)
limit_req_zone $remote_addr zone=one:10m rate=1r/s;
声明连接池 变量(客户端IP) 名称 连接池的大小 速率 2、限制数
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; < -- 每秒超过1次请求的次数不能超过5个
location {
root /opt/super_Maro;
index index.html;
}
}

nginx 配置文件与日志模块的更多相关文章

  1. Nginx配置文件及模块解析

    一.Nginx是什么? Nginx是一个基于c语言开发的高性能http服务器及反向代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu.内 ...

  2. nginx日志模块、事件模块

    日志模块 1.access_log指令 语法: access_log path [format [buffer=size [flush=time]]]; access_log logs/access. ...

  3. 循序渐进nginx(三):日志管理、http限流、https配置,http_rewrite模块,第三方模块安装,结语

    目录 日志管理 access_log error_log 日志文件切割 自定义错误页 http访问限流 限制请求数 语法 使用 限制连接数 语法 测试 补充: https配置 使用 生成证书 配置ng ...

  4. Nginx配置文件nginx.conf有哪些属性模块?

    worker_processes 1: # worker进程的数量 events { # 事件区块开始 worker_connections 1024: # 每个worker进程支持的最大连接数 } ...

  5. Python 日志模块 logging通过配置文件方式使用

    vim logger_config.ini[loggers]keys=root,infoLogger,errorlogger [logger_root]level=DEBUGhandlers=info ...

  6. Nginx配置文件nginx.conf中文详解(转)

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  7. Nginx配置文件详解

    Nginx是一款面向性能设计的HTTP服务器,相较于Apache.lighttpd具有占有内存少,稳定性高等优势. ######Nginx配置文件nginx.conf中文详解##### #定义Ngin ...

  8. 005.nginx配置文件

    1.替换nginx主配置文件 通过前面的配置,LNMP的环境已经搭建完成,现在我们替换nginx配置文件: [root@huh ~]# cd /usr/local/nginx/conf/[root@h ...

  9. nginx配置文件nginx.conf超详细讲解

    #nginx进程,一般设置为和cpu核数一样worker_processes 4;                        #错误日志存放目录 error_log  /data1/logs/er ...

随机推荐

  1. java调用redis的多种方式与心得

    心得: /** * 心得: * 1.连接方式主要有:直连同步,直连事务,直连管道,直连管道事务,分布式直连同步,分布式直连管道, * 分布式连接池同步,分布式连接池管道:普通连接池同步,普通连接池管道 ...

  2. Vue-cli代理解决跨域问题

    使用vue-cli调接口的时候,总是会出现垮与问题,因为vue的localhost与访问域名不一致导致.而这一点,开发者显然也想到了,故而在vuejs-templates,也就是vue-cli的使用的 ...

  3. 用格里高利公式求给定精度的PI值

    本题要求编写程序,计算序列部分和 4∗(1−1/3+1/5−1/7+...) ,直到最后一项的绝对值小于给定精度eps. 输入格式: 输入在一行中给出一个正实数eps. 输出格式: 在一行中按照&qu ...

  4. 在Linux系统(centos7)中,安装VScode,并在VScode上编写HTML网页

    [实验目的] 在Linux系统中,搭建编写HTML网页的环境.在VS code官网上,下载VS code安装程序,进行安装.在VS code软件中编写HTML页面,并正确运行. [实验步骤] 1)   ...

  5. Linux防止文件被误删除或修改

    chattr简介 Linux没有回收站,一旦文件或文件夹被误删除,要寻找回来很麻烦,不如事先对一些重要的文件做一些保护,这时我们需要一个命令chattr,其使用格式为 chattr 操作符 属性 文件 ...

  6. 《剑指offer》面试题27. 二叉树的镜像

    问题描述 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入:      4    /   \   2     7  / \   / \ 1   3 6   9 镜像输出:      4 ...

  7. ctfshow萌新 web1-7

    ctfshow萌新 web1 1.手动注入.需要绕过函数inval,要求id不能大于999且id=1000,所以用'1000'字符代替数字1000 2.找到?id=" "处有回显 ...

  8. IDEA2017 maven Spark HelloWorld项目(本地断点调试)

    作为windows下的spark开发环境 1.应用安装 首先安装好idea2017 java8 scalaJDK spark hadoop(注意scala和spark的版本要匹配) 2.打开idea, ...

  9. Mac系统U盘制作教程

    您可以将外置驱动器或备用宗卷用作安装 Mac 操作系统的启动磁盘. 以下高级步骤主要适用于系统管理员以及熟悉命令行的其他人员.升级 macOS 或重新安装 macOS 不需要可引导安装器,但如果您要在 ...

  10. 1.kafka

    什么是Kafka  1.Apache Kafka是一个开源消息系统,由Scala写成. 2.Kafka是一个分布式消息队列.Kafka对消息保存时根据Topic进行归类,发送消息者称为Producer ...