nginx.conf 问题
问题:
解决:
- 通过配置http传输数据大小解决;
client_max_body_size 1024m;
。 - 配置以下代码,支持压缩文件访问,可提高访问速度。
gzip on; gzip_static on; gzip_min_length 1k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_disable "MSIE [1-6].";
- 过滤路径下携带请求头。
location /api-server { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 代理地址 proxy_pass http://127.0.0.1:8084; }
- 静态资源路径配置。
location ~* .(js|css|ttf|woff|ico|wav|png|jpg) { # 资源文件包 root html/api; }
以上问题,做一个简单的配置文件nginx.conf如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 传输信息大小限制
client_max_body_size 1024m;
#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 on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8082;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 也可放在server之外
gzip on;
gzip_static on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6].";
# 静态资源存放在/html/api/文件夹下
location / {
root html/api;
index index.html index.htm;
}
# 后台服务代理
location /api-server {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8084;
}
# 静态资源文件路径
location ~* .(js|css|ttf|woff|ico|wav|png|jpg) {
root html/api;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
附录:常用匹配规则
location 顺序/优先级:
(location =) > (location 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) > (location 部分起始路径) > (/)
定义:
- = 开头表示精确匹配; 如: A 中只匹配根目录结尾的请求,后面不能带任何字符串
- ^~ 开头表示uri以某个常规字符串开头,不是正则匹配
- ~ 开头表示区分大小写的正则匹配
- ~* 开头表示不区分大小写的正则匹配
- / 通用匹配, 如果没有其它匹配,任何请求都会匹配到
常用正则:
- . : 匹配除换行符以外的任意字符
- ? : 重复0次或1次
- + : 重复1次或更多次
- * : 重复0次或更多次
- \d :匹配数字
- ^ : 匹配字符串的开始
- $ : 匹配字符串的结束
- {n} : 重复n次
- {n,} : 重复n次或更多次
- [c] : 匹配单个字符c
- [^/] : 匹配除了/之外的所有字符
- [a-z] : 匹配a-z小写字母的任意一个
location = / {
# 精确匹配 / ,主机名后面不能带任何字符串
[ conf A ]
}
location / {
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
# 但是正则和最长字符串会优先匹配
[ conf B ]
}
location /api/ {
# 匹配任何以 /api/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ conf C ]
}
location ~ /api/abc {
# 匹配任何以 /api/abc 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ conf D ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
[ conf E ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 conf E 处理,因为 ^~ 到达不了这一条正则
[ conf F ]
}
location /images/ {
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ conf G ]
}
location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ conf H ]
}
location ~ /images/abc/ {
# 只有去掉 conf E 才有效:先最长匹配 conf H 开头的地址,继续往下搜索,匹配到这一条正则,采用
[ conf I ]
}
location ~* /js/.*/\.js {
# 不区分大小写匹配
[ conf J ]
}