Mastering Nginx 读书笔记二-----反向代理服务器基础

NGINX as a Reverse Proxy


A reverse proxy is a web server that terminates connections with clients and

makes new ones to upstream servers on their behalf. An upstream server is

defined as a server that NGINX makes a connection with in order to fulfill the

client's request.



The most important directive when proxying to an upstream server is the  proxy_

pass directive. This directive takes one parameter―the URL to which the request

should be transferred. Using  proxy_pass with a URI part will replace the  request_

uri with this part. For example,  /uri in the following example will be transformed

to  /newuri when the request is passed on to the upstream:

请求的uri的部分将会被proxy_pass 后面的URL的uri部分替换,然后传递到上游服务器

location /uri {

proxy_pass http://localhost:8080/newuri;

}



There are two exceptions to this rule, however. First, if the location is defined

with a regular expression, no transformation of the URI occurs. In this example,

the URI  /local will be passed directly to the upstream, and not be transformed

to  /foreign as intended:

location 是正则匹配的话,请求的uri不会被转换,然后传递到上游服务器

location ~ ^/local {

proxy_pass http://localhost:8080/foreign;

}

The second exception is that if within the location a rewrite rule changes the URI,

and then NGINX uses this URI to process the request, no transformation occurs.

In this example, the URI passed to the upstream will be  /index.php?page=<match> ,

with  <match> being whatever was captured in the parentheses, and not  /index , as

indicated by the URI part of the  proxy_pass directive:

location中包含重写规则的话,就是用重写规则的uri,不会被proxy_pass 转换,然后传递到上游服务器

location / {

rewrite /(.*)$ /index.php?page=$1 break;

proxy_pass http://localhost:8080/index;

}




你可能感兴趣的:(nginx)