前言

若是已經看過前面 「十六部連載,兩部番外」,相信您的能力已經有所提高nginx

那麼,如今今天來講說簡單部署後端服務的事兒 🤓git

作什麼

在本章節,咱們將簡單介紹 Nginx 以及使用 Nginx 來完成對 go-gin-example 的部署,會實現反向代理和簡單負載均衡的功能github

Nginx

是什麼

Nginx 是一個 Web Server,能夠用做反向代理、負載均衡、郵件代理、TCP / UDP、HTTP 服務器等等,它擁有不少吸引人的特性,例如:golang

  • 以較低的內存佔用率處理 10,000 多個併發鏈接(每10k非活動HTTP保持活動鏈接約2.5 MB )
  • 靜態服務器(處理靜態文件)
  • 正向、反向代理
  • 負載均衡
  • 經過OpenSSL 對 TLS / SSL 與 SNI 和 OCSP 支持
  • FastCGI、SCGI、uWSGI 的支持
  • WebSockets、HTTP/1.1 的支持
  • Nginx + Lua

安裝

請右拐谷歌或百度,安裝好 Nginx 以備接下來的使用segmentfault

簡單講解

經常使用命令

  • nginx:啓動 Nginx
  • nginx -s stop:馬上中止 Nginx 服務
  • nginx -s reload:從新加載配置文件
  • nginx -s quit:平滑中止 Nginx 服務
  • nginx -t:測試配置文件是否正確
  • nginx -v:顯示 Nginx 版本信息
  • nginx -V:顯示 Nginx 版本信息、編譯器和配置參數的信息

涉及配置

一、 proxy_pass:配置反向代理的路徑。須要注意的是若是 proxy_pass 的 url 最後爲
/,則表示絕對路徑。不然(不含變量下)表示相對路徑,全部的路徑都會被代理過去後端

二、 upstream:配置負載均衡,upstream 默認是以輪詢的方式進行負載,另外還支持四種模式,分別是:api

(1)weight:權重,指定輪詢的機率,weight 與訪問機率成正比緩存

(2)ip_hash:按照訪問 IP 的 hash 結果值分配服務器

(3)fair:按後端服務器響應時間進行分配,響應時間越短優先級別越高

(4)url_hash:按照訪問 URL 的 hash 結果值分配

部署

nginx -t
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
/usr/local/etc/nginx/

反向代理

反向代理是指以代理服務器來接受網絡上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給請求鏈接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。(來自百科)

image

配置 hosts

/etc/hosts
127.0.0.1       api.blog.com

配置 nginx.conf

打開 nginx 的配置文件 nginx.conf(個人是 /usr/local/etc/nginx/nginx.conf),咱們作了以下事情:

http://127.0.0.1:8000/
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8081;
        server_name  api.blog.com;

        location / {
            proxy_pass http://127.0.0.1:8000/;
        }
    }
}

驗證

啓動 go-gin-example

回到 go-gin-example 的項目下,執行 make,再運行 ./go-gin-exmaple

$ make
github.com/EDDYCJY/go-gin-example
$ ls
LICENSE        README.md      conf           go-gin-example middleware     pkg            runtime        vendor
Makefile       README_ZH.md   docs           main.go        models         routers        service
$ ./go-gin-example 
...
[GIN-debug] DELETE /api/v1/articles/:id      --> github.com/EDDYCJY/go-gin-example/routers/api/v1.DeleteArticle (4 handlers)
[GIN-debug] POST   /api/v1/articles/poster/generate --> github.com/EDDYCJY/go-gin-example/routers/api/v1.GenerateArticlePoster (4 handlers)
Actual pid is 14672
重啓 nginx
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$ nginx -s reload
訪問接口

image

如此,就實現了一個簡單的反向代理了,是否是很簡單呢

負載均衡

負載均衡,英文名稱爲Load Balance(常稱 LB),其意思就是分攤到多個操做單元上進行執行(來自百科)

你能從運維口中常常聽見,XXX 負載怎麼忽然那麼高。 那麼它究竟是什麼呢?

其背後通常有多臺 server,系統會根據配置的策略(例如 Nginx 有提供四種選擇)來進行動態調整,儘量的達到各節點均衡,從而提升系統總體的吞吐量和快速響應

如何演示

前提條件爲多個後端服務,那麼勢必須要多個 go-gin-example,爲了演示咱們能夠啓動多個端口,達到模擬的效果

爲了便於演示,分別在啓動前將 conf/app.ini 的應用端口修改成 8001 和 8002(也能夠作成傳入參數的模式),達到啓動 2 個監聽 8001 和 8002 的後端服務

配置 nginx.conf

回到 nginx.conf 的老地方,增長負載均衡所需的配置。新增 upstream 節點,設置其對應的 2 個後端服務,最後修改了 proxy_pass 指向(格式爲 http:// + upstream 的節點名稱)

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream api.blog.com {
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
    }

    server {
        listen       8081;
        server_name  api.blog.com;

        location / {
            proxy_pass http://api.blog.com/;
        }
    }
}
重啓 nginx
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$ nginx -s reload

驗證

http://api.blog.com:8081/auth?username={USER_NAME}}&password={PASSWORD}

目前 Nginx 沒有進行特殊配置,那麼它是輪詢策略,而 go-gin-example 默認開着 debug 模式,看看請求 log 就明白了

image

image

總結

在本章節,但願您可以簡單習得平常使用的 Web Server 背後都是一些什麼邏輯,Nginx 是什麼?反向代理?負載均衡?

怎麼簡單部署,知道了吧。

參考

本系列示例代碼

本系列目錄