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
#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM