使用 Golang CLI/API 工具的 Nginx
从现在开始,努力学习吧!本文《使用 Golang CLI/API 工具的 Nginx》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!
我在网上翻阅了无数文章,试图让这项工作成功,但我做不到。我在 go 中编写了一个 cli 工具和一个 api 服务器,该服务器接受来自 cli 的 https put 方法调用并返回值。请求和响应中的所有数据都是 json 格式。当客户端直接与 api 服务器通信时,一切都正常,我什至让它通过 squid 以直通模式工作。我正在重新设计 api 服务器和客户端以使用安全的 websockets,但我发现我当前配置 squid 的方式不起作用。所以我想我应该尝试 nginx,因为我在 ibm 网站上找到了一篇关于使用 nginx 代理 ws 套接字连接的文章。唉,我现在无法让简单的 https 工作。我已经创建了一个证书和密钥对,它们由父证书签名。在我的 go 客户端代码中,我使用计算机中的 ca 证书加载新的证书池,并将我的自签名 ca 证书附加到 ca 池。目前,这正在与 squid 传递一起使用。但当我尝试通过 nginx 运行我的客户端时,却没有。我修改了 go 代码以输出发送到 nginx 的请求信息,当我查看 nginx 日志时,我看到客户端连接和如下所示的错误。我从未见过从 nginx 到 api 服务器的连接。另外,我没有在 nginx 服务器上进行 ssl 终止,因此我必须重写大量代码以使 api 服务器成为非 ssl 来适应这一点。
这是我的客户端发送到 nginx 的请求信息:
mycommand single_value
error: cannot communicate with api server: put "https://my_api_server.local:443/uri": bad request
req headers:
map[content-type:[application/json]]
req method:
put
req body:
{{"key1":"val1","key2":val2,"key3":val3,"key4":val4}}
我在 nginx 访问日志中看到的条目是:
28/dec/2021:15:19:37 -0800 172.16.150.1 "connect my_api_server.local:443 http/1.1" - https://my_api_server.local- -" tlsv1.3 tls_aes_256_gcm_sha384 400 173 "-" "-" "-"
调试模式下错误日志中的请求条目显示:
2021/12/28 15:19:37 [debug] 3006#0: epoll: fd:8 ev:0001 d:00007fd46336e010 2021/12/28 15:19:37 [debug] 3006#0: accept on 172.16.150.132:443, ready: 0 2021/12/28 15:19:37 [debug] 3006#0: posix_memalign: 0000557becd3f1b0:512 @16 2021/12/28 15:19:37 [debug] 3006#0: *2 accept: 172.16.150.1:54656 fd:9 2021/12/28 15:19:37 [debug] 3006#0: *2 event timer add: 9: 60000:22007365 2021/12/28 15:19:37 [debug] 3006#0: *2 reusable connection: 1 2021/12/28 15:19:37 [debug] 3006#0: *2 epoll add event: fd:9 op:1 ev:80002001 2021/12/28 15:19:37 [debug] 3006#0: timer delta: 536667 2021/12/28 15:19:37 [debug] 3006#0: worker cycle 2021/12/28 15:19:37 [debug] 3006#0: epoll timer: 60000 2021/12/28 15:19:37 [debug] 3006#0: epoll: fd:9 ev:0001 d:00007fd46336e1e1 2021/12/28 15:19:37 [debug] 3006#0: *2 http check ssl handshake 2021/12/28 15:19:37 [debug] 3006#0: *2 http recv(): 1 2021/12/28 15:19:37 [debug] 3006#0: *2 https ssl handshake: 0x16 2021/12/28 15:19:37 [debug] 3006#0: *2 tcp_nodelay 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl server name: "https://my_api_server.local:443" 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_do_handshake: -1 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_get_error: 2 2021/12/28 15:19:37 [debug] 3006#0: *2 reusable connection: 0 2021/12/28 15:19:37 [debug] 3006#0: timer delta: 2 2021/12/28 15:19:37 [debug] 3006#0: worker cycle 2021/12/28 15:19:37 [debug] 3006#0: epoll timer: 59998 2021/12/28 15:19:37 [debug] 3006#0: epoll: fd:9 ev:0001 d:00007fd46336e1e1 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl handshake handler: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_do_handshake: 1 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl: tlsv1.3, cipher: "tls_aes_256_gcm_sha384 tlsv1.3 kx=any au=any enc=aesgcm(256) mac=aead" 2021/12/28 15:19:37 [debug] 3006#0: *2 reusable connection: 1 2021/12/28 15:19:37 [debug] 3006#0: *2 http wait request handler 2021/12/28 15:19:37 [debug] 3006#0: *2 malloc: 0000557becd3f3c0:1024 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_read: 127 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_read: -1 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_get_error: 2 2021/12/28 15:19:37 [debug] 3006#0: *2 reusable connection: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 posix_memalign: 0000557becce8910:4096 @16 2021/12/28 15:19:37 [debug] 3006#0: *2 http process request line 2021/12/28 15:19:37 [info] 3006#0: *2 client sent invalid request while reading client request line, client: 172.16.150.1, server: my_api_server.local, request: "connect my_api_server.local:443 http/1.1" 2021/12/28 15:19:37 [debug] 3006#0: *2 http finalize request: 400, "?" a:1, c:1 2021/12/28 15:19:37 [debug] 3006#0: *2 event timer del: 9: 22007365 2021/12/28 15:19:37 [debug] 3006#0: *2 http special response: 400, "?" 2021/12/28 15:19:37 [debug] 3006#0: *2 http set discard body 2021/12/28 15:19:37 [debug] 3006#0: *2 xslt filter header 2021/12/28 15:19:37 [debug] 3006#0: *2 http/1.1 400 bad request server: nginx/1.14.1 date: tue, 28 dec 2021 23:19:37 gmt content-type: text/html content-length: 173 connection: close 2021/12/28 15:19:37 [debug] 3006#0: *2 write new buf t:1 f:0 0000557becce9720, pos 0000557becce9720, size: 152 file: 0, size: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 http write filter: l:0 f:0 s:152 2021/12/28 15:19:37 [debug] 3006#0: *2 http output filter "?" 2021/12/28 15:19:37 [debug] 3006#0: *2 http copy filter: "?" 2021/12/28 15:19:37 [debug] 3006#0: *2 image filter 2021/12/28 15:19:37 [debug] 3006#0: *2 xslt filter body 2021/12/28 15:19:37 [debug] 3006#0: *2 http postpone filter "?" 0000557becce98f0 2021/12/28 15:19:37 [debug] 3006#0: *2 write old buf t:1 f:0 0000557becce9720, pos 0000557becce9720, size: 152 file: 0, size: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 posix_memalign: 0000557beccd4c70:4096 @16 2021/12/28 15:19:37 [debug] 3006#0: *2 write new buf t:0 f:0 0000000000000000, pos 0000557bebaab920, size: 120 file: 0, size: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 write new buf t:0 f:0 0000000000000000, pos 0000557bebaabf00, size: 53 file: 0, size: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 http write filter: l:1 f:0 s:325 2021/12/28 15:19:37 [debug] 3006#0: *2 http write filter limit 0 2021/12/28 15:19:37 [debug] 3006#0: *2 posix_memalign: 0000557becd45380:512 @16 2021/12/28 15:19:37 [debug] 3006#0: *2 malloc: 0000557bece01170:16384 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl buf copy: 152 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl buf copy: 120 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl buf copy: 53 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl to write: 325 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_write: 325 2021/12/28 15:19:37 [debug] 3006#0: *2 http write filter 0000000000000000 2021/12/28 15:19:37 [debug] 3006#0: *2 http copy filter: 0 "?" 2021/12/28 15:19:37 [debug] 3006#0: *2 http finalize request: 0, "?" a:1, c:1 2021/12/28 15:19:37 [debug] 3006#0: *2 event timer add: 9: 5000:21952375 2021/12/28 15:19:37 [debug] 3006#0: timer delta: 8 2021/12/28 15:19:37 [debug] 3006#0: worker cycle 2021/12/28 15:19:37 [debug] 3006#0: epoll timer: 5000 2021/12/28 15:19:37 [debug] 3006#0: epoll: fd:9 ev:2011 d:00007fd46336e1e1 2021/12/28 15:19:37 [debug] 3006#0: epoll_wait() error on fd:9 ev:2011 2021/12/28 15:19:37 [debug] 3006#0: *2 http lingering close handler 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_read: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_get_error: 6 2021/12/28 15:19:37 [debug] 3006#0: *2 peer shutdown ssl cleanly 2021/12/28 15:19:37 [debug] 3006#0: *2 lingering read: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 http request count:1 blk:0 2021/12/28 15:19:37 [debug] 3006#0: *2 http close request 2021/12/28 15:19:37 [debug] 3006#0: *2 http log handler 2021/12/28 15:19:37 [debug] 3006#0: *2 free: 0000557becce8910, unused: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 free: 0000557beccd4c70, unused: 3815 2021/12/28 15:19:37 [debug] 3006#0: *2 close http connection: 9 2021/12/28 15:19:37 [debug] 3006#0: *2 ssl_shutdown: 1 2021/12/28 15:19:37 [debug] 3006#0: *2 event timer del: 9: 21952375 2021/12/28 15:19:37 [debug] 3006#0: *2 reusable connection: 0 2021/12/28 15:19:37 [debug] 3006#0: *2 free: 0000557bece01170 2021/12/28 15:19:37 [debug] 3006#0: *2 free: 0000557becd3f3c0 2021/12/28 15:19:37 [debug] 3006#0: *2 free: 0000557becd3f1b0, unused: 48 2021/12/28 15:19:37 [debug] 3006#0: *2 free: 0000557becd45380, unused: 400 2021/12/28 15:19:37 [debug] 3006#0: timer delta: 1 2021/12/28 15:19:37 [debug] 3006#0: worker cycle 2021/12/28 15:19:37 [debug] 3006#0: epoll timer: -1
这是我的 nginx.conf 文件:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log debug;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$time_local $remote_addr "$request" '
'$request_method $scheme://$host$request_uri $server_protocol" '
'$ssl_protocol $ssl_cipher $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 172.16.150.132:443 ssl http2 default_server;
server_name my_api_server.local;
ssl_certificate /etc/nginx/ssl_certs/my_api_server.local.crt;
ssl_certificate_key /etc/nginx/ssl_certs/my_api_server.local.key;
# ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSV1.2 TLSV1.3;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl on;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
resolver 172.16.150.132;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host: $host;
proxy_pass https://my_api_server.local:9751;
# Disable caching
expires -1;
}
}
}
脱离了 squid 中 ssl_bump 的概念,我尝试在 nginx 配置中使用与 api 服务器相同的 ssl 证书和密钥。这是正确的吗?请告诉我我缺少什么。
正确答案
我发现了我的问题并让一切正常运行。我使用 postman 来模拟我的客户请求。当我运行来自 postman 的请求时,我在 nginx 日志中注意到,nginx 显示了正确的方法和 uri,而我的客户端却没有。
之前:
172.16.150.1 - - [28/dec/2021:12:28:38 -0800] "connect my_api_server.local:9751 http/1.1" tlsv1.3 tls_aes_256_gcm_sha384 400 173 "-" "-" "-"
之后:
29/Dec/2021:13:17:37 -0800 172.16.150.132 "PUT /uri HTTP/1.1" PUT https://my_proxy_server.local/uri HTTP/1.1" TLSv1.3 TLS_AES_256_GCM_SHA384 200 9822 "-" "Go-http-client/1.1" "-"
我禁用了客户端的所有代理配置,只是告诉它向 nginx 发出请求,现在一切正常了。我在客户端中定义代理设置的方式是错误的,但由于我的 api 服务器前面总是有一个代理服务器,所以我可以忽略它,只将客户端指向 nginx 服务器。 p>
我还能够确认无论我在 nginx 服务器上终止 ssl 还是将其一直传递到我的 api 服务器,该设置都有效。
我需要通读 go http.proxyurl 功能的使用并更好地理解它。
好了,本文到此结束,带大家了解了《使用 Golang CLI/API 工具的 Nginx》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
找不到模块“.”中的文件,请确保该模块不在您的工作区中
- 上一篇
- 找不到模块“.”中的文件,请确保该模块不在您的工作区中
- 下一篇
- 减少 GoLang 中函数可能返回错误时的 if 嵌套的方法是什么?
-
- Golang · Go问答 | 3个月前 | go atomic原理 Go并发安全
- Go语言中atomic包如何保证并发安全?
- 109浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ljg-skills
- ljg-skills 是李继刚开源的 AI 技能与提示词集合,面向大模型使用者整理了一批可复用的 prompt、角色设定和任务技能模板,适合用于学习提示词设计、搭建个人 AI 工作流和沉淀团队常用智能体能力。
- 1438次使用
-
- MELO音乐
- MELO音乐是一站式AI视频与音乐制作助手,对标suno, udio的高品质体验。提供伴奏生成、原创写词、无损导出、哼唱识曲、混音变声等全套音频与短视频编辑工具。无论是流行Kpop、电音说唱、民谣古风、摇滚儿歌还是商用轻音乐,MELO为你免费谱曲,轻松做同款!
- 1381次使用
-
- UniScribe
- UniScribe 是一款 AI 音视频转文字与内容整理工具,支持上传音频、视频文件或粘贴 YouTube 链接,自动生成转写文本、摘要、思维导图和关键问题,并支持多格式导出,适合会议记录、课程学习、访谈整理和内容创作复盘。
- 1339次使用
-
- 剧云
- 剧云是专业中文剧本创作平台,安全稳定运行十余年,集成AI编剧、剧本医生审核、人物小传、剧情关系图、大纲编写、多人协作、Word导入导出、版权管控功能,数据安全防护,轻松高效创作剧本。
- 1508次使用
-
- 万象有声
- 万象有声,一个专为有声创作者打造的新一代智能有声内容创作平台。平台提供专业的智能拆章、智能画本编辑、AI配音、AI生成音效、后期制作、智能对轨、智能审听等有声创作全流程工具,可以帮助创作者高效、低成本创作出引人入胜的有声作品。立即体验,让有声书制作更简单!
- 1500次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览

