ngnix 时钟 ngx_gettimeofday 更新时间

ngxin 项目,有 windows 版本,之前为了优化性能,用 timeGetTime(); 获取时间。

这样导致了时钟不稳定,时钟正常跑一段时间后就不跑了,或者超时。

用 timeGetTime(); 获取返回的数值不定时出现问题。按 msdn 说,这个函数不能单独直接使用于代码运算。

 http://msdn.microsoft.com/en-us/library/ms713418.aspx

timeGetTime

    This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations. 

void
ngx_gettimeofday(struct timeval *tp)
{
#ifdef NGX_WIN32
    DWORD dt = timeGetTime(); // 错误代码项
    tp->tv_sec = (long) (dt / 1000);
    tp->tv_usec = (long) (dt*1000);
#else
    ULONGLONG   usec;
    FILETIME    ft;
    SYSTEMTIME  st;

    GetSystemTime(&st);
    SystemTimeToFileTime(&st, &ft);

    usec = ft.dwHighDateTime;
    usec <<= 32;
    usec |= ft.dwLowDateTime;
    usec /= 10;
    usec -= 11644473600000000LL;

    tp->tv_sec = (long) (usec / 1000000);
    tp->tv_usec = (long) (usec % 1000000);
#endif // NGX_WIN32
}


### 添加 `ngx_http_v2_module` 以启用 HTTP/2 支持 要在 Nginx 中启用 HTTP/2 协议支持,必须通过添加 `ngx_http_v2_module` 模块来实现。该模块并非默认编译进 Nginx,而是需要在编译时通过 `--add-module` 参数手动引入。通过合理配置该模块,可以充分利用 HTTP/2 的优势,显著提升网页加载速度和用户体验,这对于构建高可用性和高性能的应用系统非常有用 [^1]。 #### 编译 Nginx 时添加 `ngx_http_v2_module` 首先需要获取 Nginx 的源代码,并确保已经下载了 `ngx_http_v2_module` 模块的源代码。在配置编译选项时,使用 `--add-module` 参数指定模块的路径。例如: ```bash ./configure --add-module=../path/to/ngx_http_v2_module ``` 配置完成后,执行编译和安装命令: ```bash make sudo make install ``` 在编译过程中,确保依赖库(如 OpenSSL、HTTP/2 库)已正确安装,否则可能导致编译失败。 #### 配置 Nginx 启用 HTTP/2 在成功编译并安装 `ngx_http_v2_module` 后,需要在 Nginx 配置文件中启用 HTTP/2。在 `server` 块中,使用 `listen` 指令并添加 `http2` 参数。例如: ```nginx server { listen 443 ssl http2; server_name example.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/privkey.pem; # 其他配置项 } ``` 确保 SSL 证书路径正确,并且服务器已正确配置 HTTPS 支持。HTTP/2 要求使用加密连接(TLS),因此必须启用 SSL 配置。 #### 检查模块是否成功添加 在完成编译和安装后,可以通过以下命令检查 Nginx 是否包含 `ngx_http_v2_module`: ```bash nginx -V 2>&1 | grep -- 'http_v2_module' ``` 如果输出中包含 `--add-module=../path/to/ngx_http_v2_module`,则表示模块已成功添加。 #### 使用预编译包安装 `ngx_http_v2_module` 如果不想手动编译 Nginx,可以选择使用包含 `ngx_http_v2_module` 的预编译包。某些 Linux 发行版(如 Ubuntu 或 CentOS)的仓库中可能已经包含了支持 HTTP/2 的 Nginx 版本。可以通过以下命令安装: ```bash sudo apt-get install nginx-extras # Ubuntu sudo yum install nginx-mod-http-v2 # CentOS ``` 安装完成后,同样需要在 Nginx 配置文件中启用 HTTP/2 支持。 #### 验证 HTTP/2 是否正常工作 在完成配置后,可以使用 `curl` 命令验证 HTTP/2 是否正常工作: ```bash curl -I --http2 --insecure https://example.com ``` 如果返回的响应头中包含 `HTTP/2 200`,则表示 HTTP/2 已成功启用。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值