Nginx的配置文件是一个纯文本文件,它通常位于nginx安装目录的conf目录下,整个文件是以block的形式组织的,每一个block通常以一个{}来表示,block能够分为几个层次,整个配置文件的main位于指令的最高层!在main层下有Events,HTTP层!而HTTP层又包含了server层,即server block,server block又可分为location层!而且一个server block中包含多个location block。javascript
Nginx配置文件结构图以下:php
Nginx配置文件详解:css
nginx.conf为Nginx的主配置文件,这里重点介绍下nginx.confhtml
Nginx配置文件主要分为4部分:前端
Main全局设置:影响其余全部设置java
Server主机设置:配置指定的主机和端口node
Upstream负载均衡服务器设置 :设置一系列的后置服务器nginx
Location URL匹配特定位置的设置 :匹配网页位置缓存
#user nobody; #指定Nginx Worker进程运行用户和用户组,默认nobody帐号 worker_processes 1; #指定Nginx要开启的进程数,建议和cpu数量同样的 #定义全局错误日志文件。日志有输出级别: [ debug | info | notice | warn |error | crit ] #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #进程文件 #设定Nginx的工做模式和链接数上限 events { worker_connections 1024; #单个进程最大链接数(最大链接数=链接数*进程数) use epoll; #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select| poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,若是跑在FreeBSD上面,就用kqueue模型 } #设定服务器相关属性 http { include mime.types; #文件拓展名和文件类型映射表 default_type application/octet-stream; #默认文件类型 #日志输出格式 #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; #main为此日志输出格式的名称 client_max_body_size 8m; #设置容许客户端请求的最大单个文件字节数 client_header_buffer_size 32k; #上传文件大小限制 large_client_header_buffers 464k;#指定客户端请求中较大的消息头的缓存最大数量和大小 sendfile on; #开启高效文件传输模式 #autoindex on;#开启目录表访问,合适下载服务器,默认关闭 #tcp_nopush on; #防止网络阻塞 #tcp_nodelay on; #防止网络阻塞 #keepalive_timeout 0; keepalive_timeout 65;#长链接超时时间,单位是秒 client_header_timeout;#设置客户端请求头读取超时时间,超时后,服务端会关闭链接 client_body_timeout;#设置客户端请求主体读取超时时间,超时后,客户端没有发送任务数据, Nginx会返回"Request time out(408)错误" send_timeout:#指定响应客户端的超时时间。若超时,客户端没有任务会话,Nginx会关闭链接 #gzip on; #开启gzip压缩文件大小,实时压缩输出数据流 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.0;#压缩版本 gzip_comp_level 2; #压缩等级 gzip_types text/plainapplication/x-javascript text/css application/xml;#指定压缩类型。 不管是否指定,“text/html”类型老是压缩的 gzip_vary on; #让前端的缓存服务器通过gzip压缩的页面。例如用Squid缓存通过Nginx压缩数据 server { listen 80;#指定虚拟主机的服务端口 server_name localhost;#指定IP地址或域名,多个域名之间用空格分开 #charset koi8-r; #设定网页的默认编码格式 #access_log logs/host.access.log main; #指定虚拟主机的访问日志存放路径,最后的 main用于指定访问日志的输出格式 location / { root html; #指定虚拟主机的网页根目录,能够是相对路径,也能够是绝对路径 index index.html index.htm; #设定访问的默认首页地址 } #图片缓存时间设置 #全部以gif|jpg|jpeg|png|bmp|swf结尾的静态文件都交给nginx处理 location ~.*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10d;#指定静态文件过时时间。这里是10天 } #JS和CSS缓存时间设置 location ~ .*.(js|css)?$ { expires 1h; } #error_page 404 /404.html; #返回404错误页面 # 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 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }