2014-02-11 17:30:02
来 源
itjs.cn
Nginx
本文介绍CentOSLinux系统中Nginx服务器的编译安装及优化方法,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。

编译软件相关版本

系统版本              CentOs 5.5 Final

Libevent版本    libevent 1.4

Nginx版本           nginx-1.2.4

Pcre版本   pcre-8.30

Php版本    php5.3.10

Mysql版本           mysql5.5

目录

1 nignx的安装

1.1 安装libevent

1.2 安装pcre

1.3 修改debug参数,减小nginx编译文件大小

1.4 编译安装OPenssL

1.5 利用TCMAlloc优化Nginx的性能

1.6 修改编译文件,隐藏nginx信息

1.7 编译安装nginx

2 php的安装

2.1 安装libevent

2.2 编译安装php

2.3 设置FastCGI,支持nginx解析Php

2.4 修改nginx连接FastCGI解析phpinfo

3 nginx的优化

3.1 通过shell脚本实现nginx的日志轮训

3.2 配置错误页面的优雅显示

3.3 启用nginx的gzip压缩传输和开启客户端缓存功能

3.4 修改Nginx的运行账户

3.5 做压力测试,隐藏Nginx版本

3.6 网站文件权限安全设置

3.7 增加并发连接数目,使用epoll网络模型

3.8 FastCGI的优化

3.9 nginx的防盗链设置

3.10 nginx下的目录索引(非需要,请不要设置)

3.11 nginx 的目录别名支持php配置(非需要请不要配置,最好禁止)

3.12 你可以很容易地阻止User-Agents,如扫描器,机器人以及滥用你服务器的垃圾邮件发送者。

3.13正确途径取得源代码,勤打补丁

3.14 nginx 的日志权限授予700的权限

3.15禁止php解析指定目录

3.16 使用cdn进行网站加速,静态内容缓存到cdn

3.17 尽量减少http请求,提高网站响应速度,将文件合并达到降低Http请求的目的

4. 安装php插件,优化php

4.1编译安装 eaccelerator

4.2安装PDO_MYSQL插件

4.3安装ImageMagick图像软件

4.4安装zend 缓存及加速扩展模块

4.5安装xcache缓存加速扩展模块

4.6生产环境插件的安装建议

1.nginx的编译安装

1.1 安装libevent

tar zxf libevent-2.0.20-stable.tar.gz

cd libevent-2.0.20-stable

./configure

make && make install

1.2 安装pcre

tar zxf pcre-8.30.tar.gz

cd pcre-8.30

./configure

make && m5、安装:

make install

检查:

ls /usr/local 检查是否有pcre-7.8目录

ls /usr/local/lib   检查是否有pcre目录

ls /usr/local/include   检查是否有pcre目录

将库文件导入cache:

方法1:在/etc/ld.so.conf中加入: /usr/local/lib/pcre,然后运行ldconfig

方法2:在/etc/ld.so.conf.d/下新生成一个文件(或在其中的文件中加入同样内容),文件内容为:

/usr/local/lib/pcre,然后运行ldconfig

1.3修改debug参数,减小nginx编译大小

[[email protected] cc]# diff gcc gcc.2012

174c174

< #CFLAGS=”$CFLAGS -g”

> CFLAGS=”$CFLAGS -g”

1.4编译安装OpenSSL(失败,等待重新安装)

[[email protected] openssl-1.0.1]# ./config –prefix=/usr/local/openssl

[[email protected] openssl-1.0.1]# make && make install

[[email protected] openssl-1.0.1]# ll /usr/local/openssl/

total 16

drwxr-xr-x 2 root root 4096 Oct 12 00:37 bin

drwxr-xr-x 3 root root 4096 Oct 12 00:37 include

drwxr-xr-x 4 root root 4096 Oct 12 00:37 lib

drwxr-xr-x 6 root root 4096 Oct 12 00:37 ssl

1.5 利用TCMAlloc优化Nginx的性能

简单介绍 TCMalloc的全称是Rhread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个程序。与标准的glibc库的Malloca,TCMalloc库在内存分配效率和速度上要高很多,可以提高服务器在高并发情况下的性能,降低系统负载

要安装TCMalloc库需要libunwind(32位不需要)和google-perftools两个包

64位系统需要安装libunwind库为64位CPU和操作系统的程序提供基本函数调用练和函数调用寄存器功能

CFLAGS=-fPIC

[[email protected] libunwind-0.99-alpha]# CFLAGS=-fpic ./configure

[[email protected] libunwind-0.99-alpha]# make CFLAGS=-fpic

[[email protected] libunwind-0.99-alpha]# make CFLAGS=-fPIC install

ibunwind的最新版本是1.0.1,那为什么不选择最新版本呢?google perftools的INSTALL文件中给了说明。版本低于0.99-beta的libunwind与preftools一起工作可能不正常,但是高于0.99-beta的版本中可能包含一些与perftools不兼容的代码(因为libunwind会调用malloc,可能会导致死锁)。libunwind在X86_64平台上和perftools有不少问题,不过不会影响核心的tcmalloc库,但是会影响perftools中的工具,例如cpu-profiler, heap-checker,heap-profiler。

[[email protected] gperftools-2.0]# ./configure

[[email protected] gperftools-2.0]# make

[[email protected] gperftools-2.0]# make  install

[[email protected] gperftools-2.0]# echo “/usr/local/lib” >/etc/ld.so.conf.d/usr_local_lib.conf

[[email protected] gperftools-2.0]# ldconfig

1.6 修改编译文件,隐藏nginx服务器

编译安装时,换成别的web服务器

[[email protected] nginx-1.2.4]# cat  src/core/nginx.h

/*

* Copyright (C) Igor Sysoev

* Copyright (C) Nginx, Inc.

*/

#ifndef _NGINX_H_INCLUDED_

#define _NGINX_H_INCLUDED_

#define nginx_version      1002004

#define NGINX_VERSION      “2.5″

#define NGINX_VER          “Apache/” NGINX_VERSION

#define NGINX_VAR          “Apache”

#define NGX_OLDPID_EXT     “.oldbin”

#endif /* _NGINX_H_INCLUDED_ */

编译结果

[[email protected] conf]# curl -I http://192.168.2.5:8080/

HTTP/1.1 200 OK

Server: Apache/2.5

Date: Sun, 25 Nov 2012 17:33:50 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Sun, 25 Nov 2012 17:32:07 GMT

Connection: keep-alive

Accept-Ranges: bytes

 

1.7 编译安装nginx

[[email protected] nginx-1.2.4]# useradd -s /sbin/nologin -M nginx

./configure

–prefix=/web/nginx1.2.4

–user=nginx

–group=nginx

–with-http_stub_status_module

–with-http_ssl_module

–with-http_realip_module

–http-client-body-temp-path=/var/tmp/nginx/client/

–http-proxy-temp-path=/var/tmp/nginx/proxy/

–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

–with-google_perftools_module

–with-http_stub_status_module                         //以取得一些网页的运行状态

–with-http_ssl_module                                 //支持https加密连接

–http-client-body-temp-path=/var/tmp/nginx/client/ //指定http客户端请求缓存文件存放目录

–http-proxy-temp-path=/var/tmp/nginx/proxy/    //指定http反向代理缓存文件存放目录

–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/     //指定FastCGI缓存文件存放目录

#######################################################################################

[[email protected] nginx-1.2.4]# make

[[email protected] nginx-1.2.4]# make install

2)建立软连接

[[email protected] nginx-1.2.4]# cd /web/

[[email protected] web]# ll

total 4

drwxr-xr-x 6 root root 4096 Oct 12 01:36 nginx1.2.4

[[email protected] web]# ln -s nginx1.2.4/ nginx

[[email protected] nginx-1.2.4]# cd /web/

[[email protected] web]# ll

total 4

drwxr-xr-x 6 root root 4096 Oct 12 01:36 nginx1.2.4

[[email protected] web]# ln -s nginx1.2.4/ nginx

备份配置文件

[[email protected] web]# cd nginx/conf/

[[email protected] conf]# cp nginx.conf nginx.conf.xp.20121016

使nginx 加载google-preftools

[[email protected] conf]# diff nginx.conf nginx.conf.xp.20121016

3c3

< worker_processes  4;

> worker_processes  1;

10c10

< google_perftools_profiles /tmp/tcmalloc;

> [[email protected] conf]# ../sbin/nginx -t

nginx: the configuration file /web/nginx1.2.4/conf/nginx.conf syntax is ok

nginx: [emerg] mkdir() “/var/tmp/nginx/client/” failed (2: No such file or directory)

nginx: configuration file /web/nginx1.2.4/conf/nginx.conf test failed

[[email protected] conf]# mkdir /var/tmp/nginx/client -p

[[email protected] conf]# ../sbin/nginx -t

nginx: the configuration file /web/nginx1.2.4/conf/nginx.conf syntax is ok

nginx: configuration file /web/nginx1.2.4/conf/nginx.conf test is successful

[[email protected] conf]# ../sbin/nginx

查看google-perftools是否加载成功

[[email protected] conf]# ../sbin/nginx -s reload

[[email protected] conf]# lsof -n |grep tcmalloc

nginx     21160 nginx   11w      REG        8,3         0    2219532 /tmp/tcmalloc.21160

nginx     21161 nginx   13w      REG        8,3         0    2219540 /tmp/tcmalloc.21161

nginx     21162 nginx   15w      REG        8,3         0    2219533 /tmp/tcmalloc.21162

nginx     21163 nginx   17w      REG        8,3         0    2219535 /tmp/tcmalloc.21163

查看nginx文件大小

[[email protected] conf]# du -sh /web/

692K    /web/

验证Nginx是否安装成功

1.7配置nginx 支持fastcgi

1)将下面的信息 写到/web/nginx/con/fastcgi.conf下

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with –enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS 200;

2.编译安装php

2.1 安装libevent

[[email protected] libevent-2.0.20-stable]# ./configure

[[email protected] libevent-2.0.20-stable]# make && make install

2.2 编译安装php

1)编译安装php

./configure

–prefix=/web/php5.3.10

–with-mysql=/usr/local/mysql

–with-xmlrpc

–with-openssl

–with-zlib

–with-freetype-dir

–with-gd

–with-jpeg-dir

–with-png-dir

–with-iconv=/usr/local/libiconv

–enable-short-tags

–enable-sockets

–enable-zend-multibyte

–enable-soap

–enable-mbstring

–enable-static

–enable-gd-native-ttf

–with-curl

–with-xsl

–enable-ftp

–with-libxml-dir

–enable-sigchild

–enable-pcntl

–enable-bcmath

–disable-debug

–enable-fpm

Make && make install

–disable-debug   提高性能

–enable-fpm 支持fastcfgi 必须安装

特别说明#以下为解决msn robot “Call to undefined function pcntl_fork()”报错而加,适合nagios的PHP环境!

不需要报警暂时不用加

–enable-sigchild

–enable-pcntl

–enable-bcmath

############################################

2)复制配置文件建立软连接

[[email protected] php-5.3.10]# cp php.ini-production /web/php5.3.10/etc/php.ini

[[email protected] php-5.3.10]# cd /web/

[[email protected] web]# ln -s php5.3.10/ php

[[email protected] web]# ll

total 8

lrwxrwxrwx  1 root root   11 Oct 12 01:37 nginx -> nginx1.2.4/

drwxr-xr-x  8 root root 4096 Oct 12 01:40 nginx1.2.4

lrwxrwxrwx  1 root root   10 Oct 12 02:30 php -> php5.3.10/

drwxr-xr-x 10 root root 4096 Oct 12 02:24 php5.3.10

2.3 设置FastCGI,支持nginx解析Php

编译php-fpm文件

[[email protected] web]# cd  php/etc/

[[email protected] etc]# mv php-fpm.conf.default php-fpm.conf

[[email protected] etc]# cp php-fpm.conf php-fpm.conf.xp.20121016

[[email protected] etc]# diff php-fpm.conf php-fpm.conf.xp.20121016

132,133c132,133

< user = nginx###运行使用的用户

< group = nginx

> user = nobody

> group = nobody

164c164

< listen.allowed_clients = 192.168.2.5###允许访问FastCGI进程解析器的IP地址

> ;listen.allowed_clients = 127.0.0.1

201c201

< pm.max_children = 64 ###FastCGI开启的进程数,官方建议小于2G内存开启64个进程,大于4G开启200个进程

> pm.max_children = 5

227c227

< ;pm.max_requests = 10000###设置FastCGI处理多少个请求后关闭

> ;pm.max_requests = 500

420c420

< request_terminate_timeout = 0###设置FastCGI脚本执行的时间

> ;request_terminate_timeout = 0

424c424

< rlimit_files = 65534###设置PHP-FPM打开的最大文件描述符(需要和Linux内核打开文件描述符相关联)

> ;rlimit_files = 1024

3)设置FastCGI后台运行

[[email protected] etc]# nohup /web/php/sbin/php-fpm &

[1] 1311

[[email protected] etc]# nohup: appending output to `nohup.out’

[1]+  Done                    nohup /web/php/sbin/php-fpm

4)将FastCGI设置为开机自启动

[[email protected] etc]# echo “nohup /web/php/sbin/php-fpm &” >>/etc/rc.local

[[email protected] etc]# tail -1 /etc/rc.local

nohup /web/php/sbin/php-fpm &

2.4修改nginx连接FastCGI解析测试php解析

[[email protected] conf]# diff nginx.conf nginx.conf.xp.20121016

2,3c2,3

< user  nginx nginx ;

< worker_processes  4;

> #user  nobody;

> worker_processes  1;

7c7

< error_log  logs/error.log  info;  ###开启错误信息,便于排错

> #error_log  logs/error.log  info;

10c10

< google_perftools_profiles /tmp/tcmalloc;

>

65,71c65,71

<         location ~ .php$ {

<             root           html;

<             fastcgi_pass   127.0.0.1:9000;

<             fastcgi_index  index.php;

<             fastcgi_param  SCRIPT_FILENAME  /web/nginx/html$fastcgi_script_name;###这里的html后面请不要加/ 否则可能解析不了

<             include        fastcgi_params;

<         }

>         #location ~ .php$ {

>         #    root           html;

>         #    fastcgi_pass   127.0.0.1:9000;

>         #    fastcgi_index  index.php;

>         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

>         #    include        fastcgi_params;

>         #}

测试访问

3 Nginx编译完成后的优化

3.1 配置nginx的日志轮询

Nginx没有向apache那样的日志轮训工具,这里使用shell脚本

#!/bin/sh

logfile=/var/run/access.log

log_server=/log/web

log=/var/run/$(date +%F-%m).web.tar.gz

nginx=/web/nginx/sbin/nginx

num=30

cd /var/run

tar zcf ./$(date +%F-%m).web.tar.gz ./access.log

$nginx -s reload

mv   $log $log_server/

find $log_server/ -mtime +$num -type f |xargs rm -f

写定时任务

[[email protected] web]# echo “#nginx log cut  by root on every 23:55″ >>/var/spool/cron/root

[[email protected] web]# echo “55 23 * * * /bin/sh /server/script/nginxlog.sh” >>/var/spool/cron/root

[[email protected] web]# crontab -l

#nginx log cut  by root on every 23:55

55 23 * * * /bin/sh /server/script/nginxlog.sh

修改时间测试

[[email protected] web]# date -s 23:54:00

Tue Oct 16 23:54:00 CST 2012

清空目录测试

[[email protected] web]# rm -rf *

[[email protected] web]# ll

total 4

-rw-r–r– 1 root root 323 Oct 16 23:55 2012-10-16-10.web.tar.gz

测试自动删除功能

[[email protected] web]# date -s “2012-11-27″ ###修改系统时间

[[email protected] web]# sh  /server/script/nginxlog.sh

[[email protected] web]# ll

total 24

-rw-r–r– 1 root root 323 Oct 28 00:00 2012-10-28-10.web.tar.gz

-rw-r–r– 1 root root 323 Nov  2 00:00 2012-11-02-11.web.tar.gz

-rw-r–r– 1 root root 323 Nov  5 00:00 2012-11-05-11.web.tar.gz

-rw-r–r– 1 root root 323 Nov  6 00:00 2012-11-06-11.web.tar.gz

-rw-r–r– 1 root root 323 Nov  7 00:00 2012-11-07-11.web.tar.gz

-rw-r–r– 1 root root 323 Nov 26 00:01 2012-11-26-11.web.tar.gz

脚本最终效果

[[email protected] web]# cat  /server/script/nginxlog.sh

#!/bin/sh

logfile=/var/run/access.log

log_server=/log/web

log=/var/run/$(date +%F-%m).web.tar.gz

nginx=/web/nginx/sbin/nginx

num=30

cd /var/run

tar zcf ./$(date +%F-%m).web.tar.gz ./access.log

$nginx -s reload

mv   $log $log_server/

find $log_server/ -ctime +$num -type f |xargs rm -f

3.2 配置错误页面的优雅显示

方法一:

更改nginx.conf在http定义区域加入:(全局设置,适用于该nginx下的全部站点)

fastcgi_intercept_errors on;

error_page 404 =http://www.****.com/notfound.html;

方法二:

另外一种是在http下加入:

fastcgi_intercept_errors on;

在server里加入:

error_page 404 http://www.***.com/notfound;

location /notfound {

rewrite ^(.*) http://www.***.com/notfound permanent;

}

3.3 开启gzip 压缩传输,提供文件解压缩功能,将html css 图片等先进行压缩,然后再传输。这样可以 减少带宽,使文件传输到客户端浏览器,然后再进行解压缩查看,提高用户体验。并让客户端缓存图片等信息,减少客户端与服务器的交互,减小服务器压力

1)开启gzip的解压缩功能

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

#gzip_http_version 1.0;

gzip_comp_level 2;

gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-http

d-php image/jpeg image/gif image/png;

gzip_vary off;

gzip_disable “MSIE [1-6].”;

第1行:开启Gzip

第2行:不压缩临界值,大于1K的才压缩,一般不用改

第3行:buffer,就是,嗯,算了不解释了,不用改

第4行:用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1

第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧

第6行:进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了

第7行:跟Squid等缓存服务有关,on的话会在Header里增加”Vary: Accept-Encoding”,我不需要这玩意,自己对照情况看着办吧

第8行:IE6对Gzip不怎么友好,不给它Gzip

2)浏览器端缓存的设置

可以放在http 里对全局进行缓存 也可以放在servre 对指定网站的内容缓存 还可以定义指定类型缓存 放在location里

expires 10y;

以js结尾的url

location ~ .js$ {

expires 10y;

}

3)压缩传输 缓存加速的效果

压缩了三分之二,缓存了10年

3.4修改nginx运行的账户,最好大家都不知道的

[[email protected] conf]# egrep ‘user’ nginx.conf

user  nginx nginx ;

#########这里暂时我就不修改了###############

3.5做压力测试,修改nginx的敏感信息

1) 未修改前的

[[email protected] conf]# curl -I http://192.168.2.5/

HTTP/1.1 403 Forbidden

Server: nginx/1.2.4

Date: Sun, 25 Nov 2012 16:48:05 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

将这歌参数加到server{}里

[[email protected] conf]# grep server_tokens nginx.conf

server_tokens off;

验证效果

[[email protected] conf]# curl -I http://192.168.2.5/

HTTP/1.1 403 Forbidden

Server: nginx

Date: Sun, 25 Nov 2012 17:04:37 GMT

Content-Type: text/html

Content-Length: 162

Connection: keep-alive

2)安装webbench进行压力测试

[[email protected] webbench-1.5]# ls

ChangeLog  COPYRIGHT  debian  Makefile  socket.c  webbench.1  webbench.c

[[email protected] webbench-1.5]# make && make install

cc -Wall -ggdb -W -O   -c -o webbench.o webbench.c

webbench.c: In function ‘alarm_handler’:

webbench.c:77: warning: unused parameter ‘signal’

cc -Wall -ggdb -W -O  -o webbench webbench.o

ctags *.c

install -s webbench /usr/local/bin

install -m 644 webbench.1 /usr/local/man/man1

install -d /usr/local/share/doc/webbench

install -m 644 debian/copyright /usr/local/share/doc/webbench

install -m 644 debian/changelog /usr/local/share/doc/webbench

[[email protected] webbench-1.5]# /usr/local/bin/webbench -c 500 -t 1 http://192.168.2.5

Webbench – Simple Web Benchmark 1.5

Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Invalid URL syntax – hostname don’t ends with ‘/’.

[[email protected] webbench-1.5]# /usr/local/bin/webbench -c 500 -t 1 http://192.168.2.5/

Webbench – Simple Web Benchmark 1.5

Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.2.5/

500 clients, running 1 sec.

Speed=1239059 pages/min, 6284525 bytes/sec.

Requests: 20651 susceed, 0 failed.

-c 是指定每秒的并发数目

-t 是持续的时间

上面这个操作表示模拟100个客户端运行5秒来的首页,请求了20651次,其中成功20651次,失败0次。

所以速度是每分钟1239059次(299*60/5,把秒转发成分钟),请求字节数每秒6284525 字节

受带宽和硬件本身的影响,所以测试数据,可能有所差异

3.6 网站文件权限安全设置

① 网站目录的权限应设置为755 ,文件664

② 用户目录uploads 和userfile 应禁止Php程序解析

③ 多网站同时提供服务,请为目录单独设置属主,防止跨站

④ 如果是dedecms等安装程序,执行安装完毕,请将install 文件移动到别处或者删除

⑤ 要经常备份网站数据,以防万一

⑥ 如果有测试Phpinfo文件请移到别处或者删除

############################

禁止指定目录解析脚本演示

定义的location

location ~ .js$ {

root /web/nginx/html;

deny all;

}

3.7 增加并发连接数目,使用epoll网络模型

1)nginx的优化

优化参数一】

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

worker_processes 8;

worker_rlimit_nofile 102400;

worker_connections 102400;

Use epoll

keepalive_timeout 60;

sendfile on;

tcp_nopush     on;

tcp_nodelay on;

client_header_buffer_size 4k;

open_file_cache max=102400 inactive=20s;

open_file_cache_valid 30s;

open_file_cache_min_uses 1;

优化参数二】

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

worker_rlimit_nofile 102400;

worker_connections 102400;

sendfile on;

tcp_nopush     on;

tcp_nodelay on;

keepalive_timeout 60;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size8m;

###########推荐二#################

一条警告

[[email protected] conf]# /web/nginx/sbin/nginx -t

nginx: the configuration file /web/nginx1.2.4/conf/nginx.conf syntax is ok

nginx: [warn] the number of “worker_processes” is not equal to the number of “worker_cpu_affinity” masks, using last mask for remaining worker processes

nginx: configuration file /web/nginx1.2.4/conf/nginx.conf test is successful

这里主要是因为 我们设置的使用能够nginx的进程数,和指定 的cpu个数不一致导致的

与下面两条参数有关

worker_processes 8; 进程数

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; 为进程指定cpu

如果是纯静态网站,可以加大进程数目,提高nginx的性能

转博客教主 http://www.ha97.com/1124.html

Nginx的worker_processes设置为多少比较好 .

搜索到原作者的话:

As a general rule you need the only worker with large number of

worker_connections, say 10,000 or 20,000.

However, if nginx does CPU-intensive work as SSL or gzipping and

you have 2 or more CPU, then you may set worker_processes to be equal

to CPU number.

Besides, if you serve many static files and the total size of the files

is bigger than memory, then you may increase worker_processes to

utilize a full disk bandwidth.

Igor Sysoev

一般一个进程足够了,你可以把连接数设得很大。如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。或 者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加,以充分利用IO带宽(主要似乎是IO操作有block)。

根据我配置实践,服务器是“多个CPU+gzip+网站总文件大小大于内存”的环境,worker_processes设置为CPU个数的两倍比较好。

3.8 FastCGI的优化

配置优化参数一】

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

配置优化参数二】

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 16k;

fastcgi_buffers 16 16k;

fastcgi_busy_buffers_size 16k;

fastcgi_temp_file_write_size 16k;

fastcgi_cache TEST;

fastcgi_cache_valid 200 302 1h;

fastcgi_cache_valid 301 1d;

fastcgi_cache_valid any1m;

fastcgi_cache_min_uses 1;

fastcgi_cache_use_stale error timeout invalid_header http_500;

open_file_cache max=204800 inactive=20s;

open_file_cache_min_uses 1;

open_file_cache_valid 30s;

转 他人博客的开启Fastcgi缓存的测试

几张测试结果

静态页面为我在squid配置4W并发那篇文章中提到的测试文件,下图为同时在6台机器运行webbench -c 30000 -t 600 http://ad.test.com:8080/index.html命令后的测试结果:

使用netstat过滤后的连接数:

php页面在status中的结果(php页面为调用phpinfo):

php页面在netstat过滤后的连接数:

未使用FastCGI缓存之前的服务器负载:

此时打开php页面已经有些困难,需要进行多次刷新才能打开。上图中cpu0负载偏低是因为测试时将网卡中断请求全部分配到cpu0上,并且在nginx中开启7个进程分别制定到cpu1-7。

使用FastCGI缓存之后:

此时可以很轻松的打开php页面。

3.9 nginx的防盗链设置

一:一般的防盗链如下:

location ~* .(gif|jpg|png|swf|flv)$ {

valid_referers none blocked www.ingnix.com ;

if ($invalid_referer) {

rewrite ^/ http://www.ingnix.com/retrun.html;

#return 404;

}

}

第一行:gif|jpg|png|swf|flv

表示对gif、jpg、png、swf、flv后缀的文件实行防盗链

第二行: 表示对www.ingnix.com这2个来路进行判断

if{}里面内容的意思是,如果来路不是指定来路就跳转到http://www.ingnix.com/retrun.html页面,当然直接返回404也是可以的。

二:针对图片目录防止盗链

location /images/ {

alias /data/images/;

valid_referers none blocked server_names *.xok.la xok.la ;

if ($invalid_referer) {return 403;}

}

3.10 nginx下的目录索引(非需要,请不要设置)

autoindex on;//自动显示目录

autoindex_exact_size off;//人性化方式显示文件大小否则以byte显示

autoindex_localtime on;//按服务器时间显示,否则以gmt时间显示

3.11 nginx 的目录别名支持php配置(非需要请不要配置,最好禁止)

#####没看懂#################

location ~ /pa/.+.php.*$ {

if ($fastcgi_script_name ~ /pa/(.+.php.*)$) {

set $valid_fastcgi_script_name $1;

}

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /var/data/phpmyadmin/$valid_fastcgi_script_name;

include  fastcgi_params;

}

3.12 你可以很容易地阻止User-Agents,如扫描器,机器人以及滥用你服务器的垃圾邮件发送者。

## Block download agents ##

if ($http_user_agent ~* LWP::Simple|BBBike|wget) {

return 403;

}

阻止Soso和有道的机器人:

## Block some robots ##

if ($http_user_agent ~* Sosospider|YodaoBot) {

return 403;

}

3.13正确途径取得源代码,勤打补丁

3.14 nginx 的日志权限授予700的权限

在我们的日志脚本中加如下面两条语句

chown root.root $log

chmod 700 $log

3.15禁止php解析指定目录

location ~ .php$ {

root /web/nginx/html;

deny all;

}

3.16 使用cdn进行网站加速,静态内容缓存到cdn

3.17 尽量减少http请求,提高网站响应速度,将文件合并达到降低Http请求的目的

3.18  nginx网站程序架构优化,程序页面服务器 图片服务器 上传服务器  分离,图片服务器禁止php程序的解析

Nginx的优势主要在于静态小文件的处理,高并发模式下内存 cpu等方面的消耗低

但对动态文件的处理,不如apache,如果网站不是并发量很高,完全可以使用nginx代理apache,让apache处理动态请求,即使并发量很高也可以通过nginx的负载均衡技术,实现多台apache来支持高并发

4安装php插件,优化php

4.1 编译安装 eaccelerator

1)避免perl编译带来的问题

Echo ‘export LC_ALL=C’ >>/etc/profile

Source /etc/profile

错误现象

[[email protected] eaccelerator-0.9.5.2]# /application/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:

LANGUAGE = (unset),

LC_ALL = (unset),

LANG = “zh_cn.gb18030″

are supported and installed on your system.

perl: warning: Falling back to the standard locale (“C”).

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:

LANGUAGE = (unset),

LC_ALL = (unset),

LANG = “zh_cn.gb18030″

are supported and installed on your system.

perl: warning: Falling back to the standard locale (“C”).

[[email protected] eaccelerator-0.9.6.1]# /web/php/bin/phpize

2)编译安装eAccelerator

eAccelerator是一个自由的开放源代码的php加速、优化和动态内容缓存扩展模块,提升php程序的缓存性能,降低PHP程序在解析时对服务器的性能开销。eAccelerator还有对PHP优化作用,加快其执行效率。使PHP程序代码执效率能提高1-10倍;

3)安装过程

[[email protected] eaccelerator-0.9.6.1]# /web/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[[email protected] eaccelerator-0.9.6.1]# ./configure –enable-eaccelerator=shared –with-php-config=/web/php/bin/php-config

[[email protected] eaccelerator-0.9.6.1]# make

[[email protected] eaccelerator-0.9.6.1]# make install

Installing shared extensions:     /web/php5.3.10/lib/php/extensions/no-debug-non-zts-20090626/

4)检查是否安装成功

[[email protected] eaccelerator-0.9.6.1]# ll /web/php5.3.10/lib/php/extensions/no-debug-non-zts-20090626/

total 356

-rwxr-xr-x 1 root root 356481 Nov 26 14:07 eaccelerator.so

5)配置eaccelerator插件

[[email protected] php-5.3.10]# sed -i ‘s#; extension_dir = “./”#extension_dir = “/web/php5.3.10/lib/php/extensions/no-debug-non-zts-20090626/”#g’ /web/php/lib/php.ini

6)将这端内容追加到php.ini下就可以了

[eaccelerator]

extension=eaccelerator.so

eaccelerator.shm_size=”64″

eaccelerator.cache_dir=”/tmp/eaccelerator”

eaccelerator.enable=”1″

eaccelerator.optimizer=”1″

eaccelerator.check_mtime=”1″

eaccelerator.debug=”0″

eaccelerator.filter=”"

eaccelerator.shm_max=”0″

eaccelerator.shm_ttl=”3600″

eaccelerator.shm_prune_period=”3600″

eaccelerator.shm_only=”0″

eaccelerator.compress=”1″

eaccelerator.compress_level=”9″

7)检查是否安装成功

[[email protected] php-5.3.10]# /web/php/bin/php  -v

PHP 5.3.10(cli) (built: Oct 12 2012 02:23:35)

Copyright (c) 1997-2012 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

8)为eaccelerator建立缓存的目录

[[email protected] php-5.3.10]# mkdir /tmp/eaccelerator/

[[email protected] php-5.3.10]# chown nginx.nginx /tmp/eaccelerator/

9)然后访问页面就会看到目录下生成的缓存内容了

[[email protected] php-5.3.10]# cd /tmp/eaccelerator/

[[email protected] eaccelerator]# ls

0  1  2  3  4  5  6  7  8 9  a  b  c  d  e  f

10)缓存的数据

[[email protected] eaccelerator]# tree

|– b

|   |– 0

|   |– 1

|   |– 2

|   |– 3

|   |– 4

|   |   `– eaccelerator-1502.5681991

|   |– 5

11)如果 没看到数据 就试着把eaccelerator作为zend的一个插件来用如下

[eaccelerator]

zend_extension=”/web/php5.3.10/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so”

;extension=eaccelerator.so

eaccelerator.shm_size=”64″

eaccelerator.cache_dir=”/tmp/eaccelerator”

eaccelerator.enable=”1″

eaccelerator.optimizer=”1″

eaccelerator.check_mtime=”1″

eaccelerator.debug=”0″

eaccelerator.filter=”"

eaccelerator.shm_max=”0″

eaccelerator.shm_ttl=”3600″

eaccelerator.shm_prune_period=”3600″

eaccelerator.shm_only=”0″

eaccelerator.compress=”1″

eaccelerator.compress_level=”9″

extension = pdo_mysql.so

extension = imagick.so

eaccelerator.log_file = “/tmp/eacc.log”

我在nginx这样就实现了刚刚那样就 不能不知道为什么、

4.2安装PDO_MYSQL插件

PDO扩展为PHP访问数据库定义了一个轻量级的、一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据。

1)安装过程

[[email protected] PDO_MYSQL-1.0.2]# /web/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[[email protected] PDO_MYSQL-1.0.2]# ./configure  –with-php-config=/web/php/bin/php-config  –with-pdo-mysql=/usr/local/mysql

[[email protected] PDO_MYSQL-1.0.2]# ll /web/php5.3.10/lib/php/extensions/no-debug-non-zts-20090626/

total 484

-rwxr-xr-x 1 root root 356481 Nov 26 14:07 eaccelerator.so

-rwxr-xr-x 1 root root 124372 Nov 26 14:45 pdo_mysql.so

将下面的内容追加到Php.ini下面

extension = pdo_mysql.so

4.3安装ImageMagick图像软件

软件说明:

ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过89种基本格式的图片文件,包括流行的TIFF、JPEG、GIF、 PNG、PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存,对图片的操作,即可以通过命令行进行,也可以用C/C++、Perl、Java、PHP、Python或Ruby编程来完成。同时ImageMagick提供了一个高质量的2D工具包,部分支持SVG。现在,ImageMagic的主要精力集中在性能、减少bug以及提供稳定的API和ABI上。

1. 将图片从一个格式转换到另一个格式,包括直接转换成图标。

2. 改变尺寸、旋转、锐化(sharpen)、减色、图片特效

3. 缩略图片的合成图( a montage of image thumbnails)

4. 适于web的背景透明的图片

5. 将一组图片作成gif动画,直接convert

6. 将几张图片作成一张组合图片,montage

7. 在一个图片上写字或画图形,带文字阴影和边框渲染。

8. 给图片加边框或框架

9. 取得一些图片的特性信息

10,几乎包括了gimp可以作到的常规插件功能。甚至包括各种曲线参数的渲染功能。只是那命令的写法,够复杂。

tar zxf ImageMagick.tar.gz

cd ImageMagick-6.5.1-2/

./configure

make

make install

cd ../

安装imagick php扩展插件

插件说明:

特别提示:imagick插件工作需要ImageMagick软件的支持,所以,必须要先安装ImageMagick,否则会报错。

一个可以供PHP调用ImageMagick功能的PHP扩展。使用这个扩展可以使PHP具备和ImageMagick相同的功能。

装了ImageMagick-6.5.1-2图像程序后,再装php的扩展imagick插件,才能使用ImageMagick-6.5.1-2提供的api来进行图片的创建与修改压缩等操作,都集成在imagick这个扩展中。

安装过程

tar zxf imagick-2.3.0.tgz

cd imagick-2.3.0

/application/php/bin/phpize

[[email protected] imagick-2.3.0RC3]# ./configure –with-php-config=/web/php/bin/php-config

[[email protected] imagick-2.3.0RC3]# make

[[email protected] imagick-2.3.0RC3]# make install

[[email protected] imagick-2.3.0RC3]# ll /web/php5.3.10/lib/php/extensions/no-debug-non-zts-20090626/

total 1300

-rwxr-xr-x 1 root root 356481 Nov 26 14:07 eaccelerator.so

-rwxr-xr-x 1 root root 831209 Nov 26 15:40 imagick.so

-rwxr-xr-x 1 root root 124372 Nov 26 14:45 pdo_mysql.so

将下面的内容追加到php.ini里

extension = imagick.so

如果您喜欢这篇文章(请按Ctrl+D快速收藏)。

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。