一,所需软件包
1,GCC编译器,编译源码包的时候使用
apt-get install gcc
apt-get install g++
或者
yum -y install gcc
yum -y install gcc-c++
用 gcc -v 测试安装是否成功
2,PCRE(Perl Compatible Regular Expressions)库,用于支持正则表达式。
官网:http://sourceforge.net/projects/pcre/files/pcre/
3,openssl(Secure Socket Layer),如果需要在SSL协议上传输HTTP,就需要安装
sudo apt-get install openssl
sudo apt-get install libssl-dev
#如果yum命令
yum install -y openssl openssl-devel
4,
ngx_cache_purge-2.1 ,用于清除指定URL的缓存。属于nginx的第三方模块,可选择安装。
5,nginx官网下载稳定版本:http://nginx.org/en/download.html
二,安装
tar zxvf pcre-8.02.tar.gz
cd pcre-8.0.2/
./configure
make && make install
tar zxvf ngx_cache_purge-2.1.tar.gz
tar zxvf nginx-1.4.4.tar.gz
cd nginx-1.4.4/
./configure --user=username --group=groupname --add-module=../ngx_cache_purge-2.1 --prefix=/usr/local/nginx
--with-http_stub_status_module --with-http_ssl_module <span style="color: rgb(68, 68, 68); font-family: 微软雅黑, 宋体, Tahoma, Georgia, Times, 'Times New Roman', serif; line-height: 20.4px;">--with-http_gzip_static_module (gzip压缩模块)</span>
make && make install
三,配置
#user nobody;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - '
'$http_user_agent';
access_log logs/access.log main;
#keepalive_timeout 0;
keepalive_timeout 60s;
charset utf-8;
#Sets buffer size for reading client request header
client_header_buffer_size 16k;
#Sets the maximum number and size of buffers used for reading large client request header
large_client_header_buffers 4 32k;
#Enables or disables the use of sendfile()
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_body_buffer_size 16k;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_send_timeout 5s;
# proxy_busy_buffers_size 128k;
# proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 20k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_temp_path /usr/local/nginx/proxy_temp/adserver/images_temp;
proxy_cache_path /usr/local/nginx/proxy_temp/adserver/images_cache levels=1:2 keys_zone=picture:1024m inactive=1d max_size=50g;
server {
listen 80;
server_name 192.168.1.103;
#access_log logs/host.access.log main;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
proxy_cache picture;
#Sets caching time for different response codes
proxy_cache_valid 200 304 12h;
#Defines a key for caching
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped.
proxy_pass http://192.168.1.103:7001;
#Sets the path, format, and configuration for a buffered log write
access_log /usr/local/nginx/logs/cache.log cachelog;
expires 30d;
}
#clean the cache
location ~ /purge(/.*){
allow 192.168.0.0/16;
deny all;
proxy_cache_purge picture $host$1$is_args$args;
}
location /nginx_status{
stub_status on;
access_log off;
allow 192.168.0.0/16;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 7001;
server_name 192.168.1.103;
location / {
root /home/chenjinzhao/share;
}
access_log off;
}
}
1,sendfile
是否开启系统sendfile()调用。sendfile 实际上是 Linux 2.0+ 以后的推出的一个系统调用。磁盘文件被直接发送到了网卡的内存缓冲区,减少了数据复制和内核态切换的开销。
2,tcp_nopush
Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows
(1)sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.*;
(2)sending a file in full packets.
3,client_header_buffer_size 1k;(默认1k)
Nginx接受用户请求中HTTP header部分(包括HTTP行和HTTP头部)时分配的内存 buffer大小。有时,请求中的HTTP header部分可能会超过这个大小,这时large_client_header_buffers参数将会生效。
4,large_client_header_buffers 4 8k;
Nginx接受一个超大HTTP头部请求的buffer个数和每个buffer的大小。如果HTTP请求行(如GET/index HTTP/1.1)的大小超过上面的单个buffer,则返回“Request URI large”(414)。请求中一般会有许多header,每一个header的大小也不能超过单个buffer的大小,否则会返回“Bad request”(400)。当然,请求行和请求头部的总和也不可以超过buffer个数*buffer的大小。
5,keepalive_timeout
一个keepalive链接在闲置一定时间后(默认75s)服务器和浏览器都会去关闭这个链接。这个参数是用来约束Nginx服务器的,Nginx也会按照规范把这个时间传给浏览器。
6,tcp_nodelay
确定对keepalive链接是否使用TCP——NODELAY选项。
7,client_body_buffer_size
Nginx接受http包体的内存缓冲区大小。
8, proxy_connect_timeout
设置与后端服务器建立连接的超时时间。应该注意这个超时一般不可能大于75秒。
9,proxy_read_timeout
定义从后端服务器读取响应的超时。此超时是指相邻两次读操作之间的最长时间间隔,而不是整个响应传输完成的最长时间。如果后端服务器在超时时间段内没有传输任何数据,连接将被关闭。
10,proxy_send_timeout
定义向后端服务器传输请求的超时。此超时是指相邻两次写操作之间的最长时间间隔,而不是整个请求传输完成的最长时间。如果后端服务器在超时时间段内没有接收到任何数据,连接将被关闭。
11,proxy_buffers
为每个连接设置缓冲区的数量为number,每块缓冲区的大小为size。这些缓冲区用于保存从被代理的服务器读取的响应。每块缓冲区默认等于一个内存页的大小。这个值是4K还是8K,取决于平台。
12,proxy_busy_buffers_size
当开启缓冲响应的功能以后,在没有读到全部响应的情况下,写缓冲到达一定大小时,nginx一定会向客户端发送响应,直到缓冲小于此值。这条指令用来设置此值。 同时,剩余的缓冲区可以用于接收响应,如果需要,一部分内容将缓冲到临时文件。该大小默认是proxy_buffer_size和proxy_buffers指令设置单块缓冲大小的两倍。
13,proxy_temp_file_write_size
在开启缓冲后端服务器响应到临时文件的功能后,设置nginx每次写数据到临时文件的size(大小)限制。 size的默认值是proxy_buffer_size指令和proxy_buffers指令定义的每块缓冲区大小的两倍, 而临时文件最大容量由proxy_max_temp_file_size指令设置。
14,gzip_min_length 20k;
设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
15,gzip_buffers 4 16k;
设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
16,gzip_comp_level 2;
gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
17,gzip_types text/plain application/x-javascript text/css application/xml
匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
18,gzip_vary on;
和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
19,(2)proxy_cache_path指令,
语法 proxy_cache_path path [levels=number]
keys_zone=zone_name:zone_size[inactive=time] [max_size=size];
该指令用于设置缓存文件的存放路径.例:
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g ;
path 存放目录
levels 指定该缓存空间有两层hash目录,第一层目录为1个字母,第二层目录为2个字母,
保存的文件名会类似/data0/proxy_cache_dir/c/29/XXXXXX ;
keys_zone参数用来为这个缓存区起名.
500m 指内存缓存空间大小为500MB
inactive的1d指如果缓存数据在1天内没有被访问,将被删除
max_size的30g是指硬盘缓存空间为30G
20. proxy_cache_valid 200 304 301 302 10d;
哪些状态缓存多长时间
20,worker_processesworker进程的个数。如果不会出现阻塞式的调用,那么有多少cpu内核,就配置多少worker进程,反之,则worker进程可少多一些。
多worker进程可以充分利用多核系统架构,但若worker进程的数量多于cpu内核数,那么会增大进程间切换带来的消耗。一般情况下,配置与cpu内核相等的worker进程,并且使用worker_cpu_affinity配置来绑定cpu内核。
21,worker_cpu_affinity
绑定worker进程到指定的cpu内核。
22,ngx_http_stub_status_module
这个模块能够获取
Nginx自上次启动以来的工作状态。
第二行
服务器已接受的连接数:55(accepted connection #)
服务器已处理的连接数:55(handled connection #)
服务器已处理的请求:104
第三行
配置说明
第一行 当前的活跃连接数:1location / nginx_status {stub_status on;access_log off;allow SOME.IP.ADD.RESS;deny all;}
地址栏访问(本地):localhost/nginx_status将看到类似一下参数:
Active connections: 1 server accepts handled requests 55 55 104 Reading: 0 Writing: 1 Waiting: 0
第二行
服务器已接受的连接数:55(accepted connection #)
服务器已处理的连接数:55(handled connection #)
服务器已处理的请求:104
第三行
Reading – Nginx 读取到客户端的Header信息数 0;
Writting – Nginx 返回给客户端的Header信息数 1;
Waiting – 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接