2014-04-14 13:57:01
来 源
ITJS.CN
Nginx
本文介绍Nginx配置参数详解,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
#http://wiki.nginx.org/Chs中文wiki

#使用用户和用户组 user www www;

#user  nobody;

#指定工作进程数,一般根据cpu核数决定,如2核的值为2

worker_processes  2;

#指定错误日志路径,debug,info,notice,warn,error,crit

#crit级别最高,只记录非常严重的错误。

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

error_log  logs/error.log  info;

pid        logs/nginx.pid;

#指定文件描述符数量

#每个nginx进程打开文件描述符最大数目 配置要和系统的单进程打开文件数一

#致,linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应

#应该填写65535 nginx调度时分配请求到进程并不是那么的均衡,假如超过会返回502错误。我

#这里写的大一点

worker_rlimit_nofile 51200;

events {

#使用的网络i/o模型,linux为epoll,FreeBSD为kqueue

use epoll;

#允许连接数

worker_connections  51200;

}

http {

#设定mime类型

include       mime.types;

default_type  application/octet-stream;     charset  utf-8;

#设定日志格式

#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()函数比read(2)和write(2)函数更加高效

#后者需要从用户空间中传送文件。有时候会影响性能

sendfile        on;

#这个指令指定是否使用socket的TCP_NOPUSH(FreeBSD)或TCP_CORK(linux)选项,这个选项只在使用sendfile时有效。

#设置这个选项的将导致nginx试图将它的HTTP应答头封装到一个包中。

#tcp_nopush     on;     #参数的第一个值指定了客户端与服务器长连接的超时时间,超过这个时间,服务器将关闭连接。

#参数的第二个值(可选)指定了应答头中Keep-Alive: timeout=time的time值,这个值可以使一些浏览器知道什么时候关闭连接,以便服务器不用重复关闭,如果不指定这个参数,nginx不会在应答头中发送Keep-Alive信息。(但这并不是指怎样将一个连接“Keep-Alive”)

#参数的这两个值可以不相同。

#keepalive_timeout  0 70;

keepalive_timeout  65;

#fastcgi用于php

#这个参数指定将用多大的缓冲区来读取从FastCGI进程到来应答头。

#默认的缓冲区大小为fastcgi_buffers指令中的每块大小,可以将这个值设置更小。

#fastcgi_buffer_size 64k;

#fastcgi_buffers 4 64k;

#指定是否启用gzip压缩。

#gzip  on;

#指定缓存压缩应答的缓冲区数量和大小

#gzip_buffers 4 16K;

#指定压缩等级,其值从1到9,1为最小化压缩(处理速度快),9为最大化压缩(处理速度慢)。

#gzip_comp_level 3;

    #使用正则表达式来指定某些不需要gzip压缩的浏览器(将和User-Agents进行匹配)。依赖于PCRE库

#0.7.63版本以后,你可以为IE5.5和IE6 SP1使用msie6参数来禁止gzip压缩。

#gzip_disable  "msie6";

    #设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。

#默认值是0,不管页面多大都压缩。

#建议设置成大于1k的字节数,小于1k可能会越压越大。

#gzip_min_length 1;

    #Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。

#off - 关闭所有的代理结果数据的压缩

#expired - 启用压缩,如果header头中包含 "Expires" 头信息

#no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息

#no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息

#private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息

#no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息

#no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息

#auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息

#any - 无条件启用压缩

#gzip_proxied

#匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的。

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

#启用应答头“Vary: Accept-Encoding”,注意,由于一个bug将导致IE 4-6无法缓存内容。

#gzip_vary on;

upstream  backend  {

server   127.0.0.1:8080     weight=5     max_fails=3  fail_timeout=30s;

}

server {

listen       192.168.56.1:80;

server_name  www.server110.com;

#charset utf-8;

  log_format  test  '$remote_addr - $remote_user [$time_local] $request '

'"$status" $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/host.access.log  test;

location / {

proxy_pass http://backend;

proxy_redirect     off;

#保留用户真实信息

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 ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css)$

  {

   expires      30d;

  }

  #location ~ .(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|

  #zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {

#    root /opt/webapp;

  #   expires 24h;

#}

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

#    }

#}

}

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。