|
Nginx的配置文件是一个纯文本文件,它一般位于nginx安装目录的conf目录下,整个文件是以block的形式组织的,每个block一般以一个{}来表示,block可以分为几个层次,整个配置文件的main位于指令的最高层!在main层下有Events,HTTP层!而HTTP层又包含了server层,即server block,server block又可分为location层!并且一个server block中包含多个location block。Nginx配置文件结构图如下:
Nginx配置文件详解:
nginx.conf为Nginx的主配置文件,这里重点介绍下nginx.conf
Nginx配置文件主要分为4部分:
Main全局设置:影响其他所有设置
Server主机设置:配置指定的主机和端口
Upstream负载均衡服务器设置 :设置一系列的后置服务器
Location URL匹配特定位置的设置 :匹配网页位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
| #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;
# }
#}
}
|
|
|