type
Post
status
Published
date
Jul 7, 2022
slug
summary
Nginx是一个高性能的HTTP反向代理web服务器 Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
tags
服务器
web
password
Property
Jul 7, 2022 02:37 PM
category
运维笔记
icon

Nginx基础

nginx服务的软件特点
  1. 支持高并发,消耗内存资源少
  1. 具有多种功能
    1. 网站web服务功能 ---apache
      网站负载均衡功能 ---LVS
      网站缓存服务 —-Scquid
  1. 在多种系统平台都可以进行部署
  1. nginx实现网络通讯时使用的时异步网络IO模型:epoll模型(apache--selec模型)
    1. notion image
       
 

nginx软件的安装部署过程

两种安装方式:
  1. yum安装软件
    1. a使用官方yum源进行安装安装的是最新版本软件目录结构比较标准(推荐)
      b使用非官方yum源进行安装安装的不是最新版目录结构会发生变化
  1. 编译安装软件
    1. wget http://nginx.org/download/nginx-1.20.2.tar.gz
      1. 按照前解决软件的依赖 openssl-devel pcre-devel
    2. 解压软件,并进入目录
      1. tar xf nginx-1.20.2.tar.gz
    3. 编译按照
      1. 进行配置操作
        1. ./confiqure--prefix=--user=USER
          #说明 --prefix=PATH set installation prefix 指定程序安装路径 --user=USER set non-privileged user for worker processes 设置一个虚拟用户管理worker进程(安全) --group=GROUP set non-privileged group for worker processes 设置一个虚拟用户组管理worker进程(安全)
      2. 软件编译
        1. make install
Yum官方源安装方法:
1.更新nginx官方yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
  1. 安装软件
    1. yum install -y nginx
  1. 启动nginx服务,检查服务是否安装正确
    1. systemctl start nginx
      systemctl enable nginx 设置开机自启
      systemctl status nginx 查看启动状态
查看软件的目录结构
  • /etc/logrotate.d 实现nginx日志文件定时切割处理
    • 1 利用脚本实现切割
      #!/bin/bash mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).1og systemctl restart nginx
      2 利用专用文件切割程序
      # rotate log files weekly weekly #定义默认日志切割的周期 # keep 4 weeks worth of backlogs rotate 4 #定义只保留几个切割后的文件 # create new (empty) log files after rotating old ones create #创建出一个相同的源文件 # use date as a suffix of the rotated file dateext #定义角标(扩展名称信息) # uncomment this if you want your log files compressed #compress # 是否对切割后的文件进行压缩处理 # RPM packages drop log rotation information into this directory include /etc/logrotate.d #加载包含/etc/logrotate.d目录中的配置文件 # no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { #单独对某个文件进行切割配置 monthly create 0664 root utmp minsize 1M #最小大小为1M,小于1M不进行切割 rotate 1 }
    • nginx的重要目录
        1. /etc/nginx 配置文件
        1. /var/log/nginx 日志文件
        1. usr/bin/nginx 命令文件
        1. /usr/share/nginx/html 站点目录??? 图片 附件信息 音频 视频
    • nginx服务配置文件
      • /etc/nginx/nginx.conf # 主配置文件
        -----配置文件主区域配置----- user nginx; #定义woker进程管理的用户 #补充:nginx的进程 #master process:主进程-—-管理服务是否能够正常运行 #worker process:工作进程---处理用户的访问请求 #ps -ef|grep nginx 查看nginx进程 worker_processes 2; #定义有几个worker进程 error_log /var/log/nginx/error.log notice; #定义错误日志路径信息 pid /var/run/nginx.pid; # 定义pid文件路径信息 -------配置文件事件区域-------- events { worker_connections 1024; #一个worker进程可以同时接收1024访问请求 } -------配置http区域 http { include /etc/nginx/mime.types; #加载一个配置文件 default_type application/octet-stream; #指定默认识别文件类别 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; #指定日志路径 sendfile on; #tcp_nopush on; keepalive_timeout 65; #定义超时时间 #gzip on; include /etc/nginx/conf.d/*.conf; #加载一个配置文件 }
        /etc/nginx/conf.d/default 扩展配置文件
        ----server区域信息(配置一个网站)---- server { listen 80; #指定监听的端口 server_name localhost; # 指定网站域名 location / { root /usr/share/nginx/html; # 定义站点目录 index index.html index.htm; # 定义首页文件 } error_page 500 502 503 504 /50x.html; # 错误访问显示的页面 location = /50x.html { root /usr/share/nginx/html; } }
CentOS7的磁盘分区操作示例文章

  • Waline
  • Valine