nginx部署以及说明

nginx

1. nginx简介

Nginx是一个高性能的开源Web服务器和反向代理服务器。它由Igor Sysoev于2004年创建,并于2008年首次发布。Nginx的设计目标是提供高性能、稳定性和低资源消耗的Web服务器解决方案。

Nginx的特点包括:

  1. 高性能:Nginx采用异步、事件驱动的架构,能够处理大量并发连接,具有较低的内存消耗和更高的吞吐量。它可以处理数以千计的并发连接,适用于高流量的网站和应用程序。
  2. 反向代理和负载均衡:Nginx可以作为反向代理服务器,将客户端请求转发给后端服务器,并根据负载均衡算法分配请求。这使得Nginx可以将流量分发到多个服务器上,提高系统的可靠性和性能。
  3. 静态文件服务:Nginx能够高效地提供静态文件的服务,通过使用磁盘缓存和发送文件的方式,减轻了后端应用服务器的负担,提高了网站的响应速度。
  4. 可扩展性:Nginx支持模块化的架构,可以通过加载不同的模块来扩展其功能。它有丰富的第三方模块和插件,可以满足各种不同的需求,如安全性增强、SSL/TLS加密、缓存加速等。
  5. 简单配置和易于使用:Nginx的配置文件采用简单的文本格式,易于理解和修改。它提供了丰富的配置选项,可以灵活地进行定制和调整。

2. 代理模式

2.1 正向代理

正向代理(Forward Proxy): 正向代理是一种代理服务器,作为客户端和目标服务器之间的中间人,代表客户端发送请求并获取响应。当客户端发送请求时,请求首先发送到正向代理服务器,然后由代理服务器转发请求到目标服务器,并将目标服务器的响应返回给客户端。客户端对目标服务器的存在是不可见的,只知道与正向代理服务器进行通信。

正向代理的主要作用包括:

  1. 访问限制:正向代理可以绕过网络访问限制,例如访问被封锁的网站或绕过防火墙。
  2. 匿名性:正向代理可以隐藏客户端的真实IP地址,提供匿名性。
  3. 缓存:代理服务器可以缓存目标服务器的响应,提高访问速度和减轻目标服务器的负载。
  4. 安全性:正向代理可以提供额外的安全性,例如过滤恶意内容或加密通信

2.2 反向代理

反向代理(Reverse Proxy): 反向代理是一种代理服务器,作为目标服务器和客户端之间的中间人,代表目标服务器接收请求并返回响应。当客户端发送请求时,请求首先发送到反向代理服务器,然后由代理服务器根据配置将请求转发到相应的目标服务器,并将目标服务器的响应返回给客户端。客户端对目标服务器的存在是不可见的,只知道与反向代理服务器进行通信。

反向代理的主要作用包括:

  1. 负载均衡:反向代理可以根据负载情况将请求分发到多个目标服务器,以实现负载均衡,提高系统的性能和可靠性。
  2. 安全性:反向代理可以提供额外的安全性,例如过滤恶意请求、防止DDoS攻击或提供SSL加密。
  3. 缓存:代理服务器可以缓存目标服务器的响应,提高访问速度和减轻目标服务器的负载。
  4. 简化架构:反向代理可以隐藏后端服务器的细节,简化系统架构和管理。

2.3 透明代理

透明代理(Transparent Proxy): 透明代理是一种代理服务器,对客户端和目标服务器之间的通信是透明的,客户端和目标服务器都不知道代理服务器的存在。透明代理通常是在网络中的路由器或防火墙上实现的,它可以在不需要客户端进行任何配置的情况下截获和转发网络流量。

透明代理的主要作用包括:

  1. 缓存和访问控制:透明代理可以缓存常用的内容,提高访问速度,并可以通过访问控制策略限制特定网站或内容的访问。
  2. 安全性:透明代理可以监测和过滤网络流量,提供额外的安全性,例如防止恶意内容或攻击的传播。
  3. 流量控制:透明代理可以控制网络流量的传输速率,以避免网络拥塞或滥用。

3. nginx的模块与工作原理

nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

nginx的模块从结构上分为核心模块、基础模块和第三方模块

HTTP模块、EVENT模块和MAIL模块等属于核心模块
HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块
HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块
用户根据自己的需要开发的模块都属于第三方模块。正是有了如此多模块的支撑,nginx的功能才会如此强大

nginx模块从功能上分为三类,分别是:

Handlers(处理器模块)。此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个
Filters(过滤器模块)。此类模块主要对其他处理器模块输出的内容进行修改操作,最后由nginx输出
Proxies(代理器模块)。就是nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能
nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等

nginx基本模块:所谓基本模块,指的是nginx默认的功能模块,它们提供的指令,允许你使用定义nginx基本功能的变量,在编译时不能被禁用,包括:
核心模块:基本功能和指令,如进程管理和安全。常见的核心模块指令,大部分是放置在配置文件的顶部
事件模块:在Nginx内配置网络使用的能力。常见的events(事件)模块指令,大部分是放置在配置文件的顶部
配置模块:提供包含机制
具体的指令,请参考nginx的官方文档

4. nginx工作原理

nginx的模块直接被编译进nginx,因此属于静态编译方式。

启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。

在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。

在这里插入图片描述

下图展示了nginx模块一次常规的HTTP请求和响应的过程

在这里插入图片描述

下图展示了基本的WEB服务请求步骤

在这里插入图片描述

5. nginx安装与配置

5.1 nginx安装

//创建用户
[root@slave ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@slave ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++

//创建日志存放目录
[root@slave ~]# mkdir -p /var/log/nginx
[root@slave ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx源码包
[root@slave ~]# wget https://nginx.org/download/nginx-1.22.1.tar.gz
[root@slave ~]# tar xf nginx-1.22.1.tar.gz 
[root@slave ~]# ls
anaconda-ks.cfg  mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz  nginx-1.22.1  nginx-1.22.1.tar.gz
[root@slave ~]# cd nginx-1.22.1
[root@slave nginx-1.22.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

//编译安装
[root@slave nginx-1.22.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@slave nginx-1.22.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

5.2 nginx安装后配置

//配置环境变量
[root@slave nginx-1.22.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@slave nginx-1.22.1]# . /etc/profile.d/nginx.sh
//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}

//启动nginx
[root@slave nginx-1.22.1]# nginx
[root@slave nginx-1.22.1]# cd
[root@slave ~]# 
[root@slave ~]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           511                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:*                    
[root@slave ~]# nginx -s stop
[root@slave ~]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:* 

提供网站服务

[root@slave ~]# cd /usr/local/nginx/html
[root@slave html]# echo "hello world" > index.html
[root@slave html]# systemctl disable --now firewalld
[root@slave html]# setenforce 0
[root@slave html]# nginx
[root@slave html]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           511                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:*

在这里插入图片描述

能够访问到

编写service文件

[root@slave ~]# cd /usr/lib/systemd/system
[root@slave system]# cp sshd.service nginx.service
[root@slave system]# vi nginx.service 
[root@slave system]# cat nginx.service 
[Unit]
Description=nginx server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@slave system]# systemctl daemon-reload
[root@slave system]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:*                    
[root@slave system]# systemctl start nginx
[root@slave system]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           511                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:*

6. nginx的配置文件详解

主配置文件:/usr/local/nginx/conf/nginx.conf

  • 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
  • 可以在启动nginx时通过-c选项来指定要读取的配置文件

nginx常见的配置文件及其作用

配置文件作用
nginx.confnginx的基本配置文件
mime.typesMIME类型关联的扩展文件
fastcgi.conf与fastcgi相关的配置
proxy.conf与proxy相关的配置,编译proxy或者yum装的时候有
sites.conf配置nginx提供的网站,包括虚拟主机,编译proxy或者yum装的时候有

6.1 nginx.conf配置详解

  • nginx.conf的内容分为以下几段:
    • main配置段:全局配置段。其中main配置段中可能包含event配置段
    • event {}:定义event模型工作特性
    • http {}:定义http协议相关的配置
  • 配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...];

支持使用变量:

  • 内置变量:模块会提供内建变量定义
$remote_addr  远程地址
$remote_user  远程用户
$time_local   本地时间
$request 请求资源
$status 状态
$body_bytes_sent  发送主体的字节数
$http_referer 从哪里跳转过来的
$http_user_agent 是什么浏览器
$http_x_forwarded_for 从哪里转发过来的
  • 自定义变量:set var_name value

6.2 用于调试、定位问题的配置参数

daemon {on/off};         //daemon选项可以打开或关闭守护进程模式,调试时应关闭,默认开启
master_process {on|off};    //是否以master/worker模型来运行nginx,调试时可以设置为off,默认打开
error_log 位置 级别;    //配置错误日志
[root@slave ~]# vim /usr/local/nginx/conf/nginx.conf
[root@slave ~]# head /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;
daemon off;

[root@slave conf]# nginx   //会卡在这里不动,其实nginx已经开启了,我们开启一个新的终端

[root@slave ~]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           511                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:*
[root@slave ~]# ps -ef |grep nginx
root       83881   79021  0 16:29 pts/0    00:00:00 nginx: master process nginx
nginx      83882   83881  0 16:29 pts/0    00:00:00 nginx: worker process
root       84319   82060  0 16:31 pts/1    00:00:00 grep --color=auto nginx
//如果你希望将nginx放到前台运行可以将daemon参数设置为off

//将master_process设置为off
[root@slave conf]# vim nginx.conf
master_process off;
[root@slave conf]# nginx -s stop
[root@slave conf]# nginx
[root@slave conf]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           511                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           128                       [::]:22                     [::]:*                    
[root@slave conf]# ps -ef |grep nginx      //这里nginx就不会分master进程和worker进程了
root       85454       1  0 16:35 ?        00:00:00 nginx
root       85541   79021  0 16:35 pts/0    00:00:00 grep --color=auto nginx

//设置错误日志
[root@slave conf]# vim nginx.conf     //这三个日志选项只能开启一个
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
[root@slave conf]# cd ../logs/
[root@slave logs]# ls           //开启之后就会在nginx目录的logs目录生成日志文件
error.log  nginx.pid

6.3 正常运行必备的配置参数

//指定用户开启nginx进程,默认为nginx用户
[root@slave conf]# head nginx.conf   //指定student用户开启进程

user  student;
worker_processes  1;
[root@slave conf]# nginx -s stop
[root@slave conf]# nginx
[root@slave conf]# ps -ef |grep nginx
root       92014       1  0 17:00 ?        00:00:00 nginx: master process nginx
student    92015   92014  0 17:00 ?        00:00:00 nginx: worker process
root       92039   79021  0 17:00 pts/0    00:00:00 grep --color=auto nginx

6.4 优化性能的配置参数

worker_processes n;    //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...;    //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
    0000 0001   //第一颗cpu核心
    0000 0010   //第二颗cpu核心
    0000 0100   //第三颗cpu核心
    0000 1000   //第四颗cpu核心
    0001 0000   //第五颗cpu核心
    0010 0000   //第六颗cpu核心
    0100 0000   //第七颗cpu核心
    1000 0000   //第八颗cpu核心
timer_resolution interval;    //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number;    //指明worker进程的nice值

示例

//启动四个进程,绑定四个核心
[root@slave conf]# head nginx.conf

user  nginx;
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;

[root@slave conf]# nginx -s stop
[root@slave conf]# nginx
[root@slave conf]# ps -ef |grep nginx
root       93379       1  0 17:05 ?        00:00:00 nginx: master process nginx
nginx      93380   93379  0 17:05 ?        00:00:00 nginx: worker process
nginx      93381   93379  0 17:05 ?        00:00:00 nginx: worker process
nginx      93382   93379  0 17:05 ?        00:00:00 nginx: worker process
nginx      93383   93379  0 17:05 ?        00:00:00 nginx: worker process
root       93445   79021  0 17:05 pts/0    00:00:00 grep --color=auto nginx

6.5 事件相关的配置:event{}段中的配置参数

accept_mutex {off|on};    //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求

lock_file file;    //accept_mutex用到的互斥锁锁文件路径

Syntax:	accept_mutex on | off;
Default:	
accept_mutex off;
Context:	events

Syntax:	lock_file file;
Default:	
lock_file logs/nginx.lock;
Context:	main



use [epoll | rtsig | select | poll];    //指明使用的事件模型,建议让nginx自行选择
  
  
worker_connections #;    //每个进程能够接受的最大连接数
  
Syntax:	worker_connections number;
Default:	
worker_connections 512;
Context:	events

  
4x1024  
配置文件:
events {
    worker_connections  8000;
} 

6.6 网络连接相关的配置参数

keepalive_timeout number;    //长连接的超时时长,默认为65s

keepalive_requests number;    //在一个长连接上所能够允许请求的最大资源数

Syntax:	keepalive_requests number;
Default:	
keepalive_requests 1000;
Context:	http, server, location
This directive appeared in version 0.8.0.

keepalive_disable [msie6|safari|none];    //为指定类型的UserAgent禁用长连接

Syntax:	keepalive_disable none | browser ...;
Default:	
keepalive_disable msie6;
Context:	http, server, location

msie6:微软IE浏览器6

tcp_nodelay on|off;    //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
delay:延迟

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


client_header_timeout number;    //读取http请求报文首部的超时时长


client_body_timeout number;    //读取http请求报文body部分的超时时长


send_timeout number;    //发送响应报文的超时时长

6.7 fastcgi的相关配置参数

LNMP:php要启用fpm模型

location ~ \.php$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;      //定义反向代理
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  include fastcgi_params;
}

6.8 常需要进行调整的参数

worker_processes
worker_connections
worker_cpu_affinity
worker_priority

6.9 nginx作为web服务器时使用的配置:http{}段的配置参数

1. http{...}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:

http {//协议级别
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
  upstream {//负载均衡配置
    ...
  }
  server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>
    listen 80;
    server_name localhost;
    location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
      root html;
      index index.html index.htm;
    }
  }
}

2. http{}段配置指令:
server {}:定义一个虚拟主机,示例如下:
server {
        listen       80;
        server_name  www.ayachinene.com;
        root "/vhosts/web";
        
3. 监听指定端口
listen address[:port];
listen port;
server_name NAME [...]; 后面可跟多个主机,名称可使用正则表达式或通配符

4.当有多个server时,匹配顺序如下:
      1.先做精确匹配检查
      2.左侧通配符匹配检查,如*.idfsoft.com
      3.右侧通配符匹配检查,如mail.*
      4.正则表达式匹配检查,如~ ^.*\.idfsoft\.com$
      5.default_server(默认配置)

5.各种参数说明
root path; 设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径
alias path; 用于location配置段,定义路径别名
index file; 默认主页面
index index.php index.html;   //这样默认访问的是php的界面

# error_page code [...] [=code] URI | @name 根据http响应状态码来指明特用的错误页面,例如 error_page 404 /404_customed.html
  将error_page  404              /404.html; 取消注释,/404.html这个文件名可以自定义
在html目录下创建一个名为404.html的文件,里面写入自己想展示的html界面

error_page  404              /404.html;   //取消注释
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  index.php
[root@localhost html]# echo "error" >> 404.html
[root@localhost html]# ls
404.html  50x.html  index.php
[root@localhost html]# systemctl restart nginx

在这里插入图片描述

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"';
                         http的用户代理     记录链路中所有代理服务器的请求
access_log  logs/access.log  main;

//注意:此处可用变量为nginx各模块内建变量

找到server下access_log  logs/host.access.log  main;这一行,取消注释
然后在http中找到下面这三行,全部取消注释
[root@localhost html]# cd ../logs/
[root@localhost logs]# ls
access.log  nginx.pid
[root@localhost logs]# tail access.log 
192.168.37.1 - - [18/Oct/2023:17:13:28 +0800] "GET /45646 HTTP/1.1" 404 6 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" "-"

//自定义日志
将上面三行复制一份,直接粘贴在原来的下面,并修改如下:
log_format  nihao  '$remote_addr - [$time_local] "$request" '
                      '$status "$http_referer" "$http_user_agent"';

    access_log  logs/access.log  nihao;
    
[root@localhost logs]# systemctl restart nginx
[root@localhost logs]# tail access.log
192.168.37.1 - [18/Oct/2023:17:18:03 +0800] "GET /45646 HTTP/1.1" 404 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

//location区段,通过指定模式来与客户端请求的URI相匹配
功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能

语法:location [ 修饰符 ] pattern {......}

6.10 访问控制

//用于location段
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开
示例:
allow 192.168.1.1/32 172.16.0.0/16;
deny all;
 
在配置文件中的server一项下面添加
        location / {
            allow 192.168.187.0/24;   ----- 允许187这一网段的所有ip访问
            deny 192.168.187.100;     ----- 禁止187网段的100禁止访问
            root   html;
            index  index.html index.htm;
        }

6.11 基于用户认证

1.配置说明
auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"

2.user_auth_file内容格式为:
username:password
# 这里的密码为加密后的密码串,建议用htpasswd来创建此文件:
htpasswd -c -m /path/to/.user_auth_file USERNAME

3.修改页面
[root@localhost html]# touch index.html
[root@localhost html]# vim index.html 
[root@localhost html]# cat index.html 
hello world
<a href="/nene/wuhu.html">xjp</a>

在这里插入图片描述

页面成功显示

//我们要实现点击跳转页面之后,需要身份验证
[root@localhost html]# mkdir nene
[root@localhost html]# cd nene/
[root@localhost nene]# echo "Alicesoft 1989" >> wuhu.html

//编辑配置文件
location /nene {
            auth_basic "qwerqwer";    
            auth_basic_user_file "/usr/local/nginx/conf/pass";  
            root html;
            index auth_page.html;
        }
//下载命令依赖包
[root@localhost html]# yum -y install httpd-tools
[root@localhost conf]# htpasswd -c -m pass nene
New password: 
Re-type new password: 
Adding password for user nene
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf          scgi_params.default
fastcgi.conf.default    koi-win             nginx.conf.default  uwsgi_params
fastcgi_params          mime.types          pass                uwsgi_params.default
fastcgi_params.default  mime.types.default  scgi_params         win-utf
[root@localhost conf]# cat pass 
nene:$apr1$h.GtUPaA$.0zyLTNllJTGy6Sz1onHE/

在这里插入图片描述

需要输入正确的用户和密码才能继续访问

6.12 https配置

server {
  listen       443 ssl;
  server_name  www.idfsoft.com;
  ssl_certificate      /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key  /etc/nginx/ssl/nginx.key;
  ssl_session_cache    shared:SSL:1m;
  ssl_session_timeout  5m;
  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers  on;
  location / {
    root   html;
    index  index.html index.htm;
  }
}

6.13 开启状态界面

//在配置文件的server项中新建一项
location /status {
            stub_status on;
        }
[root@localhost conf]# systemctl restart nginx

在这里插入图片描述

# 访问状态页面的方式:http://server_ip/status

状态页面信息详解:

状态码表示的意义
Active
connections 2
当前所有处于打开状态的连接数
accepts总共处理了多少个连接
handled成功创建多少握手
requests总共处理了多少个请求
Readingnginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数
Writingnginx返回给客户端的Header信息数,表示请求已经接收完成,
且正处于处理请求或发送响应的过程中的连接数
Waiting开启keep-alive的情况下,这个值等于active - (reading + writing),
意思就是Nginx已处理完正在等候下一次请求指令的驻留连接

7. zabbix监控nginx服务状态界面

zabbix服务端安装教程

//安装zabbix_agentd端
[root@localhost ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/6.4/zabbix-6.4.6.tar.gz
[root@localhost ~]# tar xf zabbix-6.4.6.tar.gz
[root@localhost ~]# cd zabbix-6.4.6

//创建zabbix用户
[root@localhost zabbix-6.4.6]# groupadd --system zabbix
[root@localhost zabbix-6.4.6]# useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix

//安装依赖包
[root@localhost zabbix-6.4.6]# yum -y install gcc gcc-c++ make pcre-devel

//编译安装
[root@localhost zabbix-6.4.6]# ./configure --enable-agent
[root@localhost zabbix-6.4.6]# make install

# 编写配置文件中的这三行
Server=192.168.37.160               # 指定zabbix服务端的ip
ServerActive=192.168.37.160         # 指定zabbix服务端的ip
Hostname=nginx            # 自定义命名,以后在服务端用这个名字来连接这台主机

[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -antl
State      Recv-Q     Send-Q           Local Address:Port            Peer Address:Port     Process     
LISTEN     0          511                    0.0.0.0:80                   0.0.0.0:*                    
LISTEN     0          128                    0.0.0.0:22                   0.0.0.0:*                    
LISTEN     0          128                    0.0.0.0:10050                0.0.0.0:*                    
LISTEN     0          2048                 127.0.0.1:9000                 0.0.0.0:*                    
LISTEN     0          80                     0.0.0.0:3306                 0.0.0.0:*                    
LISTEN     0          128                       [::]:22                      [::]:*                    
LISTEN     0          80                        [::]:3306                    [::]:*

在服务端添加主机

在这里插入图片描述

编写查看状态页面的脚本

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# vim nginx.sh
[root@localhost scripts]# cat nginx.sh 
#! /bin/bash
 
case $1 in
    "reading")
        curl -s http://192.168.37.120/status | awk 'NR==4{print $2}'
        ;;
    "writing")
        curl -s http://192.168.37.120/status | awk 'NR==4{print $4}'
        ;;
    "waiting")
        curl -s http://192.168.37.120/status | awk 'NR==4{print $6}'
esac

编辑zabbix_agentd配置文件

UnsafeUserParameters=1    //取消注释
UserParameter=nginx_agentd[*],/bin/bash /scripts/nginx.sh $1   //添加这一行
//在zabbix服务端测试
[root@localhost ~]# zabbix_get -s 192.168.37.120 -k nginx_agentd[reading]
0

创建监控项

在这里插入图片描述

我这里随意添加了触发器的值来触发监控项

在这里插入图片描述

触发报警

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Ubuntu中部署Nginx可以按照以下步骤进行: 1. 安装Nginx: 首先需要更新源列表,运行命令`apt-get update`,然后安装Nginx,运行命令`apt-get install nginx`。可以通过运行`nginx -v`来检查Nginx是否成功安装。 2. 配置Nginx: Nginx的默认配置文件位于`/etc/nginx/nginx.conf`。可以使用命令`vi /etc/nginx/nginx.conf`来编辑该文件。根据需要修改配置文件中的相关参数。 3. 启动Nginx: 在终端中执行命令`service nginx start`来启动Nginx服务。可以使用`service nginx status`命令来检查Nginx是否成功启动。 4. 验证部署: 在浏览器中输入服务器的IP地址,如果看到Nginx的默认欢迎页面,则说明Nginx部署成功。 请注意,这只是一个基本的Nginx部署过程,你可能需要根据自己的需求进行进一步的配置和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ubuntu使用nginx部署vue项目](https://blog.csdn.net/qq_43589143/article/details/123017842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Ubuntu安装Nginx部署vue项目](https://blog.csdn.net/xwh3165037789/article/details/124438106)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值