tengine 常用配置,tengine配置


  1. 解决IE系列浏览器下下载apk变为zip问题

application/vnd.android.package-archive apk; 

application/iphone pxl ipa;

2.屏蔽nginx/apache/php版本信息

打开 nginx.conf,在http { }里加上

server_tokens off;

3.tengine安装trim filter模块

./configure --with--http_trim_filter_module=shared

make

make dso_install

nginx.conf中events上方加入

dso {

    load ngx_http_trim_filter_module.so;

}

trim on;

trim_jscss on;

看下效果首页减少2K

还有一点:trim_jscss开启不一定适合全部网站哦,开启可能会影响js

4.Nginx安装lua-nginx-module模块

wget http://luajit.org/download/LuaJIT-2.1.0-beta2.tar.gz

tar zxf LuaJIT-2.1.0-beta2.tar.gz

cd LuaJIT-2.1.0-beta2

make PREFIX=/usr/local/luajit

make install PREFIX=/usr/local/luajit

wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz

tar -xzvf v0.2.19.tar.gz

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.2.tar.gz

tar -xzvf v0.10.2.tar.gz

# tell nginx's build system where to find LuaJIT 2.1:

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1

nginx -V看下之前的安装都带了哪些参数

./configure (之前安装的参数) --with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" --add-module=/path/to/ngx_devel_kit --add-module=/path/to/lua-nginx-module

--add-module后参数路径根据解压路径为准

make -j2

make install

验证lua-nginx-module安装是否成功

在nginx下配置

location /hello_lua { 

      default_type 'text/plain'; 

      content_by_lua 'ngx.say("hello, lua")'; 

}

5.通过ngx-lua来统计nginx上的虚拟主机性能数据

用ngx-lua来做性能统计、甚至一些运营数据统计,都是非常好的选择。

1.几乎对原有项目代码分离

2.性能佳

3.支持分不同虚拟主机统计, 同一个虚拟主机下可以分不同的location统计

4.可以统计与query-times、request-time、status-code、speed相关的数据

因为基于ngx-lua所以需要先安装环境,请移步Nginx安装lua-nginx-module模块

github这个项目https://github.com/initial5/ngx-lua-stats前辈们已给代码

使用方法就是在nginx.conf的http中配置加入

lua_shared_dict log_dict 5M;

lua_shared_dict result_dict 5M;

然后在入口nginx上配置

server {

listen 80;

server_name funboxpower.com;

access_log /home/logs/funboxpower.com.log main;

location / {

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass http://funboxpower.com_backend;

log_by_lua_file ./site-enable/record.lua;

}

}

记得配upstream,否则之后用到的统计有关于upstream的值是出不来的,这个我还要进一步确认下

然后可以再配一个server

server {

listen 8080 default;

server_name _;

location / {

return 404;

}

location /status {

content_by_lua_file ./site-enable/output.lua;

}

location /empty_dict {

content_by_lua_file ./site-enable/empty_dict.lua;

}

}

配置好后,就可以通过如下命令获取

curl ip_addr:8080/status

运行一段时间之后, 字典会变大. 可以通过如下接口清理

curl ip_addr:8080/empty_dict

--------------------------

key: funboxpower.com__upstream_time_10.0.3.32:8250_counter

0.375

key: funboxpower.com__upstream_time_10.0.3.32:8250_nb_counter

124

key: funboxpower.com__upstream_time_10.0.4.93:8250_counter

0.131

key: funboxpower.com__upstream_time_10.0.4.93:8250_nb_counter

123

key: funboxpower.com__upstream_time_10.20.12.49:8250_counter

0.081

key: funboxpower.com__upstream_time_10.20.12.49:8250_nb_counter

127

key: funboxpower.com__query_counter

500

key: funboxpower.com__request_time_counter

0.68

key: funboxpower.com__upstream_time_counter

0.683

key: funboxpower.com__upstream_time_10.20.12.59:8250_counter

0.096

key: funboxpower.com__upstream_time_10.20.12.59:8250_nb_counter

126

key: funboxpower.com__bytes_sent_counter

81500

其中__用来分割虚拟主机(包含prefix)与后面的数据项,便于数据处理.

counter表示此值一直在累加

nb_counter表示次数

可以得到的数据包括: query次数 request_time bytes_sent upstream_time

其中

upstream_time_10.20.12.49:8250_counter 表示到某个特定后端的upstream_time耗时累加

upstream_time_10.20.12.49:8250_nb_counter 表示到到某个特定后端的upstream_time次数累加


数据处理,delta不明白啥意思,先记录

delta(bytes_sent_counter)/delta(query_counter) 得到就是这段时间的http传输速度

delta(upstream_time_10.20.12.49:8250_counter)/delta(upstream_time_10.20.12.49:8250_nb_counter) 得到的就是这个后端upstream_time的平均值

6.Nginx下php-fpm防止跨站跨目录安全设置


参考 http://www.funboxpower.com/


7.记一次压测引起的nginx负载均衡性能调优

nginx的最大连接数是worker num * worker_connections, 默认worker_connections是1024, 直接干到10w就可以了

虽然我们在sysctl内核里做了一些网络tcp回收的优化,但那也赶不上压力测试带来的频繁创建tcp的消耗。   果然在upstream加了keepalive

不需要重新创建socket或者发起connect()。这样既省下建立连接时在握手的时间消耗,又可以避免TCP连接的slow start

Golang的http模块貌似对http spdy支持不怎么好, 要不然可以直接用淘宝的tengine upstream spdy的方式连接后端Server。 他的速度要比keepalive要好的多,毕竟省去了等待上次返回的结果的过程


8.

参考http://www.ttlsa.com/nginx/use-nginx-proxy/

配置


1.要想能记录真实IP,需要修改后端机器的日志格式,这里假设后端也是一台nginx:

在后端配置文件里面加入这一段即可:

log_format access '$HTTP_X_REAL_IP - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $HTTP_X_Forwarded_For';

access_log logs/access.log access;



故障

之前没配置下面这段,访问时候偶尔会出现504 gateway timeout,由于偶尔出现,所以不太好排查

proxy_connect_timeout 300s;

proxy_read_timeout 300s;

proxy_send_timeout 300s;

proxy_buffer_size 64k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_ignore_client_abort on;



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1854054,如需转载请自行联系原作者

相关内容