nginx服务二---nginx配置文件解释

来源:互联网 发布:阮玲玉神女知乎 编辑:程序博客网 时间:2024/06/06 02:03
一、http配置文件
user www www;             # 定义Nginx运行的用户和用户组
worker_processes 8;                  # nginx进程数,建议设置为等于CPU总核心数。
error_log  /usr/local/nginx/logs/error.log  info;                      # 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
pid /usr/local/nginx/logs/nginx.pid;                     # 进程pid文件

worker_rlimit_nofile 65535;   # 指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit  -n)一致
events
{
use epoll;
worker_connections 65535;     # 单个进程最大连接数(最大连接数=连接数*进程数)
keepalive_timeout 60;           # keepalive超时时间。
client_header_buffer_size 4k;
open_file_cache max=65535 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 1;
open_file_cache_errors on;
}
http          # 设定http服务器,利用它的反向代理功能提供负载均衡支持
{
include mime.types;             # 文件扩展名与文件类型映射表
default_type application/octet-stream;        # 默认文件类型

charset utf-8;              # 默认编码

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
client_max_body_size 8m;     # 设定通过nginx上传文件的大小
sendfile on;

autoindex on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 120;    # 长连接超时时间,单位是秒

# FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。

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压缩输出
gzip_min_length 1k;               # 最小压缩文件大小
gzip_buffers 4 16k;             # 压缩缓冲区
gzip_http_version 1.0;             # 压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2;               # 压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; # 压缩类型,默认就已经包含textml,所以下面就不用再写了。

gzip_vary on;

limit_zone crawler $binary_remote_addr 10m;   # 开启限制IP连接数的时候需要使用

# 负载均衡配置
upstream piao.jd.com {                           # upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
#################################nginx的upstream目前支持4种方式的分配#################################################################
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}

3、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。例如:

upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;

server server2;

fair;

}
4、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
###########################################nginx的upstream目前支持4种方式的分配#######################################################
upstream bakend {     #定义负载均衡设备的Ip及设备状态
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;
}
在需要使用负载均衡的server中增加 proxy_pass http://bakend/;  
每个设备的状态设置为:
1.down表示单前的server暂时不参与负载
2.weight为weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path设置记录文件的目录 可以设置最多3层目录
location对URL进行匹配.可以进行重定向或者进行新的代理负载均衡
}
二、server配置文件
虚拟主机的配置
server
{
listen 80;
server_name www.jd.com jd.com;     # 域名可以有多个,用空格隔开
index index.html index.htm index.php;
root   /data/www/jd;
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;          # 图片缓存时间设置
}
location ~ .*.(js|css)?$
{
expires 1h;        # JS和CSS缓存时间设置
}
##日志格式设定
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
# 通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
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/host.access.log   main;
access_log   /usr/local/nginx/logs/host.access.404.log log404;
# 对 "/" 启用反向代理              
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;          #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 以下是一些反向代理的配置,可选。
proxy_set_header Host $host;   
client_max_body_size 10m;      # 允许客户端请求的最大单文件字节数
client_body_buffer_size 128k;    # 缓冲区代理缓冲用户端请求的最大字节数,默认即可
proxy_intercept_errors on;     # 表示使nginx阻止HTTP应答代码为400或者更高的应答。
proxy_connect_timeout 90;    # 后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_send_timeout 90;   # 后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90;    # 连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k;   设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;# 高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;      # 设置在写入proxy_temp_path时数据的大小
}
# 设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;     # htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}
# 本地动静分离反向代理配置,所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
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_pass http://127.0.0.1:8080;
}
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$     #所有静态文件由nginx直接读取不经过tomcat或resin
{
expires 15d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
原创粉丝点击