Nginx introduction, installation and configuration file introduction

Software Features of Nginx Service
  • Support high concurrency and consume less memory resources

  • with multiple functions

    • Website web service (apache)
    • Website load balancing function (LVS)
    • Website caching service (Squid)
  • Can be deployed on a variety of system platforms

  • Nginx uses an asynchronous network IO model when implementing network communication: epoll model

    • Apache uses the select model
      • epoll model: find people, check the registration information of people.
        • When children go to the toilet, they all stand in a certain position in the classroom and ask together
      • select model: find someone, ask each room one by one - linear polling
        • When the children go to the toilet, ask them one by one
    index select epoll
    performance As the number of connections increases, the performance drops sharply, handling thousands of concurrent connections, the performance is very poor There is basically no drop in performance as the number of connections increases. Good performance when handling thousands of concurrent connections
    Connections The number of connections is limited, and the maximum number of connections to be processed does not exceed 1024. If the number of connections to be processed exceeds 1024, you need to modify the FD_SETSIZE macro and recompile Unlimited number of connections
    internal processing mechanism linear polling callback callback
    development complexity Low middle
The installation and deployment process of Nginx software
  • yum install software

    • Use the official yum source to install, the latest version is installed, and the software directory structure is relatively standard (recommended)
    • Use unofficial yum sources for installation, the installation is not the latest version, the directory structure will change
  • Compile and install software

    • wget download package

    • Resolve software dependencies:

      • openssl-devel
      • pcre-devel
    • Unzip the downloaded directory and enter the directory

    • Compile and install the trilogy

      • Perform configuration operations

        ./configure --prefix= --group=
        --prefix=PATH:指定程序安装路径
        --user=USER:设置一个虚拟用户管理worker进程(安全)
        --group=GROUP:设置一个虚拟组管理worker进程(安全)
        
      • Compile the software

        make install
        make 编译(翻译)
        C:gcc
        python:python解释器
        
Yum official source installation method:
  • The first process: update nginx official yum source

    https://nginx.org/en/linux_packages.html#RHEL
    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
    
    
    
    ##如果验证失败
    将gpgcheck=1改为0或者注释掉
    enabled:是否启用
    
  • The second process: yum installs nginx software

    yum install nginx
    
  • The third process: start nginx service

    systemctl start nginx
    systemctl enable nginx
    
    • Test access to nginx service

      输入ip地址信息
      
View the directory structure of the software
rpm -ql nginx
path information type information Function description
/etc/logrotale.d/nginx configuration file Used for log rotation training cutting
/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
Configuration file
configuration directory
nginx main configuration file
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
configuration file cgi, Fastcgi, uwcgi configuration files
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf
configuration file nginx encoding conversion mapping file
/etc/nginx/mime.types configuration file content-type and extension of http protocol
/usr/lib/systemd/system/nginx.debug.service
/usr/lib/systemd/system/nginx.service
configuration file nginx service daemon management file
/etc/nginx/modules
/usr/lib64/nginx
/usr/lib64/nginx/modules
directory information nginx module directory
/usr/sbin/nginx
/usr/sbin/nginx-debug
command information nginx terminal management commands
/usr/share/doc/nginx-1.14.2
/usr/share/man/man8/nginx.8.gz
directory information nginx help manual information
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/index.html
directory information nginx default site directory
/var/cache/nginx directory information nginx cache directory information
/var/log/nginx directory information nginx log directory information
  • /etc/logrotate.d: Realize the timing cut processing of nginx log files

    • Using scripts to achieve cutting

      #!/bin/bash
      
      #将当天的日志重命名
      mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).log
      #生成新的一天日志文件
      systemctl restart nginx
      
    • Use a dedicated file cutting program - logrotate

      vim /etc/logrotate.conf
      
      # see "man logrotate" for details
      # rotate log files weekly:定义默认日志切割周期
      weekly
      
      #daily:每天、weekly:每周、monthy:每月
      
      # 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:加载包含/etc/logrotate.d目录中文件配置
      include /etc/logrotate.d
      
      # no packages own wtmp and btmp -- we'll rotate them here:单独对某个文件进行切割配置
      /var/log/wtmp {
          monthly
          create 0664 root utmp
              minsize 1M
          rotate 1
      }
      
      #minsize:当文件最小要到达的大小时才进行切割
      #maxsize:当文件最大到达的大小时进行切割
      #这两个参数优先于设置的切割周期
      
      /var/log/btmp {
          missingok
          monthly
          create 0600 root utmp
          rotate 1
      }
      
    • View the subconfiguration file: cd /etc/logrotate.d/

      ll
      -rw-r--r--. 1 root root  91 Apr 11  2018 bootlog
      -rw-r--r--. 1 root root 160 Sep 15  2017 chrony
      -rw-r--r--  1 root root  93 Apr 28  2021 firewalld
      -rw-r--r--. 1 root root 224 Oct 30  2018 syslog
      -rw-r--r--. 1 root root 100 Oct 31  2018 wpa_supplicant
      -rw-r--r--. 1 root root 103 Nov  5  2018 yum
      
      [root@pert logrotate.d]# cat syslog 
      /var/log/cron
      /var/log/maillog
      /var/log/messages
      /var/log/secure
      /var/log/spooler
      {
          missingok
          sharedscripts
          postrotate
              /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
          endscript
      }
      
Catalog summary
  • /etc/nginx: configuration file
  • /var/log/nginx: log files
  • /usr/bin/nginx: command file
  • /usr/share/nginx/html: site directory
nginx service configuration file
  • Main configuration file: /etc/nginx.conf

    • Part 1: Configuration file main area

      #定义worker进程管理的用户(ps -ef | grep nginx) 有两个nginx进程
      user nginx
      
      #定义有几个worker进程,进程越多,处理并发能力越强。数量最好===cpu核数(lscpu)
      worker_processes 1;
      
      #定义错误日志
      error_log /var/log/nginx/error.log warn;
      
      #定义pid文件路径信息:存在服务启动,不存在就代表没启动
      pid /var/run/nginx/pid;
      
    • Part II: Profile Events Area

      #一个worker进程可以同时接收1024访问请求
      events {
      	worker_connections 1024;
      }
      
    • Part 3: Configure the http zone

      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;
      	#加载配置文件(这里是default)
      	include /etc/nginx/conf.d/*.conf;
      }
      
    • Replenish

      #nginx进程
      master process:主进程,管理服务是否能够正常运行(boss)
      worker process:工作进程,处理用户的访问请求(员工)
      
  • Extended configuration file (virtual host configuration file): /etc/nginx/nginx.d/default

    • The fourth part: server area information (configure a website - a virtual host)

      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 = /50.html {
      		root /user/share/nginx/html 
      	}
      }
      

Guess you like

Origin blog.csdn.net/s_frozen/article/details/128841947