nginx参数优化以及编译扩展模块
nginx相关优化参数
1.隐藏nginx版本号
在nginx.conf配置文件中,http,server,location段添加
server_tokens off;
2.想要彻底修改nginx错误返回页面
在编译安装之前修改nginx.h文件
sed -n '13,17p' src/core/nginx.h
#define NGINX_VERSION "1.6.2" #将版本随便修改为"2.2.2"
#define NGINX_VER "nginx/" NGINX_VERSION #nginx可以改为shnneWS
#define NGINX_VAR "NGINX" #NGINX更改为shnne
#define NGX_OLDPID_EXT ".oldbin"
sed -n '49p' src/http/ngx_http_header_filter_module.c
static char ngx_http_server_string[] = "Server: nginx" CRLF; #将nginx更改为shnne
sed -n '21,29p' src/http/ngx_http_special_response.c
static u_char ngx_http_error_full_tail[] =
"<hr><center>" nginx "</center>" CRLF #将nginx修改为"shnne"
"</body>" CRLF
"</html>" CRLF
最后重新编译安装
./configure --prefix=/application/nginx-1.6.2 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make
make install
以下大部分都是在Module ngx_http_core_module
3.配置nginx worker进程个数
在高并发的场景,我们需要实现启动更多的nginx进程以确保快速响应并处理用户请求
worker_processes 2;
[建议进程数最好与cpu核数相等或者是2倍]
查看cpu内核数的方法:
方法一:
grep "physical id" /proc/cpuinfo
方法二:
top 按数字键 “1”
方法三:
grep "processor" /proc/cpuinfo
4.根据cpu核数进行nginx进程优化(放在模块部分之上)
worker_cpu_affinity 0001 0010 0100 1000;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;
task命令可以设置CPU亲和力
taskset -c 1,2,3 /etc/init.d/mysql start
5.调整单个worker进程允许的客户端的最大连接数(在events模块内添加)
worker_connections 20000;
use epoll;
6.配置每个worker进程打开的最大文件数和用户(放在模块部分之上)
worker_rlimit_nofile 65535;
user nginx;
7.优化服务器名字的hash表大小(放在http模块内)
server_names_hash_max_size 512;
server_names_hash_bucket_size 64;
参数作用:设置存放域名(server names)的最大哈希表的存储桶(bucket)的大小,默认值依赖CPU的缓存行
8.开启高效传输模式(放在http模块内)
sendfile on;
tcp_nopush on;
tcp_nodelay on;
sendfile参数用于开启文件高效传输模式,同时将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞
9.设置连接超时时间(放在http模块内)
keepalive_timeout 60;#设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接
client_header_timeout 15; #设置客户端请求头读取超时时间,如果过了这个时间,客户端还没有发送任何数据,Nginx将返回"Request timeout(408)"错误
client_body_timeout 15; #设置响应客户端超时时间,这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将关闭连接
10.上传文件大小限制(动态应用)(放在http模块内)
client_max_body_size 8m;
11.配置错误页面优雅显示
location / {
index index.html index.php index.htm;
error_page 404 /404.html;
}
location = /50x.html {
root html;
}
12.fastcgi调优(配合PHP引擎动态服务)
用户读取cache缓存区
fastcgi_cache ngx_fcgi_cache;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host:9000$request_uri;
读磁盘写入buffer缓冲区
fastcgi_connect_timeout 240;
fastcgi_send_timeout 240;
fastcgi_read_timeout 240;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_cache_path /data/ngx_fcgi_cache levels=1:2 keys_zone=ngx_fcgi_cache:100m inactive=1d max_size=40g;
13.配置nginx gzip压缩功能(放在http模块内)
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
14.配置nginx expires缓存功能(放在server模块内)
图片,附近一般不会被用户修改,如果用户修改了,实际上也都是更改文件名重新上传而已,网站升级对于js,css元素,一般可以改名
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)?$
{
expires 3650d;
break;
}
location ~.*\.(js|css)?$
{
expires 30d;
break;
}
2)根据目录进行判断,添加expires功能范例
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
expires 360d;
}
15 静止不同的浏览器访问
if ($http_user-agent ~* "Firefox|MSIE"){
return 403;
}
不记录不需要的访问日志
location ~ .*\.(js|jpg|JPG|jepg|JPEG|css|bmp|gif|GIF)${
access_log off;
}
配置nginx限制指定目录下的指定程序被解析
location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)$
{
deny all;
}
location ~ ^/static/.*\.(php|php5|.sh|.pl|.py)$
{
deny all;
}
location ~ ^/data/(attachment|avatar)/.*\.(php|php5)$
{
deny all;
}
16.限制来源ip访问指定的目录
location ~ ^/shnne {
allow 202.111.12.211;
deny all;
}
location ~ ^/shnne/ {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
17.设置tmpfs内存文件系统
mkdir /opt/tmp
cd /tmp
mv * /opt/tmp
mount -t tmpfs -o size=20m tmpfs /tmp
df -h
想要永久生效,可以放在/etc/rc.local 或者是 /etc/fstab
18.防止盗链
a.根据http referer实现防盗链
在http协议中,有一个表头叫referer,使用URL格式来表示从那里来的连接到当前的网页资源。通过referer可以检测目标访问的来源网页,如果是资源文件,可以跟踪到显示它的网页地址,一旦检测出来来源不是本站的,就进行阻止或者返回到指定的页面,Apache,nginx,lighttpd三者都支持防盗链
b.根据cookies处理
c.通过加密变换路径实现防盗链
利用浏览器的referer并且针对扩展名rewrite重定向
location ~ .*\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)?$ {
valid_referers none blocked *.shnne.com shnne.com;
if ($invalid_referer) {
rewrite ^/ https://www.shnne.com/img/nolink.jpg;
}
}
20.防止网站恶意解析
案例:你的网站权重比较高,为了提高权重,然后有人恶意解析域名到你的服务器ip地址
方法一:
server {
listen 80 default_server;
server_name _;
return 501;
}
方法二:
if ($host !~ ^www/.test/.com$){
rewrite ^(.*) http://www. test.com$1 permanent;
}
21.Linux内核优化参数
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
##net.nf_conntrack_max = 25000000
##net.netfilter.nf_conntrack_max = 25000000
##net.netfilter.nf_conntrack_tcp_timeout_established = 180
##net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
#writen by troy#
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time =600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#for iptables
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
apache模式
在配置编译过程中,加入--with-mpm=worker,如果不加入的话系统会采用prefork模式
优点:内存占用比prefork模式低,适合高并发、高流量的http服务
缺点:加入一个线程崩溃,整个进程就会连同其它线程一起死掉。由于线程共享内存空间,所以一个程序在运行时必须被系统识别为"每个线程都是安全的",服务的稳定性不如prefork模式
【Apache和nginx添加新模块的编译安装方法】
apache(/application/apache/bin/apxs)
cd /server/tools/httpd-2.2.22/modules/metadata/#切换到apache软件目录mod_expires程序下
/application/apache/bin/apxs -c -i -a mod_expires.c #以dos的方式编译到apache中
ll /application/apache2.2.22/moudules/mod_expires.so
apxs参数说明
-c 此选项表示需要执行的操作。它首先会编译C源程序(.c)files为对应的目标代码文件(.o)
nginx(/application/php/bin/phpize)
客户端安装memcache
http://pecl.php.net/package/memcache
1)在nginx服务器安装memcache客户端
cd /server/tools
wget -q http://pecl.php.net/get/memcache-2.2.7.tgz
tar zxf memcache-2.2.7.tgz
cd memcache-2.2.7
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/application/php/bin/php-config
make
make install
cd ../
2)查看是否安装成功
[root@lixiang tools]# ls -l /application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
total 244
-rwxr-xr-x 1 root root 246672 Sep 4 14:08 memcache.so
3)修改php.ini配置文件
extension_dir = "/application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/"
extension=memcache.so
<< 上一篇
下一篇 >>
网友留言(0 条)