nginx反向代理配置

location

location = / {
    # 精确匹配 / ,主机名后面不能带任何字符串
    [ configuration A ]
}

location / {
    # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
    # 但是正则和最长字符串会优先匹配
    [ configuration B ]
}

location /documents/ {
    # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
    # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
    [ configuration C ]
}

location ~ /documents/Abc {
    # 匹配任何以 /documents/Abc 开头的地址,匹配符合以后,还要继续往下搜索
    # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
    [ configuration CC ]
}

location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    # 匹配所有以 gif,jpg或jpeg 结尾的请求
    # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
    [ configuration E ]
}

location /images/ {
    # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
    [ configuration F ]
}

location /images/abc {
    # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
    # F与G的放置顺序是没有关系的
    [ configuration G ]
}

location ~ /images/abc/ {
    # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
    [ configuration H ]
}

location ~*/js/.*/\.js

  • =开头表示精确匹配
    如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。
  • ^~ 开头表示uri以某个常规字符串开头,不是正则匹配
  • ~ 开头表示区分大小写的正则匹配;
  • ~* 开头表示不区分大小写的正则匹配
  • / 通用匹配, 如果没有其它匹配,任何请求都会匹配到

顺序 no优先级:
(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

rewrite

syntax: rewrite regex replacement [flag]
Default: —
Context: server, location, if
  • 如果正则表达式(regex)匹配到了请求的URI(request URI),这个URI会被后面的_replacement_替换
  • _rewrite_的定向会根据他们在配置文件中出现的顺序依次执行
  • 通过使用_flag_可以终止定向后进一步的处理
  • 如果replacement以“https://”, “https://”, or “$scheme”开头,处理将会终止,请求结果会以重定向的形式返回给客户端(client)
  • 如果replacement字符串里有新的request参数,那么之前的参数会附加到其后面,如果要避免这种情况,那就在replacement字符串后面加上“?”,eg:
 rewrite ^/users/(.*)$ /show?user=$1? last;=
  • 如果正则表达式(regex)里包含“}” or “;”字符,需要用单引号或者双引号把正则表达式引起来

    可选的flag参数如下:

last

  • 结束当前的请求处理,用替换后的URI重新匹配location;
  • 可理解为重写(rewrite)后,发起了一个新请求,进入server模块,匹配location; 如果重新匹配循环的次数超过10次,nginx会返回500错误;
  • 返回302 http状态码 ;
  • 浏览器地址栏显示重地向后的url

break

  • 结束当前的请求处理,使用当前资源,不在执行location里余下的语句;
  • 返回302 http状态码 ;
  • 浏览器地址栏显示重地向后的url

redirect

  • 临时跳转,返回302 http状态码;
  • 浏览器地址栏显示重地向后的url

permanent

  • 永久跳转,返回301 http状态码;
  • 浏览器地址栏显示重定向后的url

flag标志位区别

last : 相当于Apache的[L]标记,表示完成rewrite

break : 停止执行当前虚拟主机的后续rewrite指令集

redirect : 返回302临时重定向,地址栏会显示跳转后的地址

permanent : 返回301永久重定向,地址栏会显示跳转后的地址

因为301和302不能简单的只返回状态码,还必须有重定向的URL,这就是return指令无法返回301,302的原因了。这里 last 和 break 区别有点难以理解:

last一般写在server和if中,而break一般使用在location中 last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配 break和last都能组织继续执行后面的rewrite指令

if指令与全局变量

if判断指令 语法为if(condition){…},对给定的条件condition进行判断。如果为真,大括号内的rewrite指令将被执行,if条件(conditon)可以是如下任何内容: 当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false

直接比较变量和内容时,使用=或!=

~正则表达式匹配,~*不区分大小写的匹配,!~区分大小写的不匹配

-f和!-f用来判断是否存在文件 -d和!-d用来判断是否存在目录 -e和!-e用来判断是否存在文件或目录 -x和!-x用来判断文件是否可执行

例如:

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite请求到/msid/目录下

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
} //如果cookie匹配正则,设置变量$id等于正则引用部分

if ($request_method = POST) {
    return 405;
} //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302

if ($slow) {
    limit_rate 10k;
} //限速,$slow可以通过 set 指令设置

if (!-f $request_filename){
    break;
    proxy_pass https://127.0.0.1;
} //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查

if ($args ~ post=140){
    rewrite ^ https://example.com/ permanent;
} //如果query string中包含"post=140",永久重定向到example.com

location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.jefflei.com www.leizhenfang.com;
if ($invalid_referer) {
    return 404;
} //防盗链
}

全局变量 下面是可以用作if判断的全局变量

$args : #这个变量等于请求行中的参数,同$query_string

$content_length : 请求头中的Content-length字段。

$content_type : 请求头中的Content-Type字段。

$document_root : 当前请求在root指令中指定的值。

$host : 请求主机头字段,否则为服务器名称。

$http_user_agent : 客户端agent信息

$http_cookie : 客户端cookie信息

$limit_rate : 这个变量可以限制连接速率。

$request_method : 客户端请求的动作,通常为GET或POST。

$remote_addr : 客户端的IP地址。

$remote_port : 客户端的端口。

$remote_user : 已经经过Auth Basic Module验证的用户名。

$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。

$scheme : HTTP方法(如http,https)。

$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。

$server_name : 服务器名称。

$server_port : 请求到达服务器的端口号。

$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。

$uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。

$document_uri : 与$uri相同。

proxy_pass

Syntax:    proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

不影响浏览器地址栏的url

设置被代理server的协议和地址,URI可选(可以有,也可以没有)

协议可以为http或https

地址可以为域名或者IP,端口可选;eg:

 proxy_pass https://localhost:8000/uri/;

如果一个域名可以解析到多个地址,那么这些地址会被轮流使用,此外,还可以把一个地址指定为 server group(如:nginx的upstream), eg:

upstream backend {
    server backend1.example.com
 weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;
 
    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {

  proxy_pass https://backend;
    }
}

server name, port, URI支持变量的形式,eg:

proxy_pass https://$host$uri;

这种情况下,nginx会在server groups(upstream后端server)里搜索server name,如果没有找到,会用dns解析

请求的URI按照下面的规则传给后端server

如果proxy_pass的URL定向里包括URI,那么请求中匹配到location中URI的部分会被proxy_pass后面URL中的URI替换,eg:

location /name/ {
    proxy_pass https://127.0.0.1/remote/;
}
请求https://127.0.0.1/name/test.html 会被代理到https://example.com/remote/test.html

location /name/ {
    proxy_pass https://127.0.0.1/remote;
}
请求https://127.0.0.1/name/test.html 会被代理到https://example.com/remotetest.html

location /name/ {
     proxy_pass https://127.0.0.1/;
}
请求https://127.0.0.1/name/test.html 会被代理到https://example.com/test.html

如果proxy_pass的URL定向里不包括URI,那么请求中的URI会保持原样传送给后端server,eg:

location /name/ {
    proxy_pass https://127.0.0.1;
}

请求https://127.0.0.1/name/test.html 会被代理到https://127.0.0.1/name/test.html

一些情况下,不能确定替换的URI

location里是正则表达式,这种情况下,proxy_pass里最好不要有URI

在proxy_pass前面用了rewrite,如下,这种情况下,proxy_pass是无效的,eg:


location /name/ {
    rewrite    /name/([^/]+) /users?name=$1 break;
    proxy_pass https://127.0.0.1;
}


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部