nginx负载均衡初探(三) Nginx详细配置参数

 

nginx实现像apache一样的目录列表功能

 

nginx 目录列表配置:

 

location / {

 autoindex on;

}

 

详细配置参数如下所示:

 

 

user www        www;

#启动用户

#user  nobody;

worker_processes  48;

#进程数量,启动进程,通常设置成和cpu的数量相等

error_log  logs/error.log crit;

#日志路径

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

#启动pid存储文件

worker_rlimit_nofile 65536;

#允许用户打开的最大句柄

 

#工作模式及连接数上限

events {

use epoll;

#查询模式

#epoll是多路复用IO(I/O Multiplexing)中的一种方式,

#仅用于linux2.6以上内核,可以大大提高nginx的性能,单个后台worker process进程的最大并发链接数

worker_connections  65536;

#最大句柄,允许连接的最大文件数量

# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 
# 为什么上面反向代理要除以4,应该说是一个经验值
# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:
#4 * 8000 = 32000# worker_connections 值的设置跟物理内存大小有关
# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的
#文件数大约是10万左右# 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
# $ cat /proc/sys/fs/file-max# 输出 34336# 32000 < 34336,即并发连接总数小于
#系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的
#最大文件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目# 其实质也就是根据主机的
#物理CPU和内存进行配置# 当然,理论上的并发总数可能会和实际有所偏差,因为主机
#还有其他的工作进程需要消耗系统资源。
# ulimit -SHn 65535

}

http {

#设定mime类型,类型由mime.type文件定义

    include       mime.types;

    default_type  application/octet-stream;

    charset  UTF-8;

server_names_hash_bucket_size 128;

#根据cpu一级缓存设置 ubuntu可以使用lscpu查看

    client_header_buffer_size 4;

    large_client_header_buffers 4 8k;

#设定日志格式

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,#对于普通应用,必须设为 on,#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,#以平衡磁盘与网络I/O处理速度,降低系统的uptime.

sendfile        on;

#指定nginx是否调用sendfile函数(zero copy方式)来输出文件,普通应用必须设为on,对于普通文件用on。如果进行下载I/O负载应用,设置为off,以平衡磁盘磁盘和网络I/O处理速度。

#tcp_nopush     on;

#允许或禁止使用socket的TCP_NOPUSH仅当sendfile 为on时设置改选项生效

keepalive_timeout  65;

#超时时间

    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;

    #keepalive_timeout  0;

#开启gzip压缩

gzip  on;

#允许压缩传输文件

gzip_min_length 1k;

#设置允许压缩最小字节数,0表示多大都压,最好设置大于1k,小于1k越压越大

    gzip_buffers 4 16k;

gzip_http_version 1.1;

#判断http协议版本,是否支持压缩,否则用户看到乱码。默认即可

gzip_comp_level 2;

#gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢

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

gzip_vary on;

#加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

#设定请求缓冲

client_max_body_size 50m;

#允许客户端请求的最大单个文件字节数

client_body_buffer_size 128k;

#缓冲区代理用户端请求的最大字节数,可以理解先保存到本地在发送给用户

proxy_connect_timeout 600;

#后端服务器连接的超时时间,发起握手等候相应超时时间

proxy_read_timeout 600;

连接成功后,等待后端服务器相应时间,已经进入后端的排队之中等候处理

proxy_send_timeout 600;

#后端服务器数据回传时间,在规定时间内服务器必须传完所有数据

proxy_buffer_size 16k;

#保存用户头信息供nginx进行规则处理,用于缓存代理请求

proxy_buffers    4 32k;

#nginx保存每个用户Buffer最大空间

proxy_busy_buffers_size 64k;

#如果系统很忙可以申请更大的proxy_buffers,官方推荐 *2

    proxy_temp_file_write_size 64k;

#设定虚拟主机配置

upstream fengzhanhai{

#负载均衡池

    #    ip_hash;

        server 10.0.211.2 down weight=4 max_fails=2 fail_timeout=30s;

        server 10.0.211.2:8080 weight=4 max_fails=2 fail_timeout=30s;

       # server 10.0.211.3:8080 weight=4 max_fails=2 fail_timeout=30s;

       # server 10.0.211.4:8080 weight=4 max_fails=2 fail_timeout=30s;

       # server 192.168.202.81:8080 weight=2 max_fails=2 fail_timeout=30s;

        server 192.168.202.82 weight=4  max_fails=2 fail_timeout=30s;

   }

server {

#虚拟主机反向代理负载均衡池fengzhanhai的主机应用

        listen       80;

        server_name  www.123.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

#当分发的后端业务宕机后无缝切换到pool中存活的业务之上

            proxy_pass http://fengzhanhai;

#反向代理资源池

            proxy_set_header Host www.123.com;

            proxy_set_header X-Forwarded-For  $remote_addr;

            root   html;

            index  index.html index.htm;

        }

        access_log  logs/feng.log  combined;

#访问日志

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #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;

        #}

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

}

 

 

4、          测试Nginx配置语法是否正常

5、          ./nginx –t

 

返回如下信息表示参数配置无误

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

 

6、          启动nginx进行测试./nginx

 

7、          Nginx负载均衡知识拓展

proxy_set_header  Host  $host :首先说明 proxy_set_header 指令在向反向代理的后端Web服务器发起请求时添加指定的 Header头信息,后端web服务器有多个基于域名的虚拟主机时,通过头信息Host,用于指定请求的域名,这样后端web才能识别反向代理请求哪个虚拟主机处理。

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for :在nginx反向代理添加Header头信息 X-Forwarded-For在配合后端服务器日志文件的$http_x_Forwarded_for这条就可以获得用户的IP地址

 

 

8、          nginx支持的几种算法分析

1、 轮询 每个请求按时间顺序分配到不同的后端服务器了,后端服务器down掉,自动切除;

2、weight  设定服务器权值: 如weight=2    服务器性能不均时候使用 。weight: 默认为1,weight越大,负载的权重越大;

3、 ip_hash 每个请求按访问ip的hash结果分配,每个访客有固定的后端服务器,可以解决session问题;

4、 fair(第三方) 按后端服务器的响应时间来分配,响应时间短的优先分配

5、url_hash (第三方) 按访问的url的hash结果分配,使每个url定向到同一个后端服务器,后端为缓存服务器比较有效。

 

 

详细参数介绍:

 

1)down : 当前的IP server暂时不参与负载,不进行反向代理;

2)max_fails: 允许请求失败的次数默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误;

3)fail_timeout : max_fails次失败后,暂停的时间;

4)backup:  其它所有非backup机器down或者忙时候,请求backup机器,这台机器压力最轻。

猜你喜欢

转载自starbhhc.iteye.com/blog/2030999