proxy_pass根据转发时/问题

案例:

# cat nginx.conf
  location ^~ /abc/ {                       #abc目录不存在
    proxy_pass http://192.168.1.1:8080/;    #访问www.a.com/abc/a.html===根路径/a.html

  }
# cat nginx.conf
  location ^~ /abc/ {                       ##abc目录存在
    proxy_pass http://192.168.1.1:8080;     #访问www.a.com/abc/a.html===根路径/abc/a.html
  }

http头列表

Accept               #浏览器接收的内容类型
Accept-Charset       #浏览器能识别的字符集
Accept-Encoding      #浏览器处理的编码方式
Accept-Language      #浏览器语言
Authorization        #认证,用户和密码
Cache-Control        #缓存系统,处理缓存的方式
Connection           #浏览器连接方式,http1.1默认是keep-alive
Cookie               #浏览器向服务器发送cookie,或服务器向浏览器附加cookie
Content-Length       #请求体的长度,单位字节
Content-MD5          #请求体的MD5校验
Content-Type         #请求体中的内容类型
Date                 #发送请求时的时间
Expect               #请求头通过服务器,处理该请求符合预期
From                 #发送请求的用户的地址
Host                 #服务器的域名或IP地址

 

 

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------

HTTP头域(proxy_set_header)列表与解释
HTTP 头域是HTTP协议中请求(request)和响应(response)中的头部信息,其实就是HTTP通信的操作参数,告诉web服务器和浏览器怎样处理这个通信。
HTTP头从一个请求信息或者响应信息的第二行开始(第一行是请求行或者响应行),以两个CR-LF字符组结束(CR:回车符,\r,LF:换行符\n)
而每个HTTP头是字符串形式的,用冒号分割的键值对,多个HTTP头之间用CR-LF字符组隔开。

某些http头可以有注释,例如user-agent,server,via。但这些注释会被服务器或者浏览器忽略IETF组织已经将一些核心的HTTP头定义在RFC2616规范中,
这些HTTP头是每个基于HTTP协议的软件必须实现的,而其他一些更新和扩展的头域也必须被基于HTTP的软件实现。当然,各个软件也可以定义自己的头域。

另一方面,RFC2616规范中并没有限制每个HTTP头的长度,或者限制HTTP头的数量,但出于性能和安全的考虑,多数服务器都会自己作规定,例如apache2.3
就规定每个HTTP头不能超过8190个字节,每个请求不能超过100个HTTP头。

以下来看看发送一个请求(request)时候,可能包含的各个HTTP头和它的解释。

------------标准的请求头------------

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
Accept: 浏览器(或者其他基于HTTP的客户端程序)可以接收的内容类型(Content-types),例如 Accept: text/plain
 
Accept-Charset:浏览器能识别的字符集,例如 Accept-Charset: utf-8
 
Accept-Encoding:浏览器可以处理的编码方式,注意这里的编码方式有别于字符集,这里的编码方式通常指gzip,deflate等。
                 例如 Accept-Encoding: gzip, deflate
 
Accept-Language:浏览器接收的语言,其实也就是用户在什么语言地区,例如简体中文的就是 Accept-Language: zh-CN
 
Authorization:在HTTP中,服务器可以对一些资源进行认证保护,如果你要访问这些资源,就要提供用户名和密码,这个用户名和密码就是在Authorization
              头中附带的,格式是“username:password”字符串的base64编码,例如:Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==中,
              basic指使用basic认证方式, QWxhZGRpbjpvcGVuIHNlc2FtZQ==使用base64解码就是“Aladdin:open sesame”
 
Cache-Control:这个指令在request和response中都有,用来指示缓存系统(服务器上的,或者浏览器上的)应该怎样处理缓存,因为这个头域比较重要,
               特别是希望使用缓 存改善性能的时候,内容也较多,所以我想在下一篇博文中主要介绍一下。
 
Connection:告诉服务器这个user agent(通常就是浏览器)想要使用怎样的连接方式。值有keep-alive和close。http1.1默认是keep-alive。keep-alive就是
            浏览器和服务器 的通信连接会被持续保存,不会马上关闭,而close就会在response后马上关闭。但这里要注意一点,我们说HTTP是无状态的,跟
            这个是否keep-alive没有关系,不要认为keep-alive是对HTTP无状态的特性的改进。
 
Cookie:浏览器向服务器发送请求时发送cookie,或者服务器向浏览器附加cookie,就是将cookie附近在这里的。例如:Cookie:user=admin
 
Content-Length:一个请求的请求体的内存长度,单位为字节(byte)。请求体是指在HTTP头结束后,两个CR-LF字符组之后的内容,
               常见的有POST提交的表单数据,这个Content-Length并不包含请求行和HTTP头的数据长度。
 
Content-MD5:使用base64进行了编码的请求体的MD5校验和。例如:Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
 
Content-Type:请求体中的内容的mime类型。通常只会用在POST和PUT方法的请求中。例如:Content-Type: application/x-www-form-urlencoded
 
Date:发送请求时的GMT时间。例如:Date: Tue, 15 Nov 1994 08:12:31 GMT
 
Expect:指示需要使用服务器某些特殊的功能。(这个我不是很清楚)
 
From:发送这个请求的用户的email地址。例如:From: user@example.com
 
Host:被服务器的域名或IP地址,如果不是通用端口,还包含该端口号,例如:Host: www.some.com:182
 
If-Match:通常用在使用PUT方法对服务器资源进行更新的请求中,意思就是,询问服务器,现在正在请求的资源的tag和这个If-Match的tag相不相同,如果相同,
         则证明服务器上的这个资源还是旧的,现在可以被更新,如果不相同,则证明该资源被更新过,现在就不用再更新了(否则有可能覆盖掉其他人所做的更改)。
 
If-Modified-Since:询问服务器现在正在请求的资源在某个时间以来有没有被修改过,如果没有,服务器则返回304状态来告诉浏览器使用浏览器自己本地的缓存,
                   如果有修改过,则返回200,并发送新的资源(当然如果资源不存在,则返回404。)
 
If-None-Match:和If-Modified-Since用意差不多,不过不是根据时间来确定,而是根据一个叫ETag的东西来确定。关于etag我想在下一篇博客介绍一下。
 
If-Range:告诉服务器如果这个资源没有更改过(根据If-Range后面给出的Etag判断),就发送这个资源中在浏览器缺少了的某些部分给浏览器,
          如果该资源以及被修改过,则将整个资源重新发送一份给浏览器。
 
If-Unmodified-Since:询问服务器现在正在请求的资源在某个时刻以来是否没有被修改过。
 
Max-Forwards:限制请求信息在代理服务器或网关中向前传递的次数。
 
Pragma:好像只有一个值,就是:no-cache。Pragma:no-cache 与cache-control:no-cache相同,只不过cache-control:no-cache是http1.1专门指定的,
        而Pragma:no-cache可以在http1.0和1.1中使用
 
Proxy-Authorization:连接到某个代理时使用的身份认证信息,跟Authorization头差不多。例如:Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
 
Range:在HTTP头中,"Range"字眼都表示“资源的byte形式数据的顺序排列,并且取其某一段数据”的意思。Range头就是表示请求资源的从某个数值到某个数值间的数据,
       例如:Range: bytes=500-999 就是表示请求资源从500到999byte的数据。数据的分段下载和多线程下载就是利用这个实现的。
 
Referer:指当前请求的URL是在什么地址引用的。例如在www.a.com/index.html页面中点击一个指向www.b.com的超链接,
         那么,这个www.b.com的请求中的Referer就是www.a.com/index.html。通常我们见到的图片防盗链就是用这个实现的。
 
Upgrade:请求服务器更新至另外一个协议,例如:Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
 
User-Agent:通常就是用户的浏览器相关信息。例如:User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0
 
Via:用来记录一个请求经过了哪些代理或网关才被送到目标服务器上。例如一个请求从浏览器出发(假设使用http/1.0),发送给名为 SomeProxy的内部代理,
    然后被转发至www.somenet.com的公共代理(使用http/1.1),最后被转发至目标服务器www.someweb.com,那么在someweb.com中收到的via 头应该是:
    via:1.0 someProxy 1.1 www.someweb.com(apache 1.1)
 
Warning:记录一些警告信息。

------------通用但非标准的HTTP头(通常,非标准的头域都是用“X-”开头,例如"x-powered-by")------------

1
2
3
4
5
6
7
8
9
10
11
12
13
X-Requested-With:主要是用来识别ajax请求,很多javascript框架会发送这个头域(值为XMLHttpRequest)
 
DNT:DO NOT TRACK的缩写,要求服务器程序不要跟踪记录用户信息。DNT: 1 (开启DNT) DNT: 0 (关闭DNT)火狐,safari,IE9都支持这个头域,
    并且于2011年3月7日被提交至IETF组织实现标准化
 
X-Forwarded-For:记录一个请求从客户端出发到目标服务器过程中经历的代理,或者负载平衡设备的IP。
 
X-Forwarded-Proto:记录一个请求最初从浏览器发出时候,是使用什么协议。因为有可能当一个请求最初和反向代理通信时,是使用https,
                   但反向代理和服务器通信时改变成http协议,这个时候,X-Forwarded-Proto的值应该是https
 
Front-End-Https:微软使用与其负载平衡的一个头域。
 
X-ATT-DeviceId:AT&A的产品中使用的头域,不过不是很清楚用途。

------------------曾经注释"proxy_set_header X-Forwarded-Proto https;"的一个坑-------------------
访问问卷系统https://wj.wang.com/limesurvey/index.php/admin/authentication/sa/login,登录的时候自动跳转到了公司官网首页。
这个问卷系统只能通过https方式访问,http方式访问的都自动跳转到公司官网。

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
原因就是因为在nginx代理层注释了这个参数:
[root@nginx-web01 vhosts]# cat ssl-wj.conf
upstream ssl-wj {
    server 192.168.1.22:10086 max_fails=3 fail_timeout=10s;
}
 
server {
   listen 443;
   server_name wj.wang.com;
   ssl on;
 
   ### SSL log files ###
   access_log logs/wj_access.log;
   error_log logs/wj_error.log;
 
### SSL cert files ###
   ssl_certificate ssl/wang.cer;     
   ssl_certificate_key ssl/wang.key;  
   ssl_session_timeout 5m;
 
   location / {
   proxy_pass http://ssl-wj/;                                     
   proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
   proxy_set_header Host $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 https;                                 //打开这个参数的注释即可
   proxy_redirect off;
   }
}

=======================================================================
如下Nginx代理转发需求:
访问http://grace.kevin.com的80端口的请求,转发至后端192.168.10.173和192.168.10.174的80端口(这部分是静态页的请求)。
当访问地址中匹配/leasecore/、/cms/api/、/cap/api/、/crm/api/、/cms/third/的上下文path时,转发至192.168.10.173和192.168.10.173的8080端口(tomcat处理)

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
155
LB层的Nginx转发配置:
===============================================================
[root@inner-lb01 vhosts]# cat grace.kevin.com.conf
upstream zl-80 {
      server 192.168.10.173:80 max_fails=3 fail_timeout=15s;
      server 192.168.10.174:80 max_fails=3 fail_timeout=15s;
}
 
upstream leasecore {
      server 192.168.10.173:8080 max_fails=3 fail_timeout=15s;
      server 192.168.10.174:8080 max_fails=3 fail_timeout=15s;
}
 
upstream cms-api {
      server 192.168.10.173:8080 max_fails=3 fail_timeout=15s;
      server 192.168.10.174:8080 max_fails=3 fail_timeout=15s;
}
 
upstream cap-api {
      server 192.168.10.173:8080 max_fails=3 fail_timeout=15s;
      server 192.168.10.174:8080 max_fails=3 fail_timeout=15s;
}
 
upstream crm-api {
      server 192.168.10.173:8080 max_fails=3 fail_timeout=15s;
      server 192.168.10.174:8080 max_fails=3 fail_timeout=15s;
}
 
upstream cms-third {
      server 192.168.10.173:8080 max_fails=3 fail_timeout=15s;
      server 192.168.10.174:8080 max_fails=3 fail_timeout=15s;
}
           
   server {
      listen      80;
      server_name grace.kevin.com;
     
      access_log  /data/nginx/logs/grace.kevin.com-access.log main;
      error_log  /data/nginx/logs/grace.kevin.com-error.log;
 
   location / {
         proxy_pass http://zl-80;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_read_timeout 600;
         proxy_ignore_client_abort on;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
         proxy_max_temp_file_size 128m;
        }
 
   location ^~ /leasecore/ {
         proxy_pass http://leasecore;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_read_timeout 600;
         proxy_ignore_client_abort on;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
         proxy_max_temp_file_size 128m;
        }
 
 
   location ^~ /cms/api/ {
         proxy_pass http://cms-api;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_read_timeout 600;
         proxy_ignore_client_abort on;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
         proxy_max_temp_file_size 128m;
        }
 
   location ^~ /cap/api/ {
         proxy_pass http://cap-api;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_read_timeout 600;
         proxy_ignore_client_abort on;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
         proxy_max_temp_file_size 128m;
        }
 
   location ^~ /crm/api/ {
         proxy_pass http://crm-api;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_read_timeout 600;
         proxy_ignore_client_abort on;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
         proxy_max_temp_file_size 128m;
        }
 
   location ^~ /cms/third/ {
         proxy_pass http://cms-third;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_read_timeout 600;
         proxy_ignore_client_abort on;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
         proxy_max_temp_file_size 128m;
        }
                       
}
 
 
后端192.168.10.173和192.168.10.174的nginx配置
==============================================================
[root@zl-app02 ~]# cat /data/nginx/conf/vhosts/uatzl-zpp.conf
   server {
      listen      80;
      server_name uatzl-app02.kevin.cn;
     
      access_log  /data/nginx/logs/access.log main;
      error_log  /data/nginx/logs/error.log;
     
 
   location / {
      root   /data/lease_app/dist;
      index  index.html index.htm;
   }
                       
}
 
[root@zl-app02 ~]# ps -ef|grep tomcat
app      28224     1  0 Dec13 ?        00:06:00 /data/jdk1.8.0_121/bin/java -Djava.util.logging.config.file=/data/tomcat-8.0.47/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Djava.endorsed.dirs=/data/tomcat-8.0.47/endorsed -classpath /data/tomcat-8.0.47/bin/bootstrap.jar:/data/tomcat-8.0.47/bin/tomcat-juli.jar -Dcatalina.base=/data/tomcat-8.0.47 -Dcatalina.home=/data/tomcat-8.0.47 -Djava.io.tmpdir=/data/tomcat-8.0.47/temp org.apache.catalina.startup.Bootstrap start
root     32535 32509  0 14:38 pts/1    00:00:00 grep tomcat

=======================================================================

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
顺便贴个配置案例:
比如:访问http://www.kevin.com 跳转到http://192.168.1.20:9040/portal-pc/
 
upstream scf_cluster {
    ip_hash;
    server 192.168.1.20:9020;
    server 192.168.1.21:9020;
    }
upstream portal_cluster {
    ip_hash;
    server 192.168.1.20:9040;
    server 192.168.1.21:9040;
    }
upstream file_cluster{
    ip_hash;
    server 192.168.1.20:9020;
    }
upstream workflow_cluster{
    ip_hash;
    server 192.168.1.20:9020;
    server 192.168.1.21:9020;
    }
upstream batch_cluster{
    server 192.168.1.20:9020;
    server 192.168.1.21:9020;
    }
upstream mobi_cluster{
    server 192.168.1.20:8080;
    }
 
server {
        listen       80;
        server_name  www.kevin.com;
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
 
        location /scf {
            proxy_pass http://scf_cluster/scf;
            proxy_redirect  http://scf_cluster/scf http://www.kevin.com/scf;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 
 
       location / {
            proxy_pass http://portal_cluster/portal-pc/;
            proxy_redirect  http://portal_cluster/portal-pc/ http://www.kevin.com/;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 
        location /msdp-file {
            proxy_pass http://file_cluster/msdp-file;
            proxy_redirect  http://file_cluster/msdp-file http://www.kevin.com/msdp-file;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
         
    location /upload {
            proxy_pass http://file_cluster/upload;
            proxy_redirect  http://file_cluster/upload http://www.kevin.com/upload;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
         
        location /activiti-workflow-console {
            proxy_pass http://workflow_cluster/activiti-workflow-console;
            proxy_redirect  http://workflow_cluster/activiti-workflow-console http://www.kevin.com/activiti-workflow-console;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    location /batch-framework-web {
            proxy_pass http://batch_cluster/batch-framework-web;
            proxy_redirect  http://batch_cluster/batch-framework-web http://www.kevin.com/batch-framework-web;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
}
 
server {
        listen       80;
        server_name  mobi.kevin.com;
    location / {
            proxy_pass http://mobi_cluster;
            proxy_redirect  http://mobi_cluster/ http://mobi.kevin.com/;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
server{
        listen       80;
        server_name  kevin.com;
        rewrite ^(.*)$ https://www.kevin.com$1 permanent;
 }

posted on 2018-07-26 16:57  五光十色  阅读(481)  评论(0编辑  收藏  举报

导航