因为当前不少应该都是先后端分离了,同时大量的基于http的分布式和微服务架构,使得不少时候应用和不一样项目组之间的系统相互来回调用,关系复杂。若是使用传统的作法,都在应用中进行各类处理和判断,不只维护复杂、容易出错,还大大增长开发、调试的工做量,在nginx中,有很多的非功能类实际上是能够帮咱们处理掉的,因此,对于现代开发人员来讲,有必要对nginx的location比较熟悉,以便达到事半功倍的效果,好比说,平常的图片上传就是个例子,咱们能够将图片上传到特定的目录,而后配置nginx对于用户上传的图片,都转发到特定的目录,该目录不必定是nginx的html目录,甚至是挂载的盘,这样对于通常的应用来讲,既能够按应用规划设置文件服务器,也避免了须要安装和维护ftp服务器软件的工做。php
由于Nginx是模块化架构,每一个模块都会有一系列本身引入的指令,这些指令一般包含在指令块中,好比events模块,就有一个events块。以下所示:html
events { worker_connections 1024; }
对于最经常使用的部分,指令块一般层层嵌套。例如:linux
http { server { listen 80; server_name example.com; access_log /var/log/nginx/example.com.log; location ^~ /admin/ { index index.php; } } }
默认状况下,之块会继承父块中声明的设置,除非明确覆盖。nginx
在nginx的配置中,语法比较复杂,并且不一样的指令,可能规则彻底不一样。git
好比root仅接受一个字符,声明服务于网站的文件的根路径。github
模块中一般定义了能够用于指令中的变量,变量以$开头。某些指令中不容许使用变量,好比error_log,此时它会被当作字面量处理。web
指令的值能够带双引号、带单引号、不带引号,除非使用了特殊符号,此时须要用引号括起来以免nginx解析误解,对于特殊符号须要当作字面量使用的,须要用\,好比$。正则表达式
nginx的基本模块包括Core、Events(主要是声明网络机制,某些参数对系统的性能影响较大)、Configuration,这三个模块提供了整个基础架构。spring
nginx使用多进程架构。sql
核心模块的主要指令:
events模块的主要指令包括(这些指令必须声明在events块中):
配置模块的主要指令包括:
HTTP Core模块包含了HTTP服务器的全部基础块、指令以及变量,其默认启用,实际上它也是最重要的一个模块。它包含三个主要的块:http,server,location。
一个典型的http配置结构以下:
主要指令包括:
路径相关指令包括:
客户端请求相关的指令包括:
限制相关的指令包括:
location /admin/ { limit_except GET { allow 192.168.1.0/24; deny all; } }
格式为:
limit_except METHOD1 [METHOD2…] { allow | deny | auth_basic | auth_basic_user_file | proxy_pass | perl; }
文件和缓存相关的指令:
其余指令:
HTTP Core模块包含了不少的变量,分为三类:第一类是在Http请求头中传递的,第二类是http响应头中的,第三类是彻底nginx生成的。参考nginx http server第三版 90页。
nginx容许用户声明样式匹配指定的uri,location的语法为:
location [=|~|~*|^~|@] pattern { ... }
第一个可选的参数是修饰符,各修饰符详解以下:
server { server_name website.com; location ~ ^/abcd$ { […] } }
不少时候,咱们定义的不止一个location,一般至少会有两个,一个是根自己,一个指向后端服务。因此咱们须要理解nginx接收到一个请求以后,它如何肯定匹配的location。定义在配置文件中的location顺序对于一个请求是否优先匹配没有关系,nginx搜索匹配的样式的顺序以下:
这个模块的目的就是为了URL重写,URL重写是SEO的关键元素之一。URL重写由rewrite指令执行,它接收一个样式和一个替换URI。
正则表达式规则参考nginx http server第三版P103。
注意,由于正则表达式的{}和nginx指令块冲突,因此若是要使用,必须放到引号中。
捕获,正则表达式中用()括起来的内容会被捕获到一个个内置变量中,$N,N为捕获的索引,从1开始。捕获的变量能够做为指令的值。()也一般和|一块儿使用,二选一。命名捕获使用?<name>语法设置,例如^/(?<folder>[^/]+)/(?<file>.*)$。
在nginx中,在正则表达式中捕获的值,能够在后续指令中使用,只要不被覆盖便可。
server { server_name website.com; location ~* ^/(downloads|files)/(.*)$ { add_header Capture1 $1; add_header Capture2 $2; } }
nginx区份内外部请求,内部请求由error_page, index, rewrite,try_files, add_before_body, add_after_body生成。内部请求还分两类:
简单的重定向以下:
server { server_name website.com; root /var/www/vhosts/website.com/httpdocs/; location /storage/ { internal; alias /var/www/storage/; } location /documents/ { rewrite ^/documents/(.*)$ /storage/$1; } }
Rewrite模块的指令包括:
若是声明的URI以http://开头,nginx自动使用redirect标志。
可应用于server,location,if。
任何以_pass结尾的指令都接受到一组服务器的引用。声明一组服务器的第一步是在http的upstream块内声明一个或多个server指令,以下:
http { upstream MyUpstream { server 10.0.0.201; server 10.0.0.202; server 10.0.0.203; } […] }
而后在server块内引用声明的upstream,以下:
server { server_name example.com; listen 80; root /home/example.com/www; # Proxy all requests to the MyUpstream server group proxy_pass http://MyUpstream; […] }
nginx提供多种负载均衡机制,P248。从Nginx 1.9.0开始,新增的Stream模块支持TCP负载均衡,这意味原来必须使用LVS或者HAPROXY做为负载均衡机制的模式能够采用NGINX了。
要启用线程池,必须使用--withthreads参数编译nginx,对于常常文件下载的应用,应使用以下配置:
location /downloads/ { aio threads; directio 8k; sendfile on; }
ngx_http_log_module模块负责以声明的格式记录请求日志。
其主要的两个指令是:
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off;
默认为access_log logs/access.log combined;
首先参考centos下nginx安装与配置安装所需模块。
If you are using Openresty, or have the ngx_lua
module and ngx_devel_kit
module installed, you are in luck.
You first need to declare what variables you'll be needing somewhere in your nginx.conf
file using the env
directive:
env API_KEY;
After that, when you want to access the environment variable, you can use a combination of set_by_lua
and os.getenv
, like this:
http { ... server { location / { set_by_lua $api_key 'return os.getenv("API_KEY")'; ... } } }
In this example we are assigning the environment variable to one of Nginx variables; we can use $api_key
as a regular nginx.conf
variable.
Using Lua was our preferred approach, since we have OpenResty. If you can't use Lua, a second solution involves using Perl. The first part is similar; you must declare the variables he uses using env
:
env API_KEY;
After that, you can combine perl_set
and some Perl to do the same thing as before:
http { ... server { location / { perl_set $api_key 'sub { return $ENV{"API_KEY"}; }'; ... } } }
You will need to have the ngx_http_perl_module
module enabled in order to be able to use this technique.