注明:根据《实战Nginx:取代Apache的高性能Web服务器>》第九章修改,详细请参考以上这本书
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.20.tar.gz
wget http://nginx.org/download/nginx-1.1.10.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz
一、安装pcre
# tar zxvf pcre-8.20.tar.gz
# cd pcre-8.20
# ./configure
# make
# make install
二、安装nginx
tar zxvf ngx_cache_purge-1.4.tar.gz
# tar zxvf nginx-1.1.10.tar.gz
# cd nginx-1.1.10
# ./configure --user=www --group=www --add-module=../ngx_cache_purge-1.4 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
# make
# make install
# mkdir -p /data0/proxy_temp_path
# mkdir -p /data0/proxy_cache_path
三、配置nginx
vi /usr/local/webserver/nginx/conf/nginx.conf
user www www;
worker_processes 8;
error_log /data1/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#server_names_hash_bucket_size 128k;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
keepalive_timeout 30;
tcp_nodelay on;
proxy_temp_path /data0/proxy_temp_path;
proxy_cache_path /data0/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30G;
upstream my_server_poo {
server 202.105.182.55:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name 3.test.com;
location /
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://202.105.182.55:80/;
proxy_set_header Accept-Encoding '';
proxy_set_header X-Forwarded-For $remote_addr;
proxy_ignore_headers "Cache-Control" "Expires";
if ( $request_method = "PURGE" ) {
rewrite ^(.*)$ /purge$1 last;
}
}
location ~* \.(gif|jpeg|png|bmp|swf|js)$
{
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
error_page 405 =200 /purge$1;
}
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/20;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
access_log off;
}
}
三优化
vi /etc/sysctl.conf 添加以下:
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
#sysctl -p 让以上配置生效
启动:/usr/local/webserver/nginx/sbin/nginx