分类目录归档:Network

nginx反向代理配置keepalive

有个项目上的Keep-Alive配置一直不起作用,检查了很久代码找不到问题,然后灵光一现想到应该是反向代理配置的问题

即:client—–nginx proxy—–real server

client配置了使用keepalive,real server也支持,问题就在于nginx代理没配置对

配置方式如下:

1、配置upstream,keepalive需要在upstream里配置


upstream http_backend {
server 127.0.0.1:8080;

keepalive 16;
}

2、修改proxy设置,proxy_http_version必须设置为“1.1”,且需要清空Connection头


server {

location /http/ {
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection “”;

}
}

参考资料:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

IE的“c00ce56e”bug

很简单的一个jQuery AJAX请求文件,IE8下报:

Could not complete the operation due to error c00ce56e.

检查网上都说是编码问题,不过写的不是太明确,因为我们服务器的文件编码都已经是UTF-8
最后找到说明

The problem occurs when setting the charset of a response to a XMLHttpRequest to utf8 instead of UTF-8 or ISO-8859-1 instead of ISO8859_1. IE7 is very strict on naming of the charsets.

“ISO8859_1” is the canonical representation of the Latin-1 character encoding string in the Java language and class libraries. The standard that is defined by the Internet Assigned Numbers Authority, however, is “ISO-8859-1”, which is not an accepted alias.

For more information refer: http://support.microsoft.com/default.aspx?scid=kb;en-us;304625

于是打开nginx的配置文件,把所有“utf8”替换成“UTF-8”,问题解决

Apache HttpClient与CLOSE_WAIT

前几天开始服务器时不时无法accept新连接,由于线上环境的原因都没时间调试直接重启了事
昨天频繁发生了几次,仔细看了下系统日志发现:

java.net.SocketException: Too many open files

用netstat看发现有大量的CLOSE_WAIT,搜索发现与libcurl的处理类似,apache-commons的HttpClient也默认保持了连接待复用处理。

解决办法:

  • 每次使用完都完全关闭连接

    httpmethod.setRequestHeader(“Connection”, “close”);
  • 使用统一的连接管理器复用连接,例如SimpleHttpConnectionManager,从连接管理器里获取连接,然后定时的用连接管理器来释放空闲连接
    在比较频繁的抓取项目中推荐复用连接

参考文章:http://swordinhand.javaeye.com/blog/149408

libcurl与CLOSE_WAIT

调用libcurl下载,然后使用netstat查看发现有大量的TCP连接保持在CLOSE_WAIT状态
查看libcurl的文档说明,有这样一个选项:

CURLOPT_FORBID_REUSE

Pass a long. Set to 1 to make the next transfer explicitly close the connection when done. Normally, libcurl keeps all connections alive when done with one transfer in case a succeeding one follows that can re-use them. This option should be used with caution and only if you understand what it does. Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior).

也就是说,默认情况下libcurl完成一个任务以后,出于重用连接的考虑不会马上关闭
如果没有新的TCP请求来重用这个连接,那么只能等到CLOSE_WAIT超时,这个时间默认在7200秒甚至更高,太多的CLOSE_WAIT连接会导致性能问题

解决方法:

curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);

最好再修改一下TCP参数调低CLOSE_WAIT和TIME_WAIT的超时时间

域名转出至GoDaddy

从万网转出果然是名不虚传的困难

其中的艰辛不再赘述,劝君莫用万网

还有另一个域名还在继续跟万网作斗争

PS: 发表完了看到相关日志,顿时泪流满面!