LOADING

nginx.conf配置范例模板

 

 

 

http://www.verydemo.com/demo_c372_i8556.html

#用户 用户组
user www www;
#工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点
worker_processes 5;
#错误日志
error_log logs/error.log;
#pid文件位置
pid logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
worker_connections 4096;
}

http {
include conf/mime.types;
#反向代理配置,可以打开proxy.conf看看
include /etc/nginx/proxy.conf;
#fastcgi配置,可以打开fastcgi.conf看看
include /etc/nginx/fastcgi.conf;

default_type application/octet-stream;
#日志的格式
log_format main ‘$remote_addr – $remote_user [$time_local] $status ‘
‘”$request” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
#访问日志
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#根据实际情况调整,如果server很多,就调大一点
server_names_hash_bucket_size 128; # this seems to be required for some vhosts

#这个例子是fastcgi的例子,如果用fastcgi就要仔细看
server { # php/fastcgi
listen 80;
#域名,可以有多个
server_name domain1.com www.domain1.com;
#访问日志,和上面的级别不一样,应该是下级的覆盖上级的
access_log logs/domain1.access.log main;
root html;

location / {
index index.html index.htm index.php;
}

#所有php后缀的,都通过fastcgi发送到1025端口上
#上面include的fastcgi.conf在此应该是有作用,如果你不include,那么就把fastcgi.conf的配置项放在这个下面。
location ~ .php$ {
fastcgi_pass 127.0.0.1:1025;
}
}

#这个是反向代理的例子
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;

#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}

#把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。
location / {
proxy_pass http://127.0.0.1:8080;
}
}

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。据说nginx可以根据后台响应时间调整。后台需要多个web服务器。
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}

server {
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;

location / {
proxy_pass http://big_server_com;
}
}
}

user www www; #运行NGINX所使用的用户和组
  worker_processes 4; #nginx进程数,建议按照cpu数目来指定,一般为它的倍数,每个进程消耗约10M内存
  error_log /data/logs/nginx/error.log crit;
  pid /elain/apps/nginx/nginx.pid;
  worker_rlimit_nofile 65535; #nginx能打开文件的最大句柄数,最好与ulimit -n的值保持一致,使用ulimit -SHn 65535 设置
  events {
  use epoll; #使用epoll的I/O模型
  connections 20000; #每个进程允许的最多连接数
  worker_connections 65535; #该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置
  maxclients=65535*2
  }
  http {
  include mime.types; #mine.types内定义各文件类型映像
  types {
  text/html html;
  image/gif gif;
  image/jpeg jpg;
  image/png png;
  }
  default_type application/octet-stream; #设置默认类型是二进制流,若未设置时,比如未加载PHP时,是不予解析,用浏览器访问则出现下载窗口
  server_names_hash_bucket_size 128; #不能带单位!配置个主机时必须设置该值,否则无法运行Nginx或测试时不通过,该设置与server_names_hash_max_size 共同控制保存服务器名的HASH表,hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。若hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。若报出hash max size 或 hash bucket size的提示,则我们需要增加server_names_hash_max_size的值。
  client_header_buffer_size 128k; #客户端请求头部的缓冲区大小,根据系统分页大小设置,分页大小可用命令getconf PAGESIZE取得
  large_client_header_buffers 4 128k; #4为个数,128k为大小,默认是4k。申请4个128k。当http 的URI太长或者request header过大时会报414 Request URI too large或400 bad request,这是很有可能是cookie中写入的值太大造成的,因为header中的其他参数的size一般比较固定,只有cookie可能被写入较 大的数据,这时可以调大上述两个值,相应的浏览器中cookie的字节数上限会增大。
  client_max_body_size 8m; #HTTP请求的BODY最大限制值,若超出此值,报413 Request Entity Too Large
  open_file_cache max=65535 inactive=20s; #max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
  open_file_cache_valid 30s; #指多长时间检查一次缓存的有效信息
  open_file_cache_min_uses 1; #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例, 如果有一个文件在inactive时间内一次没被使用,它将被移除。
  server_tokens off; #关闭错误时Nginx版本显示
  #提高文件传输性能
  sendfile on; #打开系统函数sendfile()支持
  tcp_nopush on; #打开linux下TCP_CORK,sendfile打开时才有效,作减少报文段的数量之用
  keepalive_timeout 60; #keepalive超时时间
  tcp_nodelay on; #打开TCP_NODELAY在包含了keepalive才有效
  fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间
  fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
  fastcgi_read_timeout 300; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
  fastcgi_buffer_size 64k; #这里可以设置为fastcgi_buffers指令指定的缓冲区大小
  fastcgi_buffers 16 16k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答
  fastcgi_busy_buffers_size 128k; #建议为fastcgi_buffers的两倍
  fastcgi_temp_file_write_size 128k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,设置上述数值设置太小时若负载上来时可能报 502 Bad Gateway
  fastcgi_cache dingtm #开启FastCGI缓存并且为其制定一个名称,有效降低CPU负载,并且防止502错误
  fastcgi_cache_valid 200 302 1h; #指定应答代码缓存时间为1小时
  fastcgi_cache_valid 301 1d; #1天
  fastcgi_cache_valid any 1m; #其它为1分钟
  fastcgi_cache_min_uses 1; #缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数 f
  gzip on; #打开GZIP压缩,实时压缩输出数据流
  gzip_min_length 1k; #从Content-Length中数值获取验证,小于1K会越压越大
  gzip_buffers 4 16k; #以16K为单位4倍的申请内存做压缩结果流缓存
  gzip_http_version 1.1;
  gzip_comp_level 3; #压缩比率1-9,1压缩比最小处理速度最快,9压缩比最大但处理最慢且耗CPU
  gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型
  include vhosts/*.conf; #虚拟主机
  }
  #虚拟主机
  server {
  listen 80;
  server_name www.elain.org; #多域名用空格隔开
  index index.php index.html index.shtml;
  root /elain/data/htdocs/elain;
  #limit_conn connlimit 20; #限制一个IP只能最多只能发起20个连接,超过报 503 Service unavailable,可防止恶意连接
  access_log /elain/logs/nginx/access_www.elain.org.log access;
  error_log /elain/logs/nginx/error_www.elain.org.log;
  location / {
  ssi on; #WEB文档根目录打开SSI支持
  ssi_types text/html;
  ssi_silent_errors off; #处理SSI出错时不提示
  }
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
  access_log off;
  expires 30d;
  }
  location ~ .*.(js|css)?$ {
  expires 1h;
  add_header Cache_Control private;
  }
  location ~ /.ht {
  deny all;
  }
  location /NginxStatus { #设定查看Nginx状态的地址
  stub_status on;
  access_log off;
  auth_basic “NginxStatus”; #标识
  auth_basic_user_file conf/.htpasswd; #网页加密,提示登录框,输入用户名和密码可查看
  }
  location ~ .*.(php|php5)?$ { #匹配文件后缀php, php5
  #fastcgi_pass unix:/tmp/php-cgi.sock; #SOCKET方式转交fastcgi处理
  fastcgi_pass 127.0.0.1:9000; #9000端口方式fastcgi
  fastcgi_index index.php;
  include fastcgi_params; #包含fastcgi配置
  #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
  }

 

 

 

http://ajava.org/blog-603-826.html

source:http://www.2cto.com/os/201107/97721.html

# Info   : The conf for nginx
# Author : dingtm
# CTime  : 2010.07.01
user    www www;           #运行NGINX所使用的用户和组
worker_processes     4;    #nginx进程数,建议按照cpu数目来指定,一般为它的倍数,每个进程消耗约10M内存
error_log       /data/logs/nginx/error.log  crit;
pid             /elain/apps/nginx/nginx.pid;
worker_rlimit_nofile  65535;   #nginx能打开文件的最大句柄数,最好与ulimit -n的值保持一致,使用ulimit -SHn 65535 设置
events {
use epoll;          #使用epoll的I/O模型
connections 20000;  #每个进程允许的最多连接数
worker_connections 65535;   #该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置
maxclients=65535*2
}
http {
include mime.types;           #mine.types内定义各文件类型映像
types {
text/html  html;
image/gif  gif;
image/jpeg jpg;
image/png  png;
}
default_type application/octet-stream;  #设置默认类型是二进制流,若未设置时,比如未加载PHP时,是不予解析,用浏览器访问则出现下载窗口
server_names_hash_bucket_size 128;    #不能带单位!配置个主机时必须设置该值,否则无法运行Nginx或测试时不通过,该设置与server_names_hash_max_size 共同控制保存服务器名的HASH表,hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。若hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。若报出hash max size 或 hash bucket size的提示,则我们需要增加server_names_hash_max_size的值。
client_header_buffer_size 128k;    #客户端请求头部的缓冲区大小,根据系统分页大小设置,分页大小可用命令getconf PAGESIZE取得
large_client_header_buffers 4 128k;  #4为个数,128k为大小,默认是4k。申请4个128k。当http 的URI太长或者request header过大时会报414 Request URI too large或400 bad request,这是很有可能是cookie中写入的值太大造成的,因为header中的其他参数的size一般比较固定,只有cookie可能被写入较 大的数据,这时可以调大上述两个值,相应的浏览器中cookie的字节数上限会增大。
client_max_body_size 8m;   #HTTP请求的BODY最大限制值,若超出此值,报413 Request Entity Too Large
open_file_cache max=65535 inactive=20s;  #max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 30s;  #指多长时间检查一次缓存的有效信息
open_file_cache_min_uses 1; #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例, 如果有一个文件在inactive时间内一次没被使用,它将被移除。
server_tokens off;          #关闭错误时Nginx版本显示
#提高文件传输性能
sendfile on;                #打开系统函数sendfile()支持
tcp_nopush on;              #打开linux下TCP_CORK,sendfile打开时才有效,作减少报文段的数量之用
keepalive_timeout 60;       #keepalive超时时间
tcp_nodelay on;             #打开TCP_NODELAY在包含了keepalive才有效
fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间
fastcgi_send_timeout 300;    #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout 300;    #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
fastcgi_buffer_size 64k;     #这里可以设置为fastcgi_buffers指令指定的缓冲区大小
fastcgi_buffers 16 16k;      #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答
fastcgi_busy_buffers_size 128k;  #建议为fastcgi_buffers的两倍
fastcgi_temp_file_write_size 128k;   #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,设置上述数值设置太小时若负载上来时可能报 502 Bad Gateway
fastcgi_cache dingtm     #开启FastCGI缓存并且为其制定一个名称,有效降低CPU负载,并且防止502错误
fastcgi_cache_valid 200 302 1h;  #指定应答代码缓存时间为1小时
fastcgi_cache_valid 301 1d;      #1天
fastcgi_cache_valid any 1m;      #其它为1分钟
fastcgi_cache_min_uses 1;        #缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数                f
gzip on;                    #打开GZIP压缩,实时压缩输出数据流
gzip_min_length  1k;        #从Content-Length中数值获取验证,小于1K会越压越大
gzip_buffers  4 16k;        #以16K为单位4倍的申请内存做压缩结果流缓存
gzip_http_version 1.1;
gzip_comp_level 3;          #压缩比率1-9,1压缩比最小处理速度最快,9压缩比最大但处理最慢且耗CPU
gzip_types      text/plain application/x-javascript text/css application/xml;  #压缩类型
include   vhosts/*.conf;      #虚拟主机
}
#虚拟主机
server {
listen 80;
server_name  www.elain.org;     #多域名用空格隔开
index index.php index.html index.shtml;
root  /elain/data/htdocs/elain;
#limit_conn connlimit 20;     #限制一个IP只能最多只能发起20个连接,超过报 503 Service unavailable,可防止恶意连接
access_log /elain/logs/nginx/access_www.elain.org.log access;
error_log  /elain/logs/nginx/error_www.elain.org.log;
location / {
ssi on;                 #WEB文档根目录打开SSI支持
ssi_types text/html;
ssi_silent_errors off;  #处理SSI出错时不提示
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log    off;
expires       30d;
}
location ~ .*.(js|css)?$ {
expires      1h;
add_header Cache_Control private;
}
location ~ /.ht {
deny all;
}
location /NginxStatus {           #设定查看Nginx状态的地址
stub_status on;
access_log off;
auth_basic “NginxStatus”;     #标识
auth_basic_user_file conf/.htpasswd;   #网页加密,提示登录框,输入用户名和密码可查看
}
location ~ .*.(php|php5)?$ {                           #匹配文件后缀php, php5
#fastcgi_pass  unix:/tmp/php-cgi.sock;  #SOCKET方式转交fastcgi处理
fastcgi_pass  127.0.0.1:9000;           #9000端口方式fastcgi
fastcgi_index index.php;
include fastcgi_params;                 #包含fastcgi配置
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
本文出自 “elain的技术博客” 博客

 

 

http://www.tennfy.com/?p=160

#用户 用户组
user www www;
#工作进程,根据硬件调整,几核cpu,就配几个
worker_processes 5;
#错误日志
error_log logs/error.log;
#pid文件位置
pid logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
worker_connections 4096;
}

http {
include conf/mime.types;
#反向代理配置,可以打开proxy.conf看看
include /etc/nginx/proxy.conf;
#fastcgi配置,可以打开fastcgi.conf看看
include /etc/nginx/fastcgi.conf;

default_type application/octet-stream;
#日志的格式
log_format main ‘$remote_addr – $remote_user [$time_local] $status ‘
‘”$request” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
#访问日志
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#根据实际情况调整,如果server很多,就调大一点
server_names_hash_bucket_size 128; # this seems to be required for some vhosts

#这个例子是fastcgi的例子,如果用fastcgi就要仔细看
server { # php/fastcgi
listen 80;
#域名,可以有多个
server_name domain1.com www.domain1.com;
#访问日志,和上面的级别不一样,应该是下级的覆盖上级的
access_log logs/domain1.access.log main;
root html;

location / {
index index.html index.htm index.php;
}

#所有php后缀的,都通过fastcgi发送到1025端口上
#上面include的fastcgi.conf在此应该是有作用,如果你不include,那么就把fastcgi.conf的配置项放在这个下面。
location ~ .php$ {
fastcgi_pass 127.0.0.1:1025;
}
}

#这个是反向代理的例子
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;

#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}

#把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。
location / {
proxy_pass http://127.0.0.1:8080;
}
}

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。据说nginx可以根据后台响应时间调整。后台需要多个web服务器。
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}

server {
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;

location / {
proxy_pass http://big_server_com;
}
}
}

主要内容转于:http://www.inginx.com/nginx-conf-in/

 

 

 

原文链接:https://www.cnblogs.com/taosim/articles/3107934.html
本文来源 互联网收集,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源,如您发现有涉嫌抄袭侵权的内容,请联系本站核实处理。

© 版权声明

相关文章