使用 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问答 | 32分钟前 |
- Go Resolver PreferGo 与系统解析器的差异边界
- 452浏览 收藏
-
- Golang · Go问答 | 54分钟前 |
- Go DNS 轮询返回多地址后的连接选择策略
- 295浏览 收藏
-
- Golang · Go问答 | 1小时前 | 网络编程 · DNS · Go问答 · DNS Go net.Resolver 解析超时 LookupIPAddr
- Go net.Resolver 自定义 DNS 解析超时的实现
- 481浏览 收藏
-
- Golang · Go问答 | 2小时前 | Go问答 · 兼容性 · tls Go MinVersion CipherSuites
- Go TLS 最低版本与密码套件迁移清单
- 130浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- Go tls.Config 复用导致证书更新不生效的处理方式
- 144浏览 收藏
-
- Golang · Go问答 | 2小时前 | 网络编程 · Go问答 · tls Go ALPN NextProtos
- Go TLS 握手因 ALPN 不匹配失败的定位方案
- 397浏览 收藏
-
- Golang · Go问答 | 3小时前 | 连接池 · 性能排查 · Go问答 · net/http Go HTTP/2 MaxConcurrentStreams StrictMaxConcurrentRequests 请求排队
- Go HTTP/2 流并发限制导致请求排队的调参思路
- 188浏览 收藏
-
- Golang · Go问答 | 4小时前 | net/http · Go问答 · Go 单页应用 SPA http.FileServer embed.FS index.html
- Go http.FileServer 为单页应用提供回退文件
- 106浏览 收藏
-
- Golang · Go问答 | 4小时前 | HTTP · Cookie · net/http · Go问答 · cookie Go net/http CookiesNamed Request.Cookies
- Go Request.Cookies 处理同名 Cookie 的读取顺序
- 102浏览 收藏
-
- Golang · Go问答 | 5小时前 |
- Go Cookie SameSite 配置在跨站请求中的边界
- 111浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 256次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 301次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 278次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 257次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 64次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览
-
- Go sql.Tx提交成功前读取结果导致事务边界混乱的修复方法
- 2026-09-20 501浏览
-
- Go select 用 time.After 做超时有什么资源代价
- 2026-09-10 501浏览

