最近使用 Nginx 比较多,对 Nginx 的配置文件看的不是很明白,所以最近花了点时间,对 Nginx 相关文档进行了一番学习。

什么是 Nginx?

Nginx 是一个http和反向代理服务器,一个邮件代理服务器一个通用的 TCP/UDP 代理服务器。它可以搭建静态资源服务器、php web 应用,代理站点、反向代理站点。

正向代理和反向代理

一句话,正向代理隐藏真实客户端,反向代理隐藏真实服务端。
例子:翻墙软件就是正向代理,Nginx上将多个端口合并成一个端口访问就叫反向代理

怎么阅读官方文档

授人予鱼,不如授人予渔。其实,官方文档已经写得很详细了,只是因为我们很多人看到是英文文档没有耐心去仔细看而已。官方文档中有两个很重要的部分,一个是介绍参考文档,另一个是各模块的参考文档:介绍文档部分,主要介绍怎么安装、怎么开始使用 Nginx、命令行使用、配置文件的文档结构;模块的参考文档,主要是对 Nginx 的各个模块进行详细的介绍,比如:核心模(ngx_http_core_module)、重写模块(ngx_http_rewrite_module)、代理模块(ngx_http_proxy_module)、各种变量(host,host,host,hostname)

  • 如果只是想简单的使用一下 Nginx,那么我建议先读一下 Beginner’s Guide,通过这个文档你可以初步了解 Nginx 常用的命令,配置文件的结构及一些常用的配置指令。
  • 如果你想更进一步的了解,我建议除了阅读 Beginner’s Guide 外,还强烈建议读一下核心模块(ngx_http_core_module),常用的配置指令都是在这个核模块文档里,比如:server、location、root、alias。

常用指令介绍

location

官方文档地址:http://nginx.org/en/docs/http/ngx_http_core_module.html#location

# 语法
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }location @name { ... }
Default:    — # 默认值
Context:    server, location # 该指令可以出现在哪里

Sets configuration depending on a request URI.

The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “…”, and possible compression of two or more adjacent slashes into a single slash.

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

location blocks can be nested, with some exceptions mentioned below.

For case-insensitive operating systems such as macOS and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.

Regular expressions can contain captures (0.7.40) that can later be used in other directives.

If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.

In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” modifiers, the search also terminated and regular expressions were not checked.

大概意思:

  • 01.location 指令主要由修饰符(modifier)和 uri 组成;

  • 02.uri 不包括参数,比如:/a/b.html?id=1 的uri 为/a/b.html;

  • 03.uri 是 url 解码后的值,如果包含...、多个连续斜杠会被合并成一个斜杠;

  • 04.location uri 部分可以是前缀字符串(prefix string)也可以是正则表达式(regular expression)(人话就是: uri 可以是普通字符串或者是正则表达式),怎么知道它是正则表达式还是前缀字符串(prefix string)呢?主要通过修饰符来判断,修饰符为~~* 则为正则表达式,否则为前缀字符串(prefix string);

  • 05.执行过程:首先搜索前缀字符串(prefix string),并且暂存最长的匹配;然后(如果有正则表达式的话),继续按配正则表达式出现的顺序进行正则匹配(regular expression),如果正则全部无法匹配,就使用暂存的那个前缀匹配,一旦有正则匹配成功,就结束整个匹配过程并使用此正则匹配,所以正则匹配在配置文件中的顺序是要注意的。如果命中的最长的前缀字符串(prefix string)的修饰符为^~ 的,则不再进行正则匹配;如果是使用 = 修饰符的表示精确匹配,一旦配置就终止查找;

    • a. 首先检查使用前缀字符串定义的 location,其中,匹配的最长前缀 location 被选择同时被记住;
    • b. 其次,检查正则表达式 location ,按照它们出现的顺序进行检查,一旦命中终止检查;
    • c. 如果正则没有匹配到,返回之前被记住的前缀 location;
    • b. 如果匹配的最长前缀 location 的修饰符是 ^~ ,则不再进行正则匹配,并终止检查;
    • e. 如果匹配的最长前缀 location 的修饰符是 =,则进行精确匹配,如果匹配成功,终止查找;
  • 06.综上,修饰符的优先级为 = > ^~ > ~|~* >

  • 07.uri 的优先级只与长度有关于与位置无关;

  • 08.= 为精确匹配;

  • 09.~ 为区分大小写的正则表达式;

  • 10.~* 为不区分大小写的正则表达式。

root

Syntax:  root path;
Default:    root html;
Context:    http, server, location, if in location

指定请求的根目录,比如:

location /user/ {root /data/w3;
}

当请求URL 为 /user 时,返回 /data/w3/user 的内容。

alias

Syntax:  alias path;
Default:    —
Context:    location

给 location 指定一个别名并用别名替换 location,比如:

location /user/ {alias /data/w3;
}

当请求URL 为 /user 时,返回 /data/w3 的内容。
所以 alias 与 root 的区别在于:root 返回的是 root+location,alias 返回的只有 alias。

实例分析

案例1

location = / {[ configuration A ]
}location / {[ configuration B ]
}location /documents/ {[ configuration C ]
}location ^~ /images/ {[ configuration D ]
}location ~* \.(gif|jpg|jpeg)$ {[ configuration E ]
}
  • 1 “/” 请求会匹配 configuration A ,因为它精确匹配了;
  • 2 “/index.html” 请求会匹配 configuration B,因为符合最长匹配,并且没有命中任何正则表达式;
  • 3 “/documents/document.html” 请求会匹配 configuration C,同2;
  • 4 “/documents/1.jpg” 请求会匹配 configuration E,虽然最长匹配符合C,但命中了E的正则表达式,所以返回E;
  • 5 “/images/1.gif” 请求会匹配 configuration D, 符合最长匹配 D,修饰符为^~,不再进行正则表达式匹配,匹配终止,故符合D;
  • 6 如果想让“/documents/1.jpg”符合D怎么办?D 的规则改为:
location ^~ /documents/ {[ configuration C ]
}

案例2

如何通过Nginx解决ajax请求跨域问题?主要有三种方法:

第一种:修改后端代码,在response头部添加支持跨域标识

第二种:通过 Nginx 代理后给 response 头部添加支持跨域标识

server {listen 7111;server_name 127.0.0.1 192.10.170.111;location / {proxy_pass http://api.example.com;proxy_set_header Host $proxy_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_hide_header Access-Control-Allow-Origin;add_header Access-Control-Allow-Origin $http_origin;# 指定允许跨域的方法,*代表所有add_header Access-Control-Allow-Methods *;# 检查请求的类型是不是预检命令if ($request_method = OPTIONS) {return 200;}}
}

第三种:通过 Nginx 反向代理,把后端请求代理到站点的某个虚拟路径下

server{listen 6111;location /api {proxy_pass http://api.example.com;}
}

[1] Nginx

Nginx 入门之授人予鱼,不如授人予渔相关推荐

  1. cmd命令大全 授人予鱼不如授人与渔

    hello,我又来了今天讲的是cmd命令行大全,<授人予鱼不如授人与渔> 不会cmd命令的程序员不是个好程序员当然linux中的shell命令不一样下面就由 首先win7或者win10+R ...

  2. JS(受人以鱼 不如受人以渔)第十七课

    比你优秀的人都比你努力,你有什么理由不去努力.基础来自己的累秒累天累月的积累 没有一个人是从天而降的天才,也没有哪个人想做一个一生贫庸的人.今天我想说 受人以鱼 不如受人以渔 推荐几个官网可以自己主动 ...

  3. 授人予鱼不如授人予渔:零基础java学习路线分享

    一切不以求职找工作为目标的学习路线分享都是耍流氓. 博主是19年7月毕业的应届生:985.A+学科硕士.2018年的秋招收获了:百度.腾讯.头条.美团.猿辅导.度小满.猫眼.流利说等offer.off ...

  4. 授您以鱼,不如授您以渔

    罗列194种创意线索: 1.把它颠倒过来 2.把它摆平 3.把缩小 4.把颜色变换一下 5.把它变为圆形 6.使它更大 7.把它变为正方形 8.使它更小 9.使它更长 10.使它闪动 11.使它更短 ...

  5. 六月,授你以鱼,再授你以渔

    最近很少更新技术文,因为期末考试了,我是孔乙己,我现在很慌.... 熟悉我的人都知道,有时间我还是乐意写干货文章的,然而,尽管我经常分享一些自己觉得还不错的东西,但这并不是我最终的目的.授人以鱼不如授 ...

  6. nginx 启动命令_Nginx实战001:Window中配置使用Nginx入门

    什么是Nginx Nginx是一款灵活.稳定.高效.低消耗的轻量级Web服务器,支持HTTP和反向代理及电子邮件(IMAP/POP3/SMTP)等服务.它具的高性能.高并发.低内存消耗及开源免费让深受 ...

  7. Nginx 入门指南

    Nginx 入门指南 简介: Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强.本教程根据淘宝核心系统服务器平台组的 ...

  8. Linux之nginx入门

    nginx入门 详见可参考:https://www.cnblogs.com/tiger666/p/10239307.html?tdsourcetag=s_pctim_aiomsg 1. 常用的WEB框 ...

  9. Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例

    场景 Nginx入门简介和反向代理.负载均衡.动静分离理解 https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102790862 Ub ...

最新文章

  1. 最大的ai计算机模型,Microsoft构建了世界排名前五的超级计算机,用于在Azure上训练大型AI模型...
  2. 前去哪儿产品总监白羽:做SaaS产品需要注意哪些坑?
  3. linux下mysql无法看到3306端口监听
  4. 字符数组、字节数组、字符串转换
  5. JSTL标签库中fmt标签,日期,数字的格式化
  6. ClickHouse Keeper 源码解析
  7. 日日行,不怕千万里;常常做,不怕千万事
  8. django,项目,app,静态文件02,数据库
  9. 【限时免费】从入门到实战,5节课玩转Kafka!赢音箱、书籍好礼!
  10. win10改成ahci后无法开机怎么办,win10开不了机
  11. aws beanstalk mysql_AWS Beanstalk搭建WordPress站点
  12. 我是如何学习Android源码的
  13. 集体智慧编程--优化
  14. 测试小兵成长记:柳暗花明又一村
  15. 我对管理和领导的理解
  16. python智能抠图
  17. 自封装验证手机号码、邮箱格式、身份证号的工具
  18. 海思开发板遇到的问题启发性的链接
  19. gettimeofday 函数
  20. qt中各种类型转成uchar

热门文章

  1. 电子发票的板式文件服务器地址是什么,税务Ukey电子发票配置菜单参数设置操作指南.pdf...
  2. 程序员做饭指南-食品安全
  3. 基于Java (spring-boot)和微信小程序的果蔬商城微信小程序(毕业设计优秀论文)
  4. 公众号被关注后自动回复两条消息,文字+图片是如何实现的?
  5. Winform小软件 —— 摇奖机
  6. BOC-PEG叶酸,BOC-NH-PEG-FA
  7. python创建画布的函数_使用Python的turtle(海龟)模块画图
  8. 仿二手车之家下拉列表
  9. 别再全网找了,这四款良心软件,还你一个清爽的电脑桌面
  10. 【信号分解】基于LMD算法和ELMD算法实现管道泄漏信号处理附matlab代码