SZStudy 2012-02-24
#——————————————– #运行nginx所在的用户名和用户组 user nobody nobody; #启动进程数 worker_processes 2; worker_cpu_affinity 0010 0001 ; #worker_cpu_affinity 0001 0100 1000 0010 0001 0100 1000 0010; #全局错误日志及PID文件 error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 65535; #工作模式及连接数上限 events { use epoll; worker_connections 65535; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http{ include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; #设定请求缓冲 client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; 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_temp_file_write_size 128k; #开启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; gzip_vary on; #设定负载均衡列表 upstream backend { #down 表示单前的server暂时不参与负载 #weigth参数表示权值,权值越高被分配到的几率越大 #server 192.168.3.69:80 weight=1; #max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 #fail_timeout:max_fails次失败后,暂停的时间。 #backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。 server 172.16.50.147:8081; server 172.16.50.147:8082; server 172.16.50.147:8083; server 172.16.50.147:8084; } #禁止通过ip访问站点 #server{ #server_name _; #return 404; #} #设定虚拟主机 server { listen 80; server_name localhost; #对 / 所有做负载均衡 (本机nginx采用完全转发,所有请求都转发到后端的tomcat集群) location / { #设定网站的资源存放路径 root /var/www ; #设定访问的默认首页地址 index index.jsp index.htm index.html; #proxy_pass http://backend ; #保留用户真实信息 include proxy.conf; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_cache cache; #proxy_store on; proxy_temp_path /root/cache; proxy_cache_valid 200 302 24h;#200和302状态码保存1小时 proxy_cache_valid 301 1d;#301状态码保存一天 proxy_cache_valid any 10h;#其它的保存一分钟 if ( !-f \$request_filename) { proxy_pass http://backend; } } #状态监控部分 location /nginx { stub_status on; access_log on; auth_basic “NginxStatus”; auth_basic_user_file /usr/local/nginx/htpasswd; #允许访问的ip allow 127.0.0.1; } #定义访问日志的写入格式 log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” $http_x_forwarded_for’; #设定访问日志的存放路径 #access_log /usr/local/nginx/logs/access.log access; #设定access log access_log logs/access.log access; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 65; (这个参数如果启用,会出现未知错误,因此暂时取消) } }