原文地址:Golang Gin实践 连载十七 用 Nginx 部署 Go 应用html
若是已经看过前面 “十六部连载,两部番外”,相信您的能力已经有所提高nginx
那么,如今今天来讲说简单部署后端服务的事儿 🤓git
在本章节,咱们将简单介绍 Nginx 以及使用 Nginx 来完成对 go-gin-example 的部署,会实现反向代理和简单负载均衡的功能github
Nginx 是一个 Web Server,能够用做反向代理、负载均衡、邮件代理、TCP / UDP、HTTP 服务器等等,它拥有不少吸引人的特性,例如:golang
请右拐谷歌或百度,安装好 Nginx 以备接下来的使用segmentfault
一、 proxy_pass:配置反向代理的路径。须要注意的是若是 proxy_pass 的 url 最后为
/,则表示绝对路径。不然(不含变量下)表示相对路径,全部的路径都会被代理过去后端
二、 upstream:配置负载均衡,upstream 默认是以轮询的方式进行负载,另外还支持四种模式,分别是:api
(1)weight:权重,指定轮询的几率,weight 与访问几率成正比缓存
(2)ip_hash:按照访问 IP 的 hash 结果值分配服务器
(3)fair:按后端服务器响应时间进行分配,响应时间越短优先级别越高
(4)url_hash:按照访问 URL 的 hash 结果值分配
在这里须要对 nginx.conf 进行配置,若是你不知道对应的配置文件是哪一个,可执行 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/
目录下,而且测试经过
反向代理是指以代理服务器来接受网络上的链接请求,而后将请求转发给内部网络上的服务器,并将从服务器上获得的结果返回给请求链接的客户端,此时代理服务器对外就表现为一个反向代理服务器。(来自百科)
因为须要用本机做为演示,所以先把映射配上去,打开 /etc/hosts
,增长内容:
127.0.0.1 api.blog.com
打开 nginx 的配置文件 nginx.conf(个人是 /usr/local/etc/nginx/nginx.conf),咱们作了以下事情:
增长 server 片断的内容,设置 server_name 为 api.blog.com 而且监听 8081 端口,将全部路径转发到 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 的项目下,执行 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 -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
如此,就实现了一个简单的反向代理了,是否是很简单呢
负载均衡,英文名称为Load Balance(常称 LB),其意思就是分摊到多个操做单元上进行执行(来自百科)
你能从运维口中常常听见,XXX 负载怎么忽然那么高。 那么它究竟是什么呢?
其背后通常有多台 server,系统会根据配置的策略(例如 Nginx 有提供四种选择)来进行动态调整,尽量的达到各节点均衡,从而提升系统总体的吞吐量和快速响应
前提条件为多个后端服务,那么势必须要多个 go-gin-example,为了演示咱们能够启动多个端口,达到模拟的效果
为了便于演示,分别在启动前将 conf/app.ini 的应用端口修改成 8001 和 8002(也能够作成传入参数的模式),达到启动 2 个监听 8001 和 8002 的后端服务
回到 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 -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 就明白了
在本章节,但愿您可以简单习得平常使用的 Web Server 背后都是一些什么逻辑,Nginx 是什么?反向代理?负载均衡?
怎么简单部署,知道了吧。