我们有一个带有嵌入式 http 服务器的 C/C++ 应用程序。我们的 http 服务器非常原生,不支持 SSL 或保持活动连接。它监听端口 5555 并提供静态页面。在提供页面之前,它会对响应进行 gzip 压缩。它还支持使用 Etags 进行缓存验证。取决于If-None-Match
它发送的值304 Not Modifed
。如果我们将浏览器重定向到,它会正常工作some-ip:5555
。示例请求和响应标头如下
Response Headers
Cache-Control max-age=3600, must-revalidate
Content-Encoding gzip
Expires Wed, 15 May 2013 07:18:08 GMT
Request Headers
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Cache-Control max-age=0
Connection keep-alive
Host 127.0.0.1:5555
If-Modified-Since Tue, 08 May 2012 08:48:53 GMT
If-None-Match "1336466933_85925"
Referer http://127.0.0.1:5555/mlogin.html
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120425 Firefox/10.0.4
Response Headers From Cache
Cache-Control max-age=3600, must-revalidate
Content-Encoding gzip
Content-Length 29832
Content-Type application/x-javascript
Etag "1336466933_85925"
Expires Wed, 15 May 2013 07:18:08 GMT
Last-Modified Tue, 08 May 2012 08:48:53 GMT
现在,当使用 nginx 反向代理尝试相同的设置时,它会502 Bad Gateway
为所有尝试重新验证缓存的页面提供信息。它对所有页面都运行良好Cache-Control no-cache
。我们的 nginx 配置如下
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_requests 0;
keepalive_timeout 0;
server {
server_name _;
listen 80;
location / {
proxy_pass http://127.0.0.1:5555;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
响应和请求标头如下
Response Headers
Connection close
Content-Length 172
Content-Type text/html
Date Tue, 15 May 2012 08:46:31 GMT
Server nginx/1.2.0
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Cache-Control max-age=0
Connection keep-alive
Host 127.0.0.1
If-Modified-Since Tue, 08 May 2012 08:48:53 GMT
If-None-Match "1336466933_85925"
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120425 Firefox/10.0.4
在 nginx 错误日志中我们收到此错误
2012/05/15 14:16:31 [error] 18832#0: *177 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /lib/jquery-1.5.2.min.js?v=1330852144 HTTP/1.1", upstream: "http://127.0.0.1:5555/lib/jquery-1.5.2.min.js?v=1330852144", host: "127.0.0.1"
有人能指出哪里出了问题吗?提前致谢。
答案1
可能是你的 http 服务器以某种方式破坏了 HTTP 协议,例如在最后一个标头之后没有发送额外的 CRLF。