1. 前言
Nginx 反向代理配置,常用的有好几种方式。比如:proxy_pass、root、alias、rewrite。
通过这篇文章,你将了解他们的用法。
2. proxy_pass
proxy_pass 作用是==将匹配到的原始地址,反向代理到另外的 HTTP 地址==。
因此,proxy_pass 通常配置为目标 URL 地址。
配置方式有以下两种情况:
1.proxy_pass 后面只有 IP+端口,其他什么都没有,也不能以“/”结尾。此时代理的路径,是将请求路径 IP+端口后面的部分,追加到 proxy_pass 后面
如下配置,当我们请求 http://192.168.25.131:9003/first/a.html,实际nginx代理地址为http://192.168.25.131:8080/first/a.html
server {
listen 9003;
server_name 192.168.25.131;
location /first {
proxy_pass localhost:8080;
}
}
2.proxy_pass 后面除了 IP+端口,还有其他内容。此时的匹配逻辑,是将 location 未匹配到的内容,追加到 proxy_pass 后面
如下配置,当我们请求 http://192.168.25.131:9003/first2/a.html,实际nginx代理地址为http://192.168.25.131:8080/first/a.html
server {
listen 9003;
server_name 192.168.25.131;
location /first2 {
proxy_pass localhost:8080/first;
}
}
注:path 和 proxy_pass 的斜杠问题
其实不应当说是斜杠问题,应该说是 proxy_pass 除了 ip 和端口还有没有其他内容的问题!
这会导致最后形成的 url 不一样,因为启动了不同的规则,那么这和 path 有关吗?我们来验证一下!
1)path 没有斜杠
location /api1
localhost/api1/xxx -> localhost:8080/api1/xxx
location /api2
localhost/api2/xxx -> localhost:8080/xxx
location /api5
localhost/api5/xxx -> localhost:8080/haha/xxx,请注意这里的 haha 和 xxx 之间有斜杠。
2)path 有斜杠
location /api1/
localhost/api1/xxx -> localhost:8080/api1/xxx
location /api2/
localhost/api2/xxx -> localhost:8080/xxx
location /api5/
localhost/api5/xxx -> localhost:8080/hahaxxx,请注意这里的 haha 和 xxx 之间没有斜杠。
总结:
path 有无斜杠无影响,主要看 proxy_pass 最后有没有斜杠。
proxy_pass 没有的话,那么 proxy_pass+path;
proxy_pass 有的话(也就是说除了 ip 和端口还有其他内容),那么 proxy_pass+匹配到 path 路径后面的部分。
3. root
- root 的作用是将 location 的内容拼接到 root 后面
- root 指定的目录是上级目录,path 匹配的整个路径会追加,即 root+path;
- ==root 配置的是本地文件夹路径,而不是 http 路径==
如下配置,浏览器打开 http://192.168.25.131:9003/html2/index.html
实际请求的是 /usr/local/nginx/html2/index.html
server {
listen 9003;
server_name 192.168.25.131;
location /html2 {
root /usr/local/nginx/;
}
}
4. alias
- alias 作用是将请求地址剔除 location 配置的部分,然后拼接到 root 后面
- alias 指定的目录必须带/,path 匹配后面的内容会在 alias 指定的目录下查找,即 alias+匹配到 path 路径后面的部分。
如下配置,当请求地址是 http://192.168.25.131:9003/modules/order/index.html
实际访问路径是 /usr/local/nginx/html2/views/order/index.html
server {
listen 9003;
server_name 192.168.25.131;
location /modules/ {
alias /usr/local/nginx/html2/views/;
}
}
5. rewrite
- rewrite 作用是地址重定向,语法:rewrite regex replacement[flag];
- 根据 regex(正则表达式)匹配请求地址,然后跳转到 replacement,结尾是 flag 标记
例子 1:请求地址是 http://192.168.25.131:9003/baidu/开头的,都会跳转到百度
location /baidu/ {
rewrite ^/(.*) http://www.baidu.com/ permanent;
}
常用正则表达式:
rewrite 最后一项 flag 参数:
例子 2:将 http://192.168.25.131:9003/api/ 开头的地址,重定向到http://192.168.25.131:9003/*。也就是说,将中间的 /api 去掉
location /api/ {
rewrite /api/(.*) /$1 break;
proxy_pass http://192.168.25.131:9003;
}
( ) --用于匹配括号之间的内容,通过$1、$2
调用
6.总结三者的区别
记录一下 nginx 配置里面 alias / root / proxy_pass 的区别
1.alias
别名配置, 匹配到的 url 路径会被替换
location /test {
alias /img;
}
请求: /test/1.png
指向: /img/1.png
2.root
根路径配置, 匹配到的 url 会被拼接到 root 路径之后
location /test {
root /img;
}
请求: /test/1.png
指向: /img/test/1.png
3.proxy_pass
反向代理, 匹配到的 url 会被转发到 proxy_pass 的 url
location /test {
proxy_pass http://127.0.0.1:8082/img;
}
请求: /test/1.png