IT俱乐部 Nginx nginx 反向代理负载均衡策略配置SSL访问匹配规则优先级

nginx 反向代理负载均衡策略配置SSL访问匹配规则优先级

引言

首先Nginx能做反向代理【关于反向代理和正向代理此处不做说明了,感兴趣的小伙伴自行谷歌】;比方说,我想在本地使用 www.google.com 的域名去访问www.taobao.com。那么这个时候我们就可以通过nginx去实现

再者Nginx能实现负载均衡,就是说应用部署在不同的服务器上,但是通过统一的域名进入,nginx则对请求进行分发,将请求分发到不同的服务器上去处理,这样就可以有效的减轻了单台服务器的压力,解决单点故障,在上面这两种情况下,nginx服务器的作用都只是作为分发服务器,真正的内容,我们可以放在其他的服务器上,这样来,还能起到一层安全隔壁的作用,nginx可以作为隔离层

解决跨域问题

同源:URL由协议、域名、端口和路径组成,如果两个URL的协议、域名和端口相同,则表示他们同源

浏览器的同源策略:浏览器的同源策略,限制了来自不同源的”document”或脚本,对当前”document”读取或设置某些属性。

从一个域上加载的脚本不允许访问另外一个域的文档属性(同源表示:协议、域名和端口相同)

例如:因为nginx和tomcat不能共用同一端口,url一样,端口不同,这样就会有跨域问题

Nginx配置文件

配置文件主要由四部分组成:

main(全区设置)

server(主机配置)

http(控制着nginx http处理的所有核心特性)

location(URL匹配特定位置设置)。

upstream(负载均衡服务器设置)

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
#Nginx的worker进程运行用户以及用户组
#user  nobody;
#Nginx开启的进程数
worker_processes  1;
#定义全局错误日志定义类型,[debug|info|notice|warn|crit]
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#指定进程ID存储文件位置
#pid        logs/nginx.pid;
#事件配置
events {
    #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
    #epoll模型是Linux内核中的高性能网络I/O模型,如果在mac上面,就用kqueue模型。
    use kqueue;
    #每个进程可以处理的最大连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。理论值:worker_rlimit_nofile/worker_processes
    worker_connections  1024;
}
#http参数
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;
    #开启高效传输模式
    sendfile        on;
    #防止网络阻塞
    #tcp_nopush     on;
    #客户端连接超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #开启gzip压缩输出
    #gzip  on;
    #虚拟主机基本设置
    server {
        #监听的端口号
        listen       80;
        #访问域名
        server_name  localhost;
        #编码格式,如果网页格式与当前配置的不同的话将会被自动转码
        #charset koi8-r;
        #虚拟主机访问日志定义
        #access_log  logs/host.access.log  main;
        #对URL进行匹配
        location / {
            #访问路径,可相对也可绝对路径
            root   html;
            #首页文件,匹配顺序按照配置顺序匹配
            index  index.html index.htm;
        }
        #错误信息返回页面
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        #访问URL以.php结尾则自动转交给127.0.0.1
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        # }
        # php脚本请求全部转发给FastCGI处理
        # 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;
        #}
        # 禁止访问.ht页面
        # 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虚拟主机定义
    # 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;
    #    }
    #}
    include servers/*;
}

反向代理实例—》

假设现在代理 www.baidu.com

1
2
3
4
5
6
7
8
9
10
11
server {
    # 监听80端口
    listen 80;
    server_name localhost;
     # individual nginx logs for this web vhost
    access_log /tmp/access.log;
    error_log  /tmp/error.log ;
    # 配置代理
    location / {
        proxy_pass http://www.baidu.com;
    }

负载均衡实例

(下面主要验证最常用的三种负载策略。虚拟主机配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
    #监听80端口
    listen 80;
    server_name localhost;
    # individual nginx logs for this web vhost
    access_log /tmp/access.log;
    error_log  /tmp/error.log ;
    location / {
        # 负载均衡策略
        # 轮询
        # proxy_pass http://polling_strategy;
        # weight权重
        # proxy_pass http://weight_strategy;
        # ip_hash
        # proxy_pass http://ip_hash_strategy;
        # fair
        # proxy_pass http://fair_strategy;
        # url_hash
        # proxy_pass http://url_hash_strategy;
        # 重定向
        # rewrite ^ http://localhost:8080;
    }

轮询策略

1
2
3
4
5
6
7
8
9
10
11
# 1、轮询(默认)
# 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream polling_strategy {
    server www.net:8080; # 应用服务器1
    server www.net:8081; # 应用服务器2
}
测试结果(通过端口号来区分当前访问):
8081:hello
8080:hello
8081:hello
8080:hello

权重策略

1
2
3
4
5
6
7
#2、指定权重
# 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream  weight_strategy {
    server glmapper.net:8080 weight=1; # 应用服务器1
    server glmapper.net:8081 weight=9; # 应用服务器2
}
测试结果:总访问次数15次,根据上面的权重配置,两台机器的访问比重:2:13

ip hash策略

iphash 算法: ip是基本的点分十进制,将ip的前三个端作为参数加入hash函数。这样做的目的是保证ip地址前三位相同的用户经过hash计算将分配到相同的后端server。作者的这个考虑是极为可取的,因此ip地址前三位相同通常意味着来着同一个局域网或者相邻区域,使用相同的后端服务让nginx在一定程度上更具有一致性

假设5台机器均在同一个局域网内【192.168.0.X】测试时发现5台机器每次都路由到了同一个服务器上,一开始以为是配置问题,但是排查之后也排除了这个可能性。最后考虑到可能是对于同网段的ip做了特殊处理,验证之后确认了猜测

1
2
3
4
5
6
7
8
9
#3、IP绑定 ip_hash
#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,
#可以解决session的问题;在不考虑引入分布式session的情况下,
#原生HttpSession只对当前servlet容器的上下文环境有效
upstream ip_hash_strategy {
    ip_hash;
    server glmapper.net:8080; # 应用服务器1
    server glmapper.net:8081; # 应用服务器2
}

其他负载均衡策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#4、fair(第三方)
#按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream fair_strategy {
    server glmapper.net:8080; # 应用服务器1
    server glmapper.net:8081; # 应用服务器2
    fair;
}
#5、url_hash(第三方)
#按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,
#后端服务器为缓存时比较有效。
upstream url_hash_strategy {
    server glmapper.net:8080; # 应用服务器1
    server glmapper.net:8081; # 应用服务器2
    hash $request_uri;
    hash_method crc32;
}

重定向rewrite

验证思路:本地使用localhost:80端口进行访问,根据nginx的配置,如果重定向没有生效,则最后会停留在当前localhost:80这个路径,浏览器中的地址栏地址不会发生改变;如果生效了则地址栏地址变为localhost:8080;

通过验证,满足预期!

1
2
3
4
location / {
    #重定向
    #rewrite ^ http://localhost:8080;
}

nginx配置Vue前端发布history模式访问

1
2
3
4
5
6
7
8
9
10
# nginx配置history模式访问:
server {   
    listen       900;   
    server_name  localhost;     
    error_page   500 502 503 504  /50x.html;    
    location  / {        
    root   C:/Users/lenovo/Desktop/前端代码/dist
    index /index.html;   try_files $uri $uri/ /index.html;
    }
 }

nginx配置静态资源文件映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
##静态资源文件:
server{   
  listen 9998;  
 server_name localhost;      
 location /file {        
         add_header 'Access-Control-Allow-Origin' '*';        
         add_header 'Access-Control-Allow-Credentials' 'true';        
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';        
         add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';      
         # 文件夹路径
         alias  C:/Users/lenovo/Desktop/TestRes/;     
         autoindex on;        
         autoindex_exact_size on;       
         autoindex_localtime on;   
     }
}

实际线上Nginx配置文件参考

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
user root;
#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;
    #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;
    client_max_body_size 4096m;
    server {
        listen       1000;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        # 配置前端Vue的dist包发布
        location / {
            root   /usr/local/vue/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
        # 转发后端Nginx请求
        location /service/ {
            proxy_pass http://localhost:3389/;
        }
        # 配置文件路径映射     
        location /files {
           alias /usr/local/resource/;
           autoindex on;
        }
        #error_page  404              /404.html;
        # 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;
        #}
    }
   #以下属性中,以ssl开头的属性表示与证书配置有关。
   server {
        listen 6666 ssl;
        # listen 443 ssl;
        #配置HTTPS的默认访问端口为443。
        #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
        #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on
        server_name www.test.cn; #需要将www.test.cn替换成证书绑定的域名。
        root html;
        index index.html index.htm;
        # cert为当前路径的文件夹
        ssl_certificate cert/www.test.cn.pem;  #需要将www.test.cn.pem替换成已上传的证书文件的名称。
        ssl_certificate_key cert/www.test.cn.key; #需要将www.test.cn.key替换成已上传的证书私钥文件的名称。
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        #表示使用的加密套件的类型。
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
        ssl_prefer_server_ciphers on;
        location /base {
              root html;  #站点目录。
              index index.html index.htm;
           }
        location / {
            root   /usr/local/vue/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
       location /service/ {
            proxy_pass http://localhost:3389/;
       }
       location /files {
           alias /usr/local/static/;
           autoindex on;
       }
  }
    # 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;
    #    }
    #}
}

后台服务访问代理配置

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
#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;
    #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       8988;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                        client_max_body_size 100m;
                        proxy_pass http://127.0.0.1/mobile/;
                        proxy_redirect    off;
                        proxy_read_timeout 3600;
                        proxy_send_timeout 3600;
                        proxy_buffer_size  128k;
                        proxy_buffers   32 32k;
                        proxy_busy_buffers_size 128k;
                        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
                        proxy_set_header  X-Real-IP  $remote_addr;
                        proxy_set_header  Host $host:$server_port;
        }
        location /etc    {
                        client_max_body_size 100m;
                        proxy_pass http://127.0.0.1/api/notice;
                        proxy_redirect    off;
                        proxy_read_timeout 3600;
                        proxy_send_timeout 3600;
                        proxy_buffer_size  128k;
                        proxy_buffers   32 32k;
                        proxy_busy_buffers_size 128k;
                        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
                        proxy_set_header  X-Real-IP  $remote_addr;
                        proxy_set_header  Host $host:$server_port;
        }
location /fileResult {        
         add_header 'Access-Control-Allow-Origin' '*';        
         add_header 'Access-Control-Allow-Credentials' 'true';        
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';        
         add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';      
        alias  /root/fileResult ;     
         autoindex on;        
         autoindex_exact_size on;       
         autoindex_localtime on;   
        }
    location /fileLocal {        
         add_header 'Access-Control-Allow-Origin' '*';        
         add_header 'Access-Control-Allow-Credentials' 'true';        
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';        
         add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';      
         # 文件路径
         alias  /root/local;     
         autoindex on;        
         autoindex_exact_size on;       
         autoindex_localtime on;   
        }
        #error_page  404              /404.html;
        # 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;
    #    }
    #}
}

Nginx调优

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
增加工作线程数和并发连接数
[root@localhost /]# vi /etc/nginx/nginx.conf
worker_processes  4; # 一般CPU 是几核就设置为几 也可以设置成auto
events {
    worker_connections  10240; # 每个进程打开的最大连接数,包含了 Nginx 与客户端和 Nginx 与 upstream 之间的连接
    multi_accept on; # 可以一次建立多个连接
    use epoll;   #epoll这种网络模型
}
查看nginx 语法是否正确
[root@localhost /]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
启用长连接
[root@localhost /]# vi /etc/nginx/nginx.conf
配置反向代理
upstream server_pool{
    server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
    server localhost:8081 weight=1 max_fails=2 fail_timeout=30s;
    keepalive 300; # 300个长连接 提高效率
}
配置反向代理服务
location / {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://server_pool#所有请求都代理给server_pool
}
配置压缩
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;
gzip_vary on;
gzip_static on;
操作系统优化
 配置文件/etc/sysctl.conf
sysctl -w net.ipv4.tcp_syncookies=1 # 防止一个套接字在有过多试图连接到时引起过载
sysctl -w net.core.somaxconn=1024 # 默认128,操作系统连接队列
sysctl -w net.ipv4.tcp_fin_timeout=10 # timewait 的超时时间
sysctl -w net.ipv4.tcp_tw_reuse=1 # os 直接使用 timewait的连接
sysctl -w net.ipv4.tcp_tw_recycle=0 # 回收禁用
 /etc/security/limits.conf
           hard    nofile            204800
            soft    nofile             204800
            soft    core             unlimited
             soft    stack             204800
其它优化
sendfile    on; # 减少文件在应用和内核之间拷贝
tcp_nopush  on; # 当数据包达到一定大小再发送
tcp_nodelay off; # 有数据随时发送

Nginx匹配规则说明以及匹配的优先级

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
location 匹配规则
语法规则
    location [=|~|~*|^~] /uri/ { … }
模式    含义
location = /uri    = 表示精确匹配,只有完全匹配上才能生效
location ^~ /uri    ^~ 开头对URL路径进行前缀匹配,并且在正则之前。
location ~ pattern    开头表示区分大小写的正则匹配
location ~* pattern    开头表示不区分大小写的正则匹配
location /uri    不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location /    通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default
前缀匹配时,Nginx 不对 url 做编码,因此请求为 /static/20%/aa
可以被规则 ^~ /static/ /aa 匹配到(注意是空格)
多个 location 配置的情况下匹配顺序为(参考资料而来):
    首先精确匹配 =
    其次前缀匹配 ^~
    其次是按文件中顺序的正则匹配
    然后匹配不带任何修饰的前缀匹配。
    最后是交给 / 通用匹配
    当有匹配成功时候,停止匹配,按当前匹配规则处理请求
注意:前缀匹配,如果有包含关系时,按最大匹配原则进行匹配。
比如在前缀匹配:location /dir01 与location /dir01/dir02
如有请求 http://localhost/dir01/dir02/file 将最终匹配到 location /dir01/dir02
例子,有如下匹配规则:
location = / {
   echo "规则A";
}
location = /login {
   echo "规则B";
}
location ^~ /static/ {
   echo "规则C";
}
location ^~ /static/files {
    echo "规则X";
}
location ~ .(gif|jpg|png|js|css)$ {
   echo "规则D";
}
location ~* .png$ {
   echo "规则E";
}
location /img {
    echo "规则Y";
}
location / {
   echo "规则F";
}
那么产生的效果如下:
访问根目录 /,比如 http://localhost/ 将匹配 规则A
访问 http://localhost/login 将匹配 规则B
访问 http://localhost/register 则匹配 规则F
访问 http://localhost/static/a.html 将匹配 规则C
访问 http://localhost/static/files/a.exe 将匹配 规则X,虽然 规则C 也能匹配到,
但因为最大匹配原则,最终选中了 规则X。你可以测试下,去掉规则 X ,则当前 URL 会匹配上 规则C。
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配 规则D 和 规则 E ,
但是 规则 D 顺序优先,规则 E 不起作用,而 http://localhost/static/c.png 则优先匹配到 规则 C
访问 http://localhost/a.PNG 则匹配 规则 E ,而不会匹配 规则 D ,因为 规则 E 不区分大小写。
访问 http://localhost/img/a.gif 会匹配上 规则D,虽然 规则Y 也可以匹配上,
但是因为正则匹配优先,而忽略了 规则Y。
访问 http://localhost/img/a.tiff 会匹配上 规则Y。
访问 http://localhost/category/id/1111 则最终匹配到规则 F ,因为以上规则都不匹配,
这个时候应该是 Nginx 转发请求给后端应用服务器,
比如 FastCGI(php),tomcat(jsp),Nginx 作为反向代理服务器存在。
所以实际使用中,笔者觉得至少有三个匹配规则定义,如下:
# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* .(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}
# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php、.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}
rewrite 语法
    last – 基本上都用这个 Flag
    break – 中止 Rewirte,不再继续匹配
    redirect – 返回临时重定向的 HTTP 状态 302
    permanent – 返回永久重定向的 HTTP 状态 301
1、下面是可以用来判断的表达式:
    -f 和 !-f 用来判断是否存在文件
    -d 和 !-d 用来判断是否存在目录
    -e 和 !-e 用来判断是否存在文件或目录
    -x 和 !-x 用来判断文件是否可执行
2、下面是可以用作判断的全局变量
    例:http://localhost:88/test1/test2/test.php?k=v
    $host:localhost
    $server_port:88
    $request_uri:/test1/test2/test.php?k=v
    $document_uri:/test1/test2/test.php
    $document_root:D: ginx/html
    $request_filename:D: ginx/html/test1/test2/test.php
redirect 语法
server {
    listen 80;
    server_name start.igrow.cn;
    index index.html index.php;
    root html;
    if ($http_host !~ "^star.igrow.cn$") {
        rewrite ^(.*) http://star.igrow.cn$1 redirect;
    }
}
防盗链
location ~* .(gif|jpg|swf)$ {
    valid_referers none blocked start.igrow.cn sta.igrow.cn;
    if ($invalid_referer) {
       rewrite ^/ http://$host/logo.png;
    }
}
根据文件类型设置过期时间
location ~* .(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        expires 1h;
        break;
    }
}
禁止访问某个目录
location ~* .(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
}
---------------------

以上就是nginx 反向代理负载均衡策略配置SSL访问匹配规则优先级的详细内容,更多关于nginx配置SSL访问的资料请关注IT俱乐部其它相关文章!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/server/nginx/9203.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部