• Nginx从安装到使用


    Nginx从安装到使用

    软件安装 -- 源码编译

    1. 官网下载nginx稳定版源码包,上传到linux

    2. 安装所需的依赖环境

      ##安装gcc环境
      yum install gcc-c++
      ##安装pcre库用于解析正则
      yum install -y pcre pcre-devel
      ##zlib[解]压缩的依赖
      yum install -y zlib zlib-devel
      ##SSL 安全的加密的套接字协议层,用于HTTP安全传输
      yum install -y openssl openssl-devel
      
    3. 解压源码并编译

      ##解压源码
      tar -zxvf nginx-1.16.1.tar.gz
      ##编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
      mkdir /var/temp/nginx -p
      ##在nginx根路径下创建makefile文件
      ./configure     
      --prefix=/usr/local/nginx     
      --pid-path=/var/run/nginx/nginx.pid     
      --lock-path=/var/lock/nginx.lock     
      --error-log-path=/var/log/nginx/error.log     
      --http-log-path=/var/log/nginx/access.log     
      --with-http_gzip_static_module     
      --http-client-body-temp-path=/var/temp/nginx/client     
      --http-proxy-temp-path=/var/temp/nginx/proxy     
      --http-fastcgi-temp-path=/var/temp/nginx/fastcgi     
      --http-uwsgi-temp-path=/var/temp/nginx/uwsgi     
      --http-scgi-temp-path=/var/temp/nginx/scgi
      ##编译安装
      make && make install
      

      配置命令含义:

      命令 解释
      –prefix 指定nginx安装目录
      –pid-path 指向nginx的pid
      –lock-path 锁定安装文件,防止被恶意篡改或误操作
      –error-log 错误日志
      –http-log-path http日志
      –with-http_gzip_static_module 启用gzip模块,在线实时压缩输出数据流
      –http-client-body-temp-path 设定客户端请求的临时目录
      –http-proxy-temp-path 设定http代理临时目录
      –http-fastcgi-temp-path 设定fastcgi临时目录
      –http-uwsgi-temp-path 设定uwsgi临时目录
      –http-scgi-temp-path 设定scgi临时目录
    4. nginx常用命令

      ##启动与终止
      nginx -s [stop, quit, reopen, reload]
      ##检测配置文件语法有效性
      nginx -t
      ##指定配置文件路径启动nginx
      nginx -c filename
      

    区别一下stop与quit:

    nginx -s stop比较粗暴:不管当前不是有客户端是否有请求到达,直接强制关闭。

    nginx -s quit则相对优雅:不再接收新的请求,等现有到达的请求处理完毕,关闭nginx。

    核心配置文件解析(nginx.conf)

    从上至下依次解析每一小块内容

    1. 设置worker进程用户[Linux系统用户],涉及nginx操作的目录或文件的一些权限,默认值nobody

      user root;
      
    2. worker进程工作数设置,通常为CPU的数量

      worker_processes 1;
      
    3. nginx 日志级别debug | info | notice | warn | error | crit | alert | emerg,错误级别从左到右越来越大

    4. 设置nginx进程文件nginx.pid存放位置

      pid        logs/nginx.pid;
      
    5. 设置工作模式

      events {
          # 默认使用epoll
          use epoll;
          # 每个worker允许连接的客户端最大连接数
          worker_connections  10240;
      }
      
    6. http 是指令块,针对http网络传输的一些指令配置

      http {
      }
      
    7. include 引入外部配置,提高可读性,避免单个配置文件过大

      # 引入支持的媒体类型
      include       mime.types;
      
    8. 设定日志格式,main为定义的格式名称,access_log等可直接使用变量

      #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  logs/access.log  main;
      

      参数名解释:

      参数名 参数含义
      remote_addr 客户端IP
      remote_user 远程客户端用户名,一般为-
      time_local 时间和时区
      request 请求的url以及method
      status 响应状态码
      body_bytes_send 响应客户端内容字节数
      http_referer 记录用户从哪个链接跳转过来的
      http_user_agent 用户使用的代理,正常是浏览器
      http_x_forwarded_for 通过代理代理服务器来记录客户端的IP
    9. sendfile使用高效文件传输,提升传输性能。启用后才能使用tcp_nopush,是指当数据表累积一定大小后才发送,提高了效率。

      sendfile        on;
      tcp_nopush      on;
      
    10. keepalive_timeout设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。

      #keepalive_timeout  0;
      keepalive_timeout  65;
      
    11. gzip启用压缩,html/js/css压缩后传输会更快

      #压缩传输
      gzip on;
      #限制最小压缩,小于1字节的文件不会压缩
      gzip_min_length 1;
      #定义压缩的级别(压缩比,文件越大,压缩越多,但是cpu使用会越多)
      gzip_comp_level 3;
      
    12. server可以在http指令块中设置多个虚拟主机

      server {
              listen       88;
              server_name  localhost;
          
              location / {
                  root   html;
                  index  index.html index.htm;
              }
      }
      

      各个名词的含义:

      名词 作用
      listen 监听端口
      server_name ip、域名
      location 请求路由映射,匹配拦截
      root 请求位置
      index 首页设置

      多个server可分别定义在单独的配置文件中,通过include引入主配置文件中,方便管理。

    Nginx的日志切割

    Nginx的访问日志都存放在access.log中,随着时间推移,日志的体量也会逐渐变大,不便于管理查看,因此我们可以将他切割成很多个小文件,根据需求,以或者小时为单位进行风格存放。

    创建分割日志的脚本: cut_access_log.sh

    #!/bin/bash
    LOG_PATH="/var/log/nginx/"
    RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d+%H:%M)
    PID=/var/run/nginx/nginx.pid
    mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
    mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
    
    #向Nginx主进程发送信号,用于重新打开日志文件
    kill -USR1 `cat $PID`
    

    为脚本增加执行权限

    chmod +x cut_access_log.sh
    

    脚本的执行方式

    手动执行:./cut_access_log.sh
    通过定时任务自动执行
    1. 定时任务主要通过Linux的crontab工具实现:yum install crontabs

    2. 编辑定时任务可通过crontab -e命令,或直接编辑/etc/crontab

      示例:每分钟执行

      #1. 通过crontab -e
      */1 * * * * /usr/local/nginx/sbin/cut_my_log.sh
      #2. 编辑/etc/crontab
      SHELL=/bin/bash
      PATH=/sbin:/bin:/usr/sbin:/usr/bin
      MAILTO=root
      # For details see man 4 crontabs
      # Example of job definition:
      # .---------------- minute (0 - 59)
      # |  .------------- hour (0 - 23)
      # |  |  .---------- day of month (1 - 31)
      # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
      # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
      # |  |  |  |  |
      # *  *  *  *  * user-name  command to be executed
      */1  *  *  *  * root  /usr/local/nginx/sbin/cut_my_log.sh
      

      定时任务表达式:

      单位 星期几 年(可选)
      取值范围 0-59 0-23 1-31 1-12 1-7 2019/2020/2021...
    3. 编辑保存任务后,重启crontab即可

      service crond restart
      
    4. 常用的一些定时任务命令

      service crond start         //启动服务
      service crond stop          //关闭服务
      service crond restart       //重启服务
      service crond reload        //重新载入配置
      crontab -e                  // 编辑任务
      crontab -l                  // 查看任务列表
      

    搭建静态资源服务

    使本地的静态资源对外提供服务,通过浏览器能够直接访问

    倘若在本地有张图片的位置为:/home/resource/img,如何映射?

    server {
            listen       82;
            server_name  localhost;
    
            location / {
                root  /home/foodie-shop;
                index  index.html;
            }
    
            location /resource {
                root  /home;
            }
            location /static {
                alias  /home/resource;
            }
    

    注意: 同一个server中可以同时存在多个location共享同一个端口,但是location后面的匹配规则不能相同。另,静态资源的映射自然不需要index(首页设置)。

    配置好location需要重启nginx,以类似如下路径在浏览器访问:

    http://192.168.150.11:82/resource/img/1.jpghttp://192.168.150.11:82/static/img/1.jpg

    区别root与alias:

    root指定具体的根路径,location匹配的资源位置紧跟在根路径之后。

    alias为location匹配的路径命名一个别名。

    location匹配规则拓展:

    “~”表示匹配正则表达式 如 location ~  .(GIF|png|jpg)
    “^”尖括号表示非
    “*”表示不区分大小写
    “=”表示精确匹配
    

    Nginx.pid打开失败以及失效问题的解决

    当我们使用nginx时可能会遇到如下的情况

    [root@localhost sbin]# ./nginx -s reload
    #第一种情况
    nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
    #第二种情况
    nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)
    

    如果在编译源码时指定了pid-path,一般不会出现第一种错误。/usr/local/nginx/logs/nginx.pid是nginx默认存放pid的位置,可在nginx.conf中修改

    尝试解决: 根据提示看出是不存在相关的文件或者文件夹

    1. 若nginx下没有logs目录,则makedir logs

    2. 指定配置文件位置,重新生成nginx.pid/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  • 相关阅读:
    day34
    day33 UDP、进程
    Java RMI 框架(远程方法调用)
    Maven查看JAR包的依赖关系
    连接Oracle数据库的时候报了“Got minus one from a read call”
    Http、Socket和WebService协议之间的区别
    http 协议
    其它
    Jmeter-CSV data set config参数化
    英文
  • 原文地址:https://www.cnblogs.com/randolf/p/14443868.html
Copyright © 2020-2023  润新知