Nginx学习笔记——配置简单代理服务器

本文介绍了如何配置Nginx作为代理服务器,通过设置server块和location块,实现图片请求本地处理,其他请求转发至指定服务器。通过proxy_pass指令指定被代理服务器,并通过正则表达式匹配特定文件扩展名的请求。
摘要由CSDN通过智能技术生成

以下内容翻译自官方文档,原文地址
原标题: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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值