您的位置:首页 > 运维架构 > 反向代理

nginx--反向代理配置(主要配置server)

2020-06-05 10:49 615 查看

案例1

访问http://localhost:8090时,重定向到http://localhost:8080

server {
#监听本地8090端口
listen       8090;
server_name  localhost;

location / {
proxy_pass http://localhost:8080/;
root   html;
index  index.html index.htm;
}

# 重定向服务错误页面到静态页面50x.html
# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}

案例2

① 访问http://localhost:9001/dev时,重定向到http://localhost:8080
② 访问http://localhost:9001/dev/swagger-ui.html时,重定向到http://192.168.1.111:8081/swagger-ui.html
③ 访问http://localhost:9001/test/swagger-ui.html时,重定向到http://192.168.1.112:8081/swagger-ui.html

server{
#监听本地8090端口
listen       9001;
server_name  localhost;

#  =开头 表示精确匹配,只有完全匹配上才能生效
location =/dev {
#被代理的url
proxy_pass http://localhost:8080/;
}

# ^~开头 表示对URL路径进行前缀匹配,并且在正则之前
location  ^~/dev {
proxy_pass http://192.168.1.111:8081/;
}
location ^~/test {
proxy_pass http://192.168.1.112:8081/;
}

#不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location /pro{
proxy_pass http://192.168.1.113:8081/;
}
}

特别注意

当访问http://192.168.1.111:8081/dev/index.html时

代理到URL:http://127.0.0.1:8082/index.html

location /dev/ {
#proxy_pass中最后以/结尾
proxy_pass http://127.0.0.1:8082/;
}

代理到URL:http://127.0.0.1:8082/dev/index.html

location /dev/ {
#proxy_pass中最后不是以/结尾
proxy_pass http://127.0.0.1:8082;
}

代理到URL:http://127.0.0.1:8082/aaa/index.html

location /dev/ {
proxy_pass http://127.0.0.1:8082/aaa/;
}

代理到URL:http://127.0.0.1:8082/aaaindex.html

location /dev/ {
proxy_pass http://127.0.0.1:8082/aaa;
}

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,实际访问的路径中包含匹配的路径。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: