文章目录

  • 1、常用配置
  • 2、location:代理目录匹配
  • 2.1、location 反斜杠用法
  • 3、proxy_set_header :设置代理请求头
  • 4、proxy_pass:请求代理服务器
  • 5、rewrite :url重定向规则


1、常用配置

server {
    listen       80;
    server_name  localhost;  # 实际情况可以写IP, 域名、主机名    
    location / {
         index  index.html index.htm;
         proxy_set_header Host $host:$server_port;          
         proxy_pass   http://userServer:9000/data/;  
         rewrite ^/(.*)  /index.php?s=$1;    
    }
}

配置项很多,挑几个几个常用的配置依次介绍:location、proxy_set_header、proxy_pass 、rewrite

2、location:代理目录匹配

# 匹配所有根目录
location /

# 字符串匹配。 表示匹配所有“/static”开头的目录
location /static

# ~ 匹配符合表达式目录。比如代理目录中存在“static/(js|images)”的目录
location ~ /static/(js|images)/

# ~* 加上 * 表示不区分大小写
location ~* /static/(js|images)/

# = 表示精确匹配。 只有"/index"路径才会被代理,"/index/test"将不会被代理
location = /index

当然还有 !^ 等匹配。

2.1、location 反斜杠用法

两台nginx服务器

nginx A: 192.168.1.30

nginx B: 192.168.1.40

1) 测试方法

在nginx A 中配置不同的规则,向 nginx A 发送请求: http://192.168.1.30/foo/api

观察nginx B 收到的请求,具体操作是查看:http://$_SERVER['HTTP_HOST']$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];

2)测试过程及结果

统计1:

nginx A配置:

location /foo/ {
  	proxy_pass http://192.168.1.40/;
}

请求: http://192.168.1.30/foo/api

案例

location 后面的参数

proxy_pass

结果

1

/foo/

http://192.168.1.30/

http://192.168.1.40/api

2

/foo

http://192.168.1.30/

http://192.168.1.40//api

3

/foo/

http://192.168.1.30

http://192.168.1.40/foo/api

4

/foo

http://192.168.1.30

http://192.168.1.40/foo/api

统计2

nginx A配置:

location /foo/ {
  	proxy_pass http://192.168.1.40/bar/;
}

请求: http://192.168.1.30/foo/api

案例

location 后面的参数

proxy_pass

结果

5

/foo/

http://192.168.1.30/bar/

http://192.168.1.40/bar/api

6

/foo

http://192.168.1.30/bar/

http://192.168.1.40/bar//api

7

/foo/

http://192.168.1.30/bar

http://192.168.1.40/barapi

8

/foo

http://192.168.1.30/bar

http://192.168.1.40/bar/api

3)结果分析

按照 ip:port 后面是否接了字符串归为2类。说明:/ 也是字符串。

当 proxy_pass 的 ip:port 后面 没有接字符串 ,nginx 会将原请求路径原封不动地转交给下一站 nginx,如案例3和4。

当 proxy_pass 的 ip:port 后面 接了字符串ip:port/参数,即 ,nginx 会将 location的参数 从 原请求路径中剔除,再将剩余的字符串拼接到 ip:port/参数 的后面,生成 新请求路径,然后将 新请求路径 转交给下一站 nginx。比如,除了案例3,4外的所有案件。

比较难理解的例子:

案例7: proxy_pass 的 ip:port 后接了字符串 /bar,因此将 location:"/foo/" 从 原请求路径:"/foo/api" 中剔除,变为api,再将 api 拼接到 proxy_pass: http://192.168.1.30/bar 后面,生成了新请求 url:http://192.168.1.30/barapi,因此下一站的 nginx 收到的请求就是 /barapi

案例6: proxy_pass 的 ip:port 后接了字符串 /bar/ ,因此将 location:"/foo" 从 原请求路径 /foo/api 中剔除,变为 /api,再将 /api 拼接到proxy_pass: http://192.168.1.30/bar/ 后面,生成了 新请求路径:http://192.168.1.30/bar//api,因此下一站的nginx收到的请求就是 /bar//api

3、proxy_set_header :设置代理请求头

# 设置代理请求服务器请求头host
proxy_set_header Host $host
# 设置代理请求的ip地址
proxy_set_header X-Forwarded-Ip $remote_addr  
# 设置代理请求自定义数据
proxy_set_header test test

这里还有很多数据,不一一说明。

4、proxy_pass:请求代理服务器

# 从 “127.0.0.1”这台服务器收发数据,当然也可以直接写域名
proxy_pass http://127.0.0.1:8080

# 从服务端机器data目录收发数据
proxy_pass http://127.0.0.1:81/data;

# 动态配置数据,$scheme表示用户请求是http还是https,$host表示客户端请求头host,$args表示客户端请求参数
proxy_pass $scheme://$host/$request_uri/?$args

5、rewrite :url重定向规则

包含3个参数:

rewrite  匹配规则  重定向规则  重定向类型;

用法示例:

# /a.html 的时候,url重定向路径 /b.html 中
rewrite /a.html /b.html last;

# break 与 last的区别是,当存在多条rewrite规则时last会继续往下匹配,break不会继续往下匹配,而是将匹配到的重定向路径当做最终路径
rewrite  /a.html  /b.html  break;

# 当然重定向规则也是可以写正则表达式的 例如:/static/images/a.png => /local/images/a.png
rewrite ^/static/images/(.*)$  /local/images/$1  break;


# permanent 表示301重定向
rewrite /a.html  /b.html permanent;

# redirect 表示302重定向
rewrite /a.html  /b.html  redirect;

301重定向, 表示永久性重定向,对于SEO相较302来说比较友好,这里不做过多说明。