#【全局块】配置影响nginx全局的指令。
#定义Nginx运行的用户和用户组
#user nobody;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 1;
#全局错误日志路径及级别,级别由低到高是:debug、info、notice、warn、error、crit
#error_log logs/error.log info;
#记录进程pid的文件
#pid logs/nginx.pid;
#进程可打开的最大描述符数目,理论值是最多打开文件数(ulimit -n)与nginx进程数相除,但nginx分配请求不均匀,所以最好与ulimit -n 的值保持一致
#worker_rlimit_nofile 1024;
#【events块】配置影响nginx服务器或与用户的网络连接
events {
#指定参考事件模型,可选:select(标准事件模型)、poll(标准事件模型)、kqueue(FreeBSD的高效事件模型)、epoll(Linux2.6+的高效事件模型)、/dev/poll(Solaris的高效事件模型)、Eventport(Solaris的高效事件模型)
use epoll;
#单个进程最大连接数,根据硬件调整,和前面工作进程配合起来用,尽量大
worker_connections 1024;
#保持活动超时时间
keepalive_timeout 60;
#客户端请求头部的缓冲区大小,必须设置为“系统分页大小(getconf PAGESIZE)”的整倍数。
client_header_buffer_size 4k;
#打开文件缓存数量,建议和上述“最大连接数”一致,inactive指经过多长时间文件没被请求后删除缓存。
open_file_cache max=1024 inactive=60s;
#多长时间检查一次缓存的有效性,默认是60s。
open_file_cache_valid 80s;
#缓存文件在指定时间内的最少使用次数,如果超过这个数字,文件描述符保留缓存中,否则将被移除。
open_file_cache_min_uses 1;
#当搜索一个文件时是否缓存错误信息,默认值off
open_file_cache_errors on;
}
【http块】可以嵌套多个server,配置代理、缓存、日志等绝大多数功能和第三方模块的配置
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#载入文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#默认编码
#charset utf-8;
#服务器名字的hash表最大值
server_names_hash_max_size 512;
#服务器名字的hash表大小
server_names_hash_bucket_size 128;
#如果Nginx检查语法提示需增大server_names_hash_max_size或server_names_hash_bucket_size,那么首要增大hash_max_size
#客户端请求头部的缓冲区大小。可根据“系统分页大小(getconf PAGESIZE)”来设置
client_header_buffer_size 4k;
#大型客户端请求头缓冲区的数量和大小,优先使用client_header_buffer_size值,不够时才使用该值
large_client_header_buffers 4 64k;
#设定通过nginx上传文件的大小
client_max_body_size 8m;
#访问日志格式设定
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#日志格式用到的相关变量含义:
#$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址(通常web服务器放在反向代理的后面时通过$remote_add拿到的IP地址是反向代理的iP地址。可以用$http_x_forwarded_for记录客户端的IP地址和客户端请求的服务器地址。)
#$remote_user:用来记录客户端用户名称;
#$time_local:用来记录访问时间与时区;
#$request:用来记录请求的url与http协议;
#$status:用来记录请求状态;成功是200,
#$body_bytes_sent:记录发送给客户端文件主体内容大小;
#$http_referer:用来记录从那个页面链接访问过来的;
#$http_user_agent:记录客户浏览器的相关信息;
#定义http服务器的访问日志路径和使用哪个格式
#access_log logs/access.log main;
#开启高效文件传输模式,普通应用设为on,磁盘IO重负载应用(如下载),设为off。
sendfile on;
#开启目录列表访问,合适下载服务器,默认关闭。
autoindex on;
#开启nginx使用socke的TCP_CORK功能,此选项仅在使用sendfile的时候使用
#tcp_nopush on;
#开启nginx使用TCP_NODELAY功能
#tcp_nodelay on;
#长连接超时时间,单位是秒
keepalive_timeout 65;
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区大小
gzip_http_version 1.0; #压缩版本[1.0|1.1]
gzip_comp_level 2; #压缩等级,范围1-9,越大压缩率越高
gzip_types text/plain application/x-javascript text/css application/xml; #要压缩的MIME类型, #默认就已经包含text/html,就不用再写了,写上去会有一个warn。
gzip_vary on; #和http头有关系,加个vary头,给代理服务器用的
#开启限制IP连接数的时候需要使用
#limit_zone crawler $binary_remote_addr 10m;
#负载均衡配置
upstream shili.com {
#负载均衡分内置策略和扩展策略,再细分如下
#1、轮询(内置默认)
#每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除
server 192.168.80.121:80
server 192.168.80.122:80
#2、weight权重(内置),根据机器配置定义,越大被分配到的几率越大
#server 192.168.80.121:80 weight=3;
#server 192.168.80.122:80 weight=2;
#3、ip_hash按来访ip的hash(内置),这样每个访客固定同一台后端服务器,可解决session问题
#ip_hash;
#server 192.168.0.14:88;
#server 192.168.0.15:80;
#4、fair按后端服务器的响应时间(扩展),响应时间短的优先分配
#server server1;
#server server2;
#fair;
#5、url_hash按访问url的hash(扩展),使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
#server squid1:3128;
#server squid2:3128;
#hash $request_uri;
#hash_method crc32;
#另外每个设备的状态可设置为:
#1.down:表示单前的server暂时不参与负载
#2.weight:为weight越大,负载的权重就越大。
#3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
#4.fail_timeout:max_fails次失败后,暂停的时间。
#5.backup:其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
#client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
#client_body_temp_path设置记录文件的目录 可以设置最多3层目录
#location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
}
#【server块】配置虚拟主机
server {
listen 80; #监听端口
server_name localhost; #域名有多个时用空格隔开
#charset utf-8; #设置字符集
#keepalive_requests 120; #单连接请求上限次数
#定义本虚拟主机的访问日志路径和使用哪个格式
#access_log logs/host.access.log main;
#【location块】配置路由、各种页面处理情况
location / {
root html; #根目录
index index.html index.htm; #设置默认页
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
#将服务器404错误页面重定向到静态页面/404.html
#error_page 404 /404.html;
#将服务器错误页面重定向到静态页面/50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#将PHP脚本反向代理到127.0.0.1:80
location ~ \.php$ {
proxy_pass http://127.0.0.1; #代理转向服务器地址
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
#允许客户端请求的最大单文件字节数
client_max_body_size 10m;
#缓冲用户端请求的最大字节数
client_body_buffer_size 128k;
#表示使nginx阻止HTTP应答代码为400或者更高的应答。
proxy_intercept_errors on;
#代理连接后端超时时间
proxy_connect_timeout 90;
#代理发送数据给后端超时时间
proxy_send_timeout 90;
#代理读取后端服务器数据超时时间
proxy_read_timeout 90;
#设置代理服务器保存用户头信息的缓冲区大小
proxy_buffer_size 4k;
#设置用于读取应答的缓冲区数目和大小
proxy_buffers 4 32k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;
#设置在写入代理临时文件时数据的大小
proxy_temp_file_write_size 64k;
#}
#将PHP脚本传递给在127.0.0.1:9000上监听的FastCGI服务器
#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;
#}
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#设置静态文件缓存时间
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
expires 15d;
}
#拒绝访问.htaccess文件
#location ~ /\.ht {
# deny all;
#}
}
#基于IP、域名、端口的虚拟主机配置
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS服务器配置
#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;
# }
#}
}
转载请注明:运维博客 » Nginx配置文件nginx.conf详解