Nginx使用Golang CLI/API工具



我在网上浏览了无数篇文章,试图实现这一点,但我做不到。我在GO中编写了一个CLI工具和一个API服务器,该服务器接受来自CLI的HTTPs PUT方法调用并返回值。请求和响应中的所有数据都是JSON格式。当客户端直接与API服务器通信时,一切都会正常工作,我甚至让它以直通模式通过Squid工作。我正在重新设计API服务器和客户端,以使用安全的websocket,我发现我目前配置Squid的方式不起作用。所以我想我应该试试Nginx,因为我在IBM的网站上发现了一篇关于使用它代理ws套接字连接的文章。唉,我现在无法让简单的https工作。我已经创建了一个证书和密钥对,它们由父证书签名。在我的GO客户端代码中,我加载了一个新的证书池,其中包含来自机器的CA证书,并将我的自签名CA证书附加到CA池中。这是目前工作的鱿鱼通过。不过,当我试图通过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_Bumpp的概念出发,我尝试在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服务器。

我还能够确认,无论我是在Nginx服务器上终止SSL,还是将其一直传递到我的API服务器,设置都有效。

我需要通读GO http的使用。ProxyURL功能并更好地理解它。

最新更新