#user nobody nobody;
Nginx用户及组,window下不指定
worker_processes 4;
工作进程的数目(nginx工作时分主进程和若干个工作进程)。根据硬件调整,通常等于CPU核芯总数。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
错误日志存放路径
#pid logs/nginx.pid;
pid文件存放路径
events {
worker_connections 1024;
}
一个工作进程可处理的连接数量
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;
这里配置access日志文件的格式和路径,格式中的变量含义如下:
$remote_addr:用以记录客户端的ip地址;
$http_x_forwarded_for:由代理服务器发送过来的真实客户端ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
下面是此配置的实际记录示例:
127.0.0.1 - - [20/Nov/2017:17:45:22 +0800] "GET / HTTP/1.1" 200 159 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"
sendfile on;
#tcp_nopush on;
开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
tcp_nopush:
允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#keepalive_timeout 0;
keepalive_timeout 65;
长连接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。长连接请求大量小文件的时候,可以减少重建连接的开销,但假如有大文件上传,65s内没上传完成会导致失败。如果设置时间过长,用户又多,长时间保持连接会占用大量资源。
#send_timeout :
用于指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。
client_max_body_size 500m
允许客户端请求的最大单文件字节数。如果有上传较大文件,请设置它的限制值
client_body_buffer_size 128k
缓冲区代理缓冲用户端请求的最大字节数,可尽量设置得大一些
#gzip on;
开启gzip压缩传输,具体配置项目如下:
gzip_min_length 1k :
设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是20。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_buffers 4 16k :
设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
gzip_http_version 1.0 :
用于识别 http 协议的版本,早期的浏览器不支持 Gzip 压缩,用户就会看到乱码,所以为了支持前期版本加上了这个选项,如果你用了 Nginx 的反向代理并期望也启用 Gzip 压缩的话,由于末端通信是 http/1.0,故请设置为 1.0。
gzip_comp_level 6 :
gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_types :
匹配mime类型进行压缩,无论是否指定,"text/html"类型总是会被压缩的。
gzip_proxied any :
Nginx作为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必须要返回包含"Via"的 header头。
gzip_vary on :
和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可以让前端的缓存服务器缓存经过gzip压缩的页面,例如,用Squid缓存经过Nginx压缩的数据。。
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
自动列出目录
autoindex_exact_size:
默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime:
默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
server {
listen 80;
监听端口,也可以写成listen *:80、listen 127.0.0.1:80等形式。
server_name localhost;
服务器名,如localhost、www.example.com,可以通过正则匹配。
charset utf-8;
access_log logs/host.access.log main;
这里指定了针对这台虚拟服务器的日志文件路径和使用的格式
location / {
#root html;
root F:/server/www;
index index.html index.php index.htm;
}
每个server都可以配置多个location,因为server只是指定了服务器名,对于不同的访问路径,可以有不同的配置,比如以下这些配置:
location / { 通用匹配, 如果没有其它匹配,任何请求都会匹配到
location = /50x.html { 精确匹配URL: 服务器名/50x.html
location ~ \.php$ { 区分大小写的正则匹配:以.php结尾的URL
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html;
指定404及其他错误页面的路径及网页文件
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; root F:/server/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; #} } server { listen 80; server_name cmseasy; charset utf-8; #access_log logs/host.access.log main; location / { #root html; root F:/server/cmseasy; index index.html index.php index.htm; } #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; root F:/server/cmseasy; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; # } #} }