官网:Beginner’s Guide

翻译部分:Setting Up a Simple Proxy Server

开始!

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实例上。


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 / {}
}

首先,添加多一个有以下内容的server块到nginx配置文件中来定义被代理服务器。


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.

这是一个监听8080端口的简单的服务器(自使用标准端口80后,才指定listen指令),且将所有请求映射到本地文件系统的/data/upl目录中。


Create this directory and put the index.html file into it.

创建/data/upl目录并在其中放置一个index.html文件。


Note that the root directive is placed in the server context.

请注意,root指令在server 块里面。


Such root directive is used when the location block selected for serving a request does not include its own root directive.

当匹配请求的location块没有包含它自己的root指令时,它会用server块中的root指令。


Next, use the server configuration from the previous section and modify it to make it a proxy server configuration.

接下来,修改上面的服务器配置来使它成为一个代理服务器配置。


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块中放置参数带有被代理服务器协议、名称和端口的proxy_pass指令

proxy_pass指令的参数:http://localhost:8080;

被代理服务器的协议:http

被代理服务器的名称:localhost

被代理服务器的端口:8080


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;
}

接着修改第二个location块 ,这个块映射前缀为/images/的请求/data/images目录下的文件。为了使它匹配带有常见扩展名的图像请求,修改第二个location块如下:

~ 表示 \.(gif|jpg|png)$ 是正则表达式,.gif  .jpg .png结尾的请求都会去到/data/images去找。


The parameter is a regular expression matching all URIs ending with .gif.jpg, or .png.

这个参数是一个匹配所有以.gif  .jpg  .png结尾的URI的正则表达式。


A regular expression should be preceded with ~.

一个正则表达式应该有~在前面。~和正则表达式中间由空格隔开。


The corresponding requests will be mapped to the /data/images directory.

相应的请求将映射到/data/images目录 。


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指令,并记住拥有最长前缀的那个location块,然后在检查正则表达式(~ xxx)


If there is a match with a regular expression, nginx picks this location or, otherwise, it picks the one remembered earlier.

如果有匹配到正则表达式,nginx会首先选择它,否则,nginx会选择刚刚记住的那个拥有最长前缀的location块。


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;}
}

代理服务器的配置如上。


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.

以.gif .jpg .png结尾的请求被映射到/data/images目录中,将其他所有请求转发到上面配置的被代理服务器(http://localhost:8080)上.


To apply new configuration, send the reload signal to nginx as described in the previous sections.

记得重启 nginx -s reload


There are many more directives that may be used to further configure a proxy connection.

3、nginx设置简单的代理服务器-阅读官方文档相关推荐

  1. Apache KafKa阅读官方文档心得

    Apache KafKa阅读官方文档心得 概念 Apache Kafka是一个分布式流媒体平台,流媒体平台有三个关键功能: 1.它允许您发布和订阅记录流.在这方面,它类似于消​​息队列或企业消息系统. ...

  2. pandas学习笔记——阅读官方文档

    1. 初始化 (1)生成简单序列pd.Series >>>s = pd.Series([1,3,5,np.nan,6,8]) >>>s 0 1.0 1 3.0 2 ...

  3. Spring Boot 2.0官方文档之 Actuator

    https://blog.csdn.net/alinyua/article/details/80009435 前言:本文翻译自Spring Boot 2.0.1.RELEASE官方文档,该Spring ...

  4. linux3.10.53编译,根据官方文档在Linux下编译安装Apache

    根据官方文档在Linux下编译安装Apache 前言 永远记住官方文档才是最准确的安装手册,这篇文章仅为对官方文档的解读和补充,学习提升务必阅读官方文档: http://httpd.apache.or ...

  5. iOS 9官方文档(翻译)

    iOS9已经发布一段时间了,我也在最近升级了Xcdoe 7.0正式版,升级后才发现又有了很多奇妙的变化,于是查看官方文档的一些解释,顺便做了一些翻译,和大家分享一下(转载请注明出处). iPad多任务 ...

  6. 什么!作为程序员你连英文版的官方文档都看不懂?

    目录 一.笔者英文基础介绍 二.为啥程序员需要阅读官方文档? 三.如何才能无障碍阅读英文文档? 四.坚持!坚持!坚持! 五.来个约定吧! 这篇文章不聊技术,我们来聊一个某种程度上比技术更重要的话题:一 ...

  7. MySQL8.0.28安装教程全程参考MySQL官方文档

    MySQL8.0.28详细安装教程.提供了Windows10下安装MariaDB与MySQL8.0同时共存的方法,以及Linux发行版Redhat7系列安装MySQL8.0详细教程.Windows10 ...

  8. tensorflow 官方文档中文版 tensorflow教程 tensorflow教学

    github链接:TensorFlow 最新官方文档中文版 文档链接:https://tensorflow.juejin.im/get_started/ 理论上来说,只要仔细阅读官方文档,便能对ten ...

  9. 总结Python设置Excel单元格样式的一切,比官方文档还详细。

    Python对Excel表格处理非常方便,本文专门对Excel单元格样式设置进行总结,日常用到的设置基本都可以用openpyxl库完成. 创建一个表格 openpyxl是第三方库,如果你还没有安装,输 ...

最新文章

  1. python学习之第四课时--运算符
  2. 文章推荐 | 城市规划中城市信息学的研究进展
  3. 在Win上做Python开发?当然是用官方的MS Terminal和VS Code了
  4. python中常用的模块二
  5. XVI Open Cup named after E.V. Pankratiev. GP of Eurasia
  6. 有趣的反直觉的“三门问题”
  7. 奇闻异事之NoSuchMethodError
  8. VC下Debug 和Release 区别【转】
  9. python连接redis002
  10. idea运行sql文件
  11. 数据库查询-分数排名
  12. mysql可以存储拼音吗_MySQL汉字变换拼音(存储函数)
  13. 如何在WPS中打开多个窗口
  14. 实例教程_次世代兽人制作教程
  15. 任正非讲话稿400篇_2021国家公务员考试笔试考情分析【申论篇】
  16. 计算机一级office软件,计算机一级office
  17. 什么是token token用在哪 token放在哪比较好
  18. STM32F103驱动SDIO wifi Marvell8801/Marvell88w8801 介绍(十一) ---- 编写LWIP DHCP server
  19. ORA-00934: group function is not allowed here
  20. S32K144 PWM实践

热门文章

  1. 硬件设计17之DCDC-MP2315解读
  2. Session与Cookie介绍
  3. OneNav一为主题魔改教程(三):给底部加上当前页面的消耗时间--洞五洞洞幺
  4. PCIe实践之路:BAR空间和TLP
  5. node.js v14.9.0以及v12.18.3两版本 百度网盘下载链接
  6. android火焰图分析,android实现简单的火焰效果
  7. 无服务计算的未来和挑战: A Berkeley View on Serverless Computing
  8. postman的完美替代品,超好用的api测试软件
  9. QII中的几个Warning的解决方法
  10. htc hd2刷android,真正的刷机之王! HTC HD2成功刷入安卓7.0