Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

1.4. http 配置

1.4.1. 缓冲区相关设置

自定义缓冲区相关设置

			
client_body_buffer_size 1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
			
		

上传文件提示 client intended to send too large body,配置下面参数可以解决。

			
server {
  ...
  client_max_body_size 200M;
}
			
		

1.4.2. 超时设置

超时相关设置

			
	client_body_timeout 10;
	client_header_timeout 10;
	keepalive_timeout 65;
	send_timeout 10;
			
		

1.4.3. gzip

			
	gzip on;
	gzip_min_length 1000;
	gzip_buffers 4 8k;
	gzip_types text/plain text/css application/json application/x-javascript application/xml;


	gzip on;
	gzip_http_version 1.0;
	gzip_disable "MSIE [1-6].";
	gzip_types text/plain application/x-javascript text/css text/javascript;
			
		

gzip_types 压缩类型

			
	gzip_types text/plain text/css application/javascript text/javascript application/x-javascript text/xml application/xml application/xml+rss application/json;
			
		

text/html 是 gzip_types 默认值,所以不要将text/html加入到gzip_types

测试,验证 gzip 正常工作

			
neo@netkiller:~/workspace$ curl -s -I -H 'Accept-Encoding: gzip,deflate' http://img.netkiller.cn/js/react.js | grep gzip
Content-Encoding: gzip
			
		

如果提示 Content-Encoding: gzip 便是配置正确

不仅仅只能压缩html,js,css还能压缩json

			
neo@netkiller:~$ curl -s -I -H 'Accept-Encoding: gzip,deflate' http://inf.netkiller.cn/list/json/2.json
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 15 Dec 2016 03:36:31 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Cache-Control: max-age=60
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type,Origin
Access-Control-Allow-Methods: GET,OPTIONS
Content-Encoding: gzip
			
		

1.4.3.1. CDN支持

配置 gzip_proxied any; 后CDN才能识别 gzip

				
server_tokens off;
gzip on;
gzip_types text/plain text/css application/javascript text/javascript application/x-javascript text/xml application/xml application/xml+rss application/json;
gzip_proxied any;
				
			

1.4.3.2. 使用包含配置文件配置 gzip

				
cat <<-EOF> /etc/nginx/conf.d/gzip.conf
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_min_length 1000;
gzip_types text/plain text/css application/javascript application/json application/xml application/octet-stream;
EOF

# text/html 类型无需配置,否则会提示
# nginx: [warn] duplicate MIME type "text/html" in /etc/nginx/conf.d/default.conf				
				
			

1.4.4. server_tokens

隐藏nginx版本号

			
http {
...
server_tokens off;
...
}
			
		

1.4.5. ssi

			
http {
	ssi on;
}

location / {
	ssi on;
	ssi_silent_errors on;
	ssi_types text/shtml;
}
			
		
			
	ssi on;
	ssi_silent_errors on;
	ssi_types text/shtml;
	ssi_value_length 256;
	
	server_names_hash_bucket_size 128;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	client_max_body_size 8m;
			
		

ssi_silent_errors 默认值是off,开启后在处理SSI文件出错时不输出错误提示:"[an error occurred while processing the directive] "

ssi_types 默认是ssi_types text/html,如果需要shtml支持,则需要设置:ssi_types text/shtml

ssi_value_length 默认值是 256,用于定义SSI参数的长度。

1.4.6. DNS 解析

从指定的 DNS 解析域名

			
resolver 202.102.134.68 114.114.114.114 valid=5 ipv6=off;
set $proxy "http://api.netkiller.cn:8080";
location /v1/api { 
    proxy_pass $proxy;
}			
			
		

1.4.7. rewrite

		
Rewrite Flags
last - 基本上都用这个Flag。
break - 中止Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301

文件及目录匹配,其中:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行

正则表达式全部符号解释
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~* 分别为区分大小写不匹配及不区分大小写不匹配
(pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘\(’ 或 ‘\)’。
^ 匹配输入字符串的开始位置。
$ 匹配输入字符串的结束位置。		
		
		
		
	server {
		listen 80;
		server_name www.example.com example.com ;
		if ($host = "example.com" )
		{
			rewrite ^/(.*)$ http://www.example.com/$1 permanent;
		}
		if ($host != "www.example.com" )
		{
			rewrite ^/(.*)$ http://www.example.com/$1 permanent;
		}
	}		
		

		

1.4.7.1. 处理泛解析

			
	if ($host ~ '(.*)\.example\.com' ) {
		set $subdomain $1;
		rewrite "^/(.*)$" /$subdomain/$1;
	}		
			
			

1.4.7.2. 处理扩展名

			
	location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
		if (!-f $request_filename){
			rewrite /(.*) http://images.example.com/$1;
		}
	}		
			

			

1.4.7.3. http get 参数处理

需求如下

			
原理地址:
http://www.netkiller.cn/redirect/index.html?skuid=133

目的地址:
http://www.netkiller.cn/to/133.html
			
			

注意:nginx rewrite 并不支持http get 参数处理,也就是说“?”之后的内容rewrite根部获取不到。

下面的例子是行不通的

			
rewrite ^/redirect/index\.html\?skuid=(\d+)$ /to/$1.html permanent ;
			
			

我们需要通过正在查出参数,然后赋值一个变量,再将变量传递给rewrite。具体做法是:

			
server {
    listen       80;
    server_name www.netkiller.cn;

    #charset koi8-r;
    access_log  /var/log/nginx/test.access.log  main;

    location / {
        root   /www/test;
        index  index.html;

		if ($request_uri ~* "^/redirect/index\.html\?skuid=([0-9]+)$") {
                set $argv1 $1;
                rewrite .* /to/$argv1.html? permanent;
        }
    }
}
			
			

测试结果

			
[neo@netkiller conf.d]$ curl -I http://www.netkiller.cn/redirect/index.html?skuid=133
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 12 Apr 2016 06:59:33 GMT
Content-Type: text/html
Content-Length: 178
Location: http://www.netkiller.cn/to/133.html
Connection: keep-alive
			
			

1.4.7.4. 正则取非

需求如下,除了2015年保留,其他所有页面重定向到新页面

				rewrite ^/promotion/(?!2015\/)(.*) https://www.netkiller.cn/promotion.html permanent;
			

1.4.7.5. 去掉扩展名

需求

			
http://www.example.com/article/10	=>	http://www.example.com/article/10.html			
			
			
			
location / {
    if (!-e $request_filename){
        rewrite ^(.*)$ /$1.html last;
        break;
    }
}			
			
			

1.4.7.6. 添加扩展名

			
原地址 http://ipfs.netkiller.cn/ipfs/QmcA1Fsrt6jGTVqAUNZBqaprMEdFaFkmkzA5s2M6mF85UC
目标地址: http://ipfs.netkiller.cn/ipfs/QmcA1Fsrt6jGTVqAUNZBqaprMEdFaFkmkzA5s2M6mF85UC.mp4
			
			
			
    location / {
        rewrite ^/(.*)\.mp4$ /$1 last;
        proxy_pass      http://127.0.0.1:8080;
    }