[javascript] view plaincopy

  1. #用户 用户组  

  2. user www www;  

  3. #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点  

  4. worker_processes 5;  

  5. #错误日志  

  6. error_log logs/error.log;  

  7. #pid文件位置  

  8. pid logs/nginx.pid;  

  9. worker_rlimit_nofile 8192;  

  10.   

  11. events {  

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

  13. worker_connections 4096;  

  14. }  

  15.   

  16. http {  

  17. include conf/mime.types;  

  18. #反向代理配置,可以打开proxy.conf看看  

  19. include /etc/nginx/proxy.conf;  

  20. #fastcgi配置,可以打开fastcgi.conf看看  

  21. include /etc/nginx/fastcgi.conf;  

  22.   

  23. default_type application/octet-stream;  

  24. #日志的格式  

  25. log_format main '$remote_addr - $remote_user [$time_local] $status '  

  26. '"$request" $body_bytes_sent "$http_referer" '  

  27. '"$http_user_agent" "$http_x_forwarded_for"';  

  28. #访问日志  

  29. access_log logs/access.log main;  

  30. sendfile on;  

  31. tcp_nopush on;  

  32. #根据实际情况调整,如果server很多,就调大一点  

  33. server_names_hash_bucket_size 128; # this seems to be required for some vhosts  

  34.  

  35. #这个例子是fastcgi的例子,如果用fastcgi就要仔细看  

  36. server { # php/fastcgi  

  37. listen 80;  

  38. #域名,可以有多个  

  39. server_name domain1.com www.domain1.com;  

  40. #访问日志,和上面的级别不一样,应该是下级的覆盖上级的  

  41. access_log logs/domain1.access.log main;  

  42. root html;  

  43.   

  44. location / {  

  45. index index.html index.htm index.php;  

  46. }  

  47.  

  48. #所有php后缀的,都通过fastcgi发送到1025端口上  

  49. #上面include的fastcgi.conf在此应该是有作用,如果你不include,那么就把fastcgi.conf的配置项放在这个下面。  

  50. location ~ .php$ {  

  51. fastcgi_pass 127.0.0.1:1025;  

  52. }  

  53. }  

  54.  

  55. #这个是反向代理的例子  

  56. server { # simple reverse-proxy  

  57. listen 80;  

  58. server_name domain2.com www.domain2.com;  

  59. access_log logs/domain2.access.log main;  

  60.  

  61. #静态文件,nginx自己处理  

  62. location ~ ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {  

  63. root /var/www/virtual/big.server.com/htdocs;  

  64. #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。  

  65. expires 30d;  

  66. }  

  67.  

  68. #把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。  

  69. location / {  

  70. proxy_pass http://127.0.0.1:8080;  

  71. }  

  72. }  

  73.  

  74. #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。据说nginx可以根据后台响应时间调整。后台需要多个web服务器。  

  75. upstream big_server_com {  

  76. server 127.0.0.3:8000 weight=5;  

  77. server 127.0.0.3:8001 weight=5;  

  78. server 192.168.0.1:8000;  

  79. server 192.168.0.1:8001;  

  80. }  

  81.   

  82. server {  

  83. listen 80;  

  84. server_name big.server.com;  

  85. access_log logs/big.server.access.log main;  

  86.   

  87. location / {  

  88. proxy_pass http://big_server_com;  

  89. }  

  90. }  

  91. }   

  92.   

  93.   

  94.   

  95.     user    www www;           #运行NGINX所使用的用户和组  

  96.   worker_processes     4;    #nginx进程数,建议按照cpu数目来指定,一般为它的倍数,每个进程消耗约10M内存  

  97.   error_log       /data/logs/nginx/error.log  crit;  

  98.   pid             /elain/apps/nginx/nginx.pid;  

  99.   worker_rlimit_nofile  65535;   #nginx能打开文件的最大句柄数,最好与ulimit -n的值保持一致,使用ulimit -SHn 65535 设置  

  100.   events {  

  101.   use epoll;          #使用epoll的I/O模型  

  102.   connections 20000;  #每个进程允许的最多连接数  

  103.   worker_connections 65535;   #该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置  

  104.   maxclients=65535*2  

  105.   }  

  106.   http {  

  107.   include mime.types;           #mine.types内定义各文件类型映像  

  108.   types {  

  109.   text/html  html;  

  110.   p_w_picpath/gif  gif;  

  111.   p_w_picpath/jpeg jpg;  

  112.   p_w_picpath/png  png;  

  113.   }  

  114.   default_type application/octet-stream;  #设置默认类型是二进制流,若未设置时,比如未加载PHP时,是不予解析,用浏览器访问则出现下载窗口  

  115.   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的值。  

  116.   client_header_buffer_size 128k;    #客户端请求头部的缓冲区大小,根据系统分页大小设置,分页大小可用命令getconf PAGESIZE取得  

  117.   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的字节数上限会增大。  

  118.   client_max_body_size 8m;   #HTTP请求的BODY最大限制值,若超出此值,报413 Request Entity Too Large  

  119.   open_file_cache max=65535 inactive=20s;  #max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。  

  120.   open_file_cache_valid 30s;  #指多长时间检查一次缓存的有效信息  

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

  122.   server_tokens off;          #关闭错误时Nginx版本显示  

  123.   #提高文件传输性能  

  124.   sendfile on;                #打开系统函数sendfile()支持  

  125.   tcp_nopush on;              #打开linux下TCP_CORK,sendfile打开时才有效,作减少报文段的数量之用  

  126.   keepalive_timeout 60;       #keepalive超时时间  

  127.   tcp_nodelay on;             #打开TCP_NODELAY在包含了keepalive才有效  

  128.   fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间  

  129.   fastcgi_send_timeout 300;    #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。  

  130.   fastcgi_read_timeout 300;    #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。  

  131.   fastcgi_buffer_size 64k;     #这里可以设置为fastcgi_buffers指令指定的缓冲区大小  

  132.   fastcgi_buffers 16 16k;      #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答  

  133.   fastcgi_busy_buffers_size 128k;  #建议为fastcgi_buffers的两倍  

  134.   fastcgi_temp_file_write_size 128k;   #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,设置上述数值设置太小时若负载上来时可能报 502 Bad Gateway  

  135.   fastcgi_cache dingtm     #开启FastCGI缓存并且为其制定一个名称,有效降低CPU负载,并且防止502错误  

  136.   fastcgi_cache_valid 200 302 1h;  #指定应答代码缓存时间为1小时  

  137.   fastcgi_cache_valid 301 1d;      #1天  

  138.   fastcgi_cache_valid any 1m;      #其它为1分钟  

  139.   fastcgi_cache_min_uses 1;        #缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数                f  

  140.   gzip on;                    #打开GZIP压缩,实时压缩输出数据流  

  141.   gzip_min_length  1k;        #从Content-Length中数值获取验证,小于1K会越压越大  

  142.   gzip_buffers  4 16k;        #以16K为单位4倍的申请内存做压缩结果流缓存  

  143.   gzip_http_version 1.1;  

  144.   gzip_comp_level 3;          #压缩比率1-9,1压缩比最小处理速度最快,9压缩比最大但处理最慢且耗CPU  

  145.   gzip_types      text/plain application/x-javascript text/css application/xml;  #压缩类型  

  146.   include   vhosts/*.conf;      #虚拟主机  

  147.   }  

  148.   #虚拟主机  

  149.   server {  

  150.   listen 80;  

  151.   server_name  <a href="http://www.elain.org/">www.elain.org</a>;     #多域名用空格隔开  

  152.   index index.php index.html index.shtml;  

  153.   root  /elain/data/htdocs/elain;  

  154.   #limit_conn connlimit 20;     #限制一个IP只能最多只能发起20个连接,超过报 503 Service unavailable,可防止恶意连接  

  155.   access_log /elain/logs/nginx/access_www.elain.org.log access;  

  156.   error_log  /elain/logs/nginx/error_www.elain.org.log;  

  157.   location / {  

  158.   ssi on;                 #WEB文档根目录打开SSI支持  

  159.   ssi_types text/html;  

  160.   ssi_silent_errors off;  #处理SSI出错时不提示  

  161.   }  

  162.   location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {  

  163.   access_log    off;  

  164.   expires       30d;  

  165.   }  

  166.   location ~ .*.(js|css)?$ {  

  167.   expires      1h;  

  168.   add_header Cache_Control private;  

  169.   }  

  170.   location ~ /.ht {  

  171.   deny all;  

  172.   }  

  173.   location /NginxStatus {           #设定查看Nginx状态的地址  

  174.   stub_status on;  

  175.   access_log off;  

  176.   auth_basic “NginxStatus”;     #标识  

  177.   auth_basic_user_file conf/.htpasswd;   #网页加密,提示登录框,输入用户名和密码可查看  

  178.   }  

  179.   location ~ .*.(php|php5)?$ {                           #匹配文件后缀php, php5  

  180.   #fastcgi_pass  unix:/tmp/php-cgi.sock;  #SOCKET方式转交fastcgi处理  

  181.   fastcgi_pass  127.0.0.1:9000;           #9000端口方式fastcgi  

  182.   fastcgi_index index.php;  

  183.   include fastcgi_params;                 #包含fastcgi配置  

  184.   #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  

  185.   }  

  186.   }