5. Nginx 实现Apache一样目录列表
location / {
autoindex on;
}
6. 上文件大小限制
主配置文件里加入如下,具体大小根据你自己的业务做调整。
client_max_body_size 10m;
十、 Nginx 日志处理
1.Nginx 日志切割
#contab -e
59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1
[root@count ~]# cat /usr/local/sbin/logcron.sh
#!/bin/bash
log_dir=”/data/logs”
time=`date +%Y%m%d`
/bin/mv ${log_dir}/access_linuxso.com.log ${log_dir}/access_count.linuxso.com.$time.log
kill -USR1 `cat /var/run/nginx.pid`
更多的日志分析与处理就关注(同时欢迎你参加讨论):http://bbs.linuxso.com/forum-8-1.html
2.利用AWSTATS分析NGINX日志
设置好Nginx日志格式,仍后利用awstats进行分析.
请参考: http://bbs.linuxso.com/thread-56-1-1.html
3. Nginx 如何不记录部分日志
日志太多,每天好几个G,少记录一些,下面的配置写到server{}段中就可以了
location ~ .*.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
{
access_log off;
}
十一、Nginx Cache服务配置
如果需要将文件缓存到本地,则需要增加如下几个子参数:
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path 缓存目录;
其中,
proxy_store on用来启用缓存到本地的功能,
proxy_temp_path用来指定缓存在哪个目录下,如:proxy_temp_path html;
在经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会向远端拉取文件,为了避免去
远端拉取文件,必须修改
proxy_pass:
if ( !-e $request_filename) {
proxy_pass http://mysvr;
}
即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的proxy_temp_path指定的目录下
不存在时,再向后端拉取。
十二、Nginx 负载均衡
1. Nginx 负载均衡基础知识
nginx的upstream目前支持4种方式的分配
1)、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2)、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)、url_hash(第三方)
2. Nginx 负载均衡实例1
upstream bbs.linuxso.com {#定义负载均衡设备的Ip及设备状态
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://bbs.linuxso.com/;
每个设备的状态设置为:
a) down 表示单前的server暂时不参与负载
b) weight 默认为1.weight越大,负载的权重就越大。
c) max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义
的错误
d) fail_timeout:max_fails次失败后,暂停的时间。
e) backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
3. Nginx 负载均衡实例 2
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,
也可以用作提高Squid缓存命中率.
简单的负载均等实例:
#vi nginx.conf //nginx主配置文件核心配置
……….
#loadblance my.linuxso.com
upstream my.linuxso.com {
ip_hash;
server 127.0.0.1:8080;
server 192.168.169.136:8080;
server 219.101.75.138:8080;
server 192.168.169.117;
server 192.168.169.118;
server 192.168.169.119;
}
…………..
include vhosts/linuxtone_lb.conf;
………
# vi proxy.conf
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;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access user:rw group:rw all:r;
#nginx cache
#client_body_temp_path /data/nginx_cache/client_body 1 2;
proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
#vi linuxtone_lb.conf
server
{
listen 80;
server_name my.linuxso.com;
index index.php;
root /data/www/wwwroot/mylinuxtone;
if (-f $request_filename) {
break;
}
if (-f $request_filename/index.php) {
rewrite (.*) $1/index.php break;
}
error_page 403 http://my.linuxso.com/member.php?m=user&a=login;
location / {
if ( !-e $request_filename) {
proxy_pass http://my.linuxso.com;
break;
}
include /usr/local/nginx/conf/proxy.conf;
}
}
十三、Nginx简单优化
1. 减小nginx编译后的文件大小 (Reduce file size of nginx)
默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类),编译以
后一个nginx有好几兆。去掉nginx的debug模式编译,编译以后只有几百K
在 auto/cc/gcc,最后几行有:
# debug
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。
2. 修改Nginx的header伪装服务器
1) 修改nginx.h
#vi nginx-0.7.30/src/core/nginx.h
#define NGINX_VERSION “1.8″
#define NGINX_VER “LTWS/” NGINX_VERSION
#define NGINX_VAR “NGINX”
#define NGX_OLDPID_EXT “.oldbin”
2) 修改nginx_http_header_filter_module
#vi nginx-0.7.30/src/http/ngx_http_header_filter_module.c
将如下
static char ngx_http_server_string[] = “Server: nginx” CRLF;
修改为
static char ngx_http_server_string[] = “Server: LTWS” CRLF;
a) 修改nginx_http_header_filter_module
#vi nginx-0.7.30/src/http/ngx_http_special_response.c
将如下:
static u_char ngx_http_error_full_tail[] =
“<hr><center>” NGINX_VER “</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;
static u_char ngx_http_error_tail[] =
“<hr><center>nginx</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;
修改为:
static u_char ngx_http_error_full_tail[] =
“<center> “NGINX_VER” </center>” CRLF
“<hr><center>http://www.linuxso.com</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;
static u_char ngx_http_error_tail[] =
“<hr><center>LTWS</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;
修改后重新编译一下环境
3.为特定的CPU指定CPU类型编译优化.
默认nginx使用的GCC编译参数是-O
需要更加优化可以使用以下两个参数
–with-cc-opt=’-O3′
–with-cpu-opt=opteron
使得编译针对特定CPU以及增加GCC的优化.
此方法仅对性能有所改善并不会有很大的性能提升,供朋友们参考.
CPUD类型确定: # cat /proc/cpuinfo | grep “model name”
编译优化参数参考:http://en.gentoo-wiki.com/wiki/Safe_Cflags
4.Tcmalloc优化Nginx 性能
# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
# tar zxvf libunwind-0.99-alpha.tar.gz
# cd libunwind-0.99-alpha/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install
# wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz
# tar zxvf google-perftools-0.98.tar.gz
# cd google-perftools-0.98/
# ./configure
# make && make install
# echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf
# ldconfig
# lsof -n | grep tcmalloc
编译nginx 加载google_perftools_module:
./configure –with-google_perftools_module
在主配置文件加入nginx.conf 添加:
google_perftools_profiles /path/to/profile;
5.内核参数优化
# vi /etc/sysctl.conf #在末尾增加以下内容:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
#使配置立即生效
/sbin/sysctl -p
十四、如何构建高性的LEMP
请参见: http://www.linuxso.com/lemp/lemp.pdf
1、提供完整的配置脚本下载:http://www.linuxso.com/lemp/scripts.tar.gz