Nginx学习笔记(四)暴露一个端口访问多个Spring Boot服务,JS/CSS 404问题解决

1.场景

需求:
1.系统包含多个服务,彼此之间需要互相访问;
2.三个服务占用三个不同的端口,8081、8082、8083;
3.服务器只对外暴露一个端口。

2. 原设计及配置

在这里插入图片描述

Nginx配置nginx.conf

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

	upstream auth {
		server localhost:8081;
	}
	upstream business {
		server localhost:8082;
	}
	upstream file {
		server localhost:8083;
	}
	
    sendfile        on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		location / {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://localhost:8080;
		}
		
		location = /auth {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://auth;
		}
		location = /business {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://business;
		}
		location = /file {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://file;
		}
    }
}

3.问题

JS/CSS文件链接丢失
在这里插入图片描述

4.修改后设计及配置

在这里插入图片描述

1.Spring Boot 增加前缀的方式:配置server.servlet.context-path

server:
  port: 8081
  servlet:
    context-path: /auth

2.所有的js请求的接口要改为相对路径,例如:“/user/info” 要修改为 “user/info

3.Nginx配置nginx.conf

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

	upstream auth {
		server localhost:8081;
	}
	upstream business {
		server localhost:8082;
	}
	upstream file {
		server localhost:8083;
	}
	
    sendfile        on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		location / {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://localhost:8080;
		}
		
		location = /auth {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://auth/auth;
		}
		location ^~ /auth/ {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://auth/auth/;
		}
		location = /business {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://business/business;
		}
		location ^~ /business/ {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://business/business/;
		}
		location = /file {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://file/file;
		}
		location ^~ /file/ {
			proxy_set_header        Host $http_host;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_pass		http://file/file/;
		}
    }
}

修改后重试,页面可以正常加载,问题修复,完结撒花~
在这里插入图片描述

5.补充

nginx.conf= /auth^~ /auth^~/auth/ 在匹配上有什么区别?

  • = /auth仅匹配: ip:8080/auth
  • ^~ /auth前缀匹配,匹配示例:ip:8080/auth、ip:8080/author、ip:8080/auth/user…
  • ^~/auth/前缀路径匹配,匹配示例:ip:8080/auth/user、ip:8080/auth/all…
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值