nginx 配置 模型

nginx

一个强大的web服务器和反向代理服务器!

配置

全局区:

 

#用户

user nginx ;

 

#工作进程,根据硬件调整,大于等于cpu核数

worker_processes 8;

 

#错误日志

error_log logs/nginx_error.log info;

 

#pid放置的位置

pid logs/nginx.pid;

 

#进程可以打开的最大描述符

#这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文

#件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致

worker_rlimit_nofile 204800;

 

 

events

{

         #使用epollI/O 模型

         use epoll;

 

         #工作进程的最大连接数量

         worker_connections 204800;

 

         #keepalive超时时间

         keepalive_timeout 60;

        

         #客户端请求头部的缓冲区大小

         client_header_buffer_size 4k;

 

         #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,inactive是指经过多长时间文件没被请求后删除缓存。

         open_file_cache max=65535 inactive=60s;

 

         #这个是指多长时间检查一次缓存的有效信息

         open_file_cache_valid 80s;

 

         #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被#移除。

         open_file_cache_min_uses 1;

}

 

 

http作用域:

#设定web服务器和反向代理服务器功能

 

http

{

 

#设定mime类型,类型由mime.type文件定义,配置了描述信息内容类型的国际标准。

include mime.types;

 

#设定mime的默认类型

default_type application/octet-stream;

 

#日志格式

log_format main '$host $status [$time_local] $remote_addr [$time_local] $request_uri '

 

$remote_addr$http_x_forwarded_for用以记录客户端的ip地址;

$remote_user:用来记录客户端用户名称;

$time_local:用来记录访问时间与时区;

$request:用来记录请求的urlhttp协议;

$status:用来记录请求状态;成功是200

$body_bytes_s ent:记录发送给客户端文件主体内容大小;

$http_referer:用来记录从那个页面链接访问过来的;

$http_user_agent:记录客户毒啊浏览器的相关信息;

 

#访问日志路径

access_log /usr/local/nginx/logs/access_log main;

 

#保存服务器名字的hash

server_names_hash_bucket_size 128;

 

#sendfile指令指定nginx是否调用sendfile函数来输出文件,

sendfile on;

 

#此选项允许或禁止使用sockeTCP_CORK的选项,此选项仅在使用sendfile的时候使用

tcp_nopush on;

 

#设定通过nginx上传文件的最大大小

client_max_body_size 300m;

 

#设置客户端body缓冲区大小

client_body_buffer_size 512k;

 

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

proxy_connect_timeout 90;

 

#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说#是后端服务器处理请求的时间)

proxy_read_timeout 180;

 

#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_send_timeout 180;

 

#临时文件存储路径

proxy_temp_path /data0/proxy_temp_dir;

 

#设置web缓存空间大小为200MB1天没有被访问的内容自动清除,硬盘缓存空间大小为#30GB

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

 

#压缩配置

gzip  on;

gzip_min_length  1000;

gzip_buffers     4 8k;

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

 

#负载均衡配置

 

upstream img_relay {

server 127.0.0.1:8027;

server 127.0.0.1:8028;

server 127.0.0.1:8029;

hash $request_uri;

}

 

nginxupstream目前支持5种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器

2weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

例如:

upstream bakend {

server 192.168.0.14 weight=10;

server 192.168.0.15 weight=10;

}

3ip_hash

每个请求按访问iphash结果分配,这样每个访客固定访问一个后端服务器

例如:

upstream bakend {

ip_hash;

server 192.168.0.14:88;

server 192.168.0.15:80;

}

4fair

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

upstream backend {

server 192.168.0.14:88;

server 192.168.0.15:80;

fair;

}

 

5url_hash(第三方)

按访问urlhash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

upstream backend {

server squid1:3128;

server squid2:3128;

hash $request_uri;

hash_method crc32;

}

 

#定义负载均衡设备的Ip及设备状态

upstream bakend{

ip_hash;

server 127.0.0.1:9090 down;

server 127.0.0.1:8080 weight=2;

server 127.0.0.1:6060;

server 127.0.0.1:7070 backup;

}

 

每个设备的状态设置为:

1.down表示单前的server暂时不参与负载

2.weight默认为1.weight越大,负载的权重就越大。

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

4.fail_timeout:max_fails次失败后,暂停的时间。

5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

 

nginx支持同时设置多组的负载均衡,用来给不同的server来使用。

 

server作用域:

server

{

#配置监听端口

listen 80;

 

#配置访问域名

server_name ebp.renren.com;

 

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

 

location /ebp/ {

                fastcgi_connect_timeout 5;

                fastcgi_read_timeout 5;

                 fastcgi_send_timeout 5;

       log_format ebp '$real_addr $cookie_id [$time_local] $upstream_response_time "$request" $status $body_bytes_sent '

        '"$http_referer" "$http_user_agent"';

 

                access_log  logs/ad_ebp.log ebp;

                fastcgi_pass bakend;

                include      fastcgi_params;

                expires       -1;

}

}

Nginx的过滤功能配置

~  为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配

 

例子:

location / {

    if ($http_user_agent ~ MSIE) {

           return 503;

}

}

 

location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$) {

    if ($http_referer ~*hecks.tk) {

           rewrite ^/http://www/hecks.tk/403.html

}

}

 

if ($request_url ~* (.*)(insert|select|delete|update|count|)(.*)$) {

    return 404;

}

 

If($remote_addr = 192.168.0.83) {

    Return 404;

}

 

 

内置变量:

 

例如$http_user_agent,$http_cookie等等。

此外还有其它的一些变量

 

$args此变量与请求行中的参数相等

$content_length等于请求行的“Content_Length”的值。

$content_type等同与请求头部的”Content_Type”的值

$document_root等同于当前请求的root指令指定的值

$document_uri$uri一样

$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样

$limit_rate允许限制的连接速率

$request_method等同于requestmethod,通常是“GET”或“POST

$remote_addr客户端ip

$remote_port客户端port

$remote_user等同于用户名,由ngx_http_auth_basic_module认证

$request_filename当前请求的文件的路径名,由rootaliasURI request组合而成

$request_body_file

$request_uri含有参数的完整的初始URI

$query_string$args一样

$sheeme http模式(http,https)尽在要求是评估例如

$server_protocol等同于request的协议,使用“HTTP/或“HTTP/

$server_addr request到达的serverip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。

$server_name请求到达的服务器名

$server_port请求到达的服务器的端口号

$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index

 

./nginx 启动

./nginx -s reload 重启

./nginx -s stop 停止nginx



Nginx通信模型

nginx 配置 模型

负载均衡容错模型

nginx 配置 模型

转载于:https://my.oschina.net/hejiula/blog/123935