1. 程式人生 > >Nginx學習筆記——配置簡單代理伺服器

Nginx學習筆記——配置簡單代理伺服器

以下內容翻譯自官方文件,原文地址
原標題:Setting Up a Simple Proxy Server

Nginx的一個常見用途是設定為代理伺服器,所謂代理,就是接收客戶端請求,然後傳遞給被代理的伺服器並獲得響應,再返回給客戶端。
One of the frequent uses of nginx is setting it up as a proxy server, which means a server that receives requests, passes them to the proxied servers, retrieves responses from them, and sends them to the clients.

我們將要配置一個基本的代理伺服器,它會用本地目錄來響應所有的圖片請求,其他請求都會轉發給被代理的伺服器。在這個例子中,伺服器和代理伺服器都用同一個nginx伺服器實現。
We will configure a basic proxy server, which serves requests of images with files from the local directory and sends all other requests to a proxied server. In this example, both servers will be defined on a single nginx instance.

首先,在nginx的配置檔案中增加一個包含如下內容的server塊,定義一個被代理的伺服器。
First, define the proxied server by adding one more server block to the nginx’s configuration file with the following contents:

server {
    listen 8080;
    root /data/up1;

    location / {
    }
}

這是一個簡單的伺服器,監聽8080埠(之前,listen指令沒有指定,因為使用了標準80埠)並將所有請求對映到本地檔案系統的/data/up1目錄。


This will be a simple server that listens on the port 8080 (previously, the listen directive has not been specified since the standard port 80 was used) and maps all requests to the /data/up1 directory on the local file system.

建立這個目錄,把index.html檔案放在目錄下。
Create this directory and put the index.html file into it.

注意,root指令被放在了server上下文。
Note that the root directive is placed in the server context.

如果被選中用來處理請求的location塊沒有自己的root指令,就會使用這個root指令。
Such root directive is used when the location block selected for serving a request does not include own root directive.

然後,使用上一節使用的server配置,修改一下作為代理伺服器配置。
Next, use the server configuration from the previous section and modify it to make it a proxy server configuration.

在第一個location塊,加入一個proxy_pass指令,指明被代理伺服器指定的協議、機器名、埠號(在本例中,使用了http://localhost:8080
In the first location block, put the proxy_pass directive with the protocol, name and port of the proxied server specified in the parameter (in our case, it is http://localhost:8080):

server {
    location / {
        proxy_pass http://localhost:8080;
    }

    location /images/ {
        root /data;
    }
}

第二個location塊,現在將/images/包含字首的請求對映到/data/images目錄下的檔案,我們修改一下,讓它匹配包含特定副檔名的請求。修改後的location塊如下:
We will modify the second location block, which currently maps requests with the /images/ prefix to the files under the /data/images directory, to make it match the requests of images with typical file extensions. The modified location block looks like this:

location ~ \.(gif|jpg|png)$ {
    root /data/images;
}

引數是正則表示式,匹配所有以 .gif、.jpg或.png結尾的URI。
The parameter is a regular expression matching all URIs ending with .gif, .jpg, or .png.

正則表示式需要前置一個~符號。
A regular expression should be preceded with ~.

請求會被對映到/data/images目錄。
The corresponding requests will be mapped to the /data/images directory.

當nginx選擇location塊來處理請求時,它首先檢查指定了字首的location指令,記住有最長字首的location,然後檢查正則表示式。
When nginx selects a location block to serve a request it first checks location directives that specify prefixes, remembering location with the longest prefix, and then checks regular expressions.

如果有匹配的正則表示式,nginx選擇這個location,否則,選擇前面記住的那個location。
If there is a match with a regular expression, nginx picks this location or, otherwise, it picks the one remembered earlier.

代理伺服器的最終配置如下:
The resulting configuration of a proxy server will look like this:

server {
    location / {
        proxy_pass http://localhost:8080/;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}

這個伺服器會過濾以 .gif、 .jpg、 或者 .png結尾的請求,將他們對映到/data/images目錄(就是把root引數插入到URI中)然後將所有其他請求傳遞給上面配置好的被代理的伺服器。
This server will filter requests ending with .gif, .jpg, or .png and map them to the /data/images directory (by adding URI to the root directive’s parameter) and pass all other requests to the proxied server configured above.

要應用最新的配置,就像上一節介紹的,傳送reload指令給nginx主程序。
To apply new configuration, send the reload signal to nginx as described in the previous sections.

還有很多指令可以用作配置代理伺服器。
There are many more directives that may be used to further configure a proxy connection.