nginx-转发规则案例详解

本文以http协议举例,https的暂未列举 如有错误请指正


http {
	#文件扩展名与文件类型映射表
    include       mime.types;				
	#默认文件类型,默认为text/plain	
    default_type  application/octet-stream;										

	#是否启用服务日志   
	access_log on;
	#自定义格式	
    log_format myFormat '$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 myFormat;

	#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile        on;															
    
	#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
	sendfile_max_chunk 100k;  		
	#连接超时时间,默认为75s,可以在http,server,location块。	
    keepalive_timeout 65;  														
    #开启Gzip 加快访问速度
    #gzip  on;

    server {
		#监听的端口
        listen       80;
		#监听的域名
        server_name  localhost;

        #charset koi8-r;
		#可以在这边指定只属于当前server的日志
        #access_log  logs/host.access.log  main;
		
		#等号匹配 优先级最高
		location = /50x.html {								
            root   html;
        }
		
		#^~ 匹配 优先级第二,不支持正则表达式
		#匹配成功就不再匹配其他location,如果有多个location匹配的话,则使用表达式最长的那个
		location ^~\.txt {
            root /;#文件放到/html文件夹下
        }
		
		#正则匹配 优先级第三,地址不可以带/,可以使用 /x$1 拼接。
		#例:proxy_pass http://127.0.0.1/x$;  
		location ~ /abc(.*) {
            root /;#文件放到/html文件夹下
        }
		
		#常规匹配1 如果有多个location匹配的话,则使用表达式最长的那个
        location /test1 {
			proxy_pass http://localhost:8080;
        }
		
		#常规匹配2 如果proxy_pass最后带/,则表示剪切掉匹配到的项目名重新拼接。如果有携带session等需要另外配置,此项会造成session丢失
		#例:http://localhost/test2/1经过转发后变为http://localhost/1
		location /test2 {
			proxy_pass http://localhost:8080/;
        }
		
        #错误页
        error_page   500 502 503 504  /50x.html;							
    }
}
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
上次更新: 2024-01-03, 13:22:13
最近更新
01
2023年度总结
01-03
02
MongoDB的简单的常用语法
12-11
03
cetnos7通过nfs共享磁盘文件
11-24
更多文章>