Nginx Reload 背后的动作

其实每个人或多或少都知道,nginx reload后的步骤,1. nginx master进程接收到信号时,进行检查配置文件,当检查结束后,会产生新的worker进程,并且销毁没有使用的worker进程,这篇博客主要是再来验证该reload步骤。

 

机器:Linux WindowsXP 4.15.0-30deepin-generic #31 SMP Fri Nov 30 04:29:02 UTC 2018 x86_64 GNU/Linux

Nginx配置:nginx version: nginx/1.10.3

Goland版本:go version go1.10.5 linux/amd64

 

nginx是个多进程的服务器,通过fock master进程的方法来实现的,启动的时候,会启动两个类型的进程,一个是master进程和若干个worker进程

我们更改完毕后,reload做的事情是

  1. 向nginx master进程发送信号,HUP signal (kill -1 Nginx_Master_PID)   
    reload 可以使用2种方法来实现

      a. 使用nginx自带的reload来实现

      b. 去向master进程发送信道 (kill -1 PID),来使nginx进程被reload掉

  1. 主进程首先会去检查配置的语言是否有效
  2. 尝试使用新的配置 如果失败,则使用旧的配置,返回错误(也可能是记录在错误log文件中的),若成成功,则新增新的配置,若旧的进程已经处理完毕,则回收,若正在处理,等待处理完毕后回收。

 

  

实验:

 

准备环境 : nginx 服务器  goweb服务器

 goweb 服务器

package main

import (
        "github.com/gin-gonic/gin"
        "github.com/labstack/gommon/log"
        "net/http"
        "time"
)

func GetVersion(c *gin.Context) {
        time.Sleep(30 * time.Second)
        c.String(200,"9090")
}

func main() {
        r := gin.Default()

        r.GET("/",GetVersion)

        //r.Run("0.0.0.0:9090")

        // 设置http服务器
        s := &http.Server{
                Addr: "0.0.0.0:9090",
                Handler: r,
                ReadTimeout: 36000 * time.Second ,
                WriteTimeout: 36000 * time.Second ,
                MaxHeaderBytes: 1 << 20 ,
        }

        log.Fatal(s.ListenAndServe())
}

nginx 配置

user nginx;
worker_processes 3;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    server {
        listen 80;
        server_name localhost;
        location / {
                proxy_read_timeout 300;
                proxy_pass http://127.0.0.1:9090;
        }
    }
}
# ps aux | grep nginx | grep -v grep
root      5669  0.0  0.4  56748  2060 ?        Ss   11:31   0:00 nginx: master process /usr/sbin/nginx
nginx     5725  0.0  0.4  57188  2028 ?        S    11:35   0:00 nginx: worker process
nginx     5726  0.0  0.4  57188  2028 ?        S    11:35   0:00 nginx: worker process
nginx     5727  0.0  0.4  57188  2028 ?        S    11:35   0:00 nginx: worker process
#
# ps aux | grep nginx | grep -v grep
root      5669  0.0  0.4  56748  2060 ?        Ss   11:31   0:00 nginx: master process /usr/sbin/nginx
nginx     5725  0.0  0.4  57188  2028 ?        S    11:35   0:00 nginx: worker process
nginx     5726  0.0  0.4  57188  2028 ?        S    11:35   0:00 nginx: worker process
nginx     5727  0.0  0.4  57188  2028 ?        S    11:35   0:00 nginx: worker process
#
posted @ 2020-02-01 19:50  pdudos  阅读(922)  评论(0编辑  收藏  举报