栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

用Nginx部署Go应用

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Nginx部署Go应用

Nginx 是一个 Web Server,可以用作反向代理、负载均衡、邮件代理、TCP / UDP、HTTP 服务器等等,它拥有很多吸引人的特性,例如:

  • 以较低的内存占用率处理 10,000 多个并发连接(每10k非活动HTTP保持活动连接约2.5 MB )

  • 静态服务器(处理静态文件)

  • 正向、反向代理

  • 负载均衡

  • 通过OpenSSL 对 TLS / SSL 与 SNI 和 OCSP 支持

  • FastCGI、SCGI、uWSGI 的支持

  • WebSockets、HTTP/1.1 的支持

  • Nginx + Lua

简单讲解

常用命令

  • nginx:启动 Nginx

  • nginx -s stop:立刻停止 Nginx 服务

  • nginx -s reload:重新加载配置文件

  • nginx -s quit:平滑停止 Nginx 服务

  • nginx -t:测试配置文件是否正确

  • nginx -v:显示 Nginx 版本信息

  • nginx -V:显示 Nginx 版本信息、编译器和配置参数的信息

涉及配置

1、 proxy_pass:配置反向代理的路径。需要注意的是如果 proxy_pass 的 url 最后为
/,则表示绝对路径。否则(不含变量下)表示相对路径,所有的路径都会被代理过去

2、 upstream:配置负载均衡,upstream 默认是以轮询的方式进行负载,另外还支持四种模式,分别是:

(1)weight:权重,指定轮询的概率,weight 与访问概率成正比

(2)ip_hash:按照访问 IP 的 hash 结果值分配

(3)fair:按后端服务器响应时间进行分配,响应时间越短优先级别越高

(4)url_hash:按照访问 URL 的 hash 结果值分配

反向代理

反向代理是指以代理服务器来接受网络上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

配置 hosts

由于需要用本机作为演示,因此先把映射配上去,打开 /etc/hosts,增加内容:

127.0.0.1       api.blog.com

配置 nginx.conf

打开 nginx 的配置文件 nginx.conf(我的是 /usr/local/etc/nginx/nginx.conf),我们做了如下事情:

增加 server 片段的内容,设置 server_name 为 api.blog.com 并且监听 8081 端口,将所有路径转发到 http://127.0.0.1:8000/ 下

  1. worker_processes  1;

  2. events {

  3.    worker_connections  1024;

  4. }

  5. http {

  6.    include       mime.types;

  7.    default_type  application/octet-stream;

  8.    sendfile        on;

  9.    keepalive_timeout  65;

  10.    server {

  11.        listen       8081;

  12.        server_name  api.blog.com;

  13.        location / {

  14.            proxy_pass http://127.0.0.1:8000/;

  15.        }

  16.    }

  17. }

验证

启动 go-gin-example

回到 go-gin-example 的项目下,执行 make,再运行 ./go-gin-exmaple

  1. $ make

  2. github.com/EDDYCJY/go-gin-example

  3. $ ls

  4. LICENSE        README.md      conf           go-gin-example middleware     pkg            runtime        vendor

  5. Makefile       README_ZH.md   docs           main.go        models         routers        service

  6. $ ./go-gin-example

  7. ...

  8. [GIN-debug] DELETE /api/v1/articles/:id      --> github.com/EDDYCJY/go-gin-example/routers/api/v1.DeleteArticle (4 handlers)

  9. [GIN-debug] POST   /api/v1/articles/poster/generate --> github.com/EDDYCJY/go-gin-example/routers/api/v1.GenerateArticlePoster (4 handlers)

  10. 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

如此,就实现了一个简单的反向代理了,是不是很简单呢

负载均衡

负载均衡,英文名称为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 的节点名称)

  1. worker_processes  1;

  2. events {

  3.    worker_connections  1024;

  4. }

  5. http {

  6.    include       mime.types;

  7.    default_type  application/octet-stream;

  8.    sendfile        on;

  9.    keepalive_timeout  65;

  10.    upstream api.blog.com {

  11.        server 127.0.0.1:8001;

  12.        server 127.0.0.1:8002;

  13.    }

  14.    server {

  15.        listen       8081;

  16.        server_name  api.blog.com;

  17.        location / {

  18.            proxy_pass http://api.blog.com/;

  19.        }

  20.    }

  21. }

重启 nginx

$ nginx -tnginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is oknginx: 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 就明白

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号