Hello all, I'm new to spring cloud, and met a problem with zuul, after redirecting, the url of js file didn't changed and could not be visited. Here are my zuul properties:
zuul:
addProxyHeaders: true
routes:
book:
path: /book/**
url: http://localhost:8090
zuul runs on http://localhost:8080, the book service runs on http://localhost:8090, it contains a index.html and a js file:
index.html
<html>
<body>
<script src="js/hello.js">
</script>
</body>
</html>
hello.js
document.write("hello world from hello.js!");
The problem is, if I goes to http://localhost:8090, it runs fine, but with http://localhost:8080/book, which was redirect by zuul, it failed, the error is 404, http://localhost:8080/js/hello.js not found, it seems that zuul haven't change the url.
I also found the same problem here: http://stackoverflow.com/questions/31085582/spring-boot-cloud-zuul-proxy-hardcoded-proxy-route-references-in-code, but it was not marked as solved. Are there any problems with my codes? I'm using spring cloud Brixton.RELEASE and spring boot 1.3.5 RELEASE.
Hello @ratoy,
the problem is, that you write in your HTML <script src="js/hello.js">
If the URL you are on is http://localhost:8080/book, then the browser tries to retrieve http://localhost:8080/js/hello.js and fails because the file isn't there(from the browser view this is absolutely correct behaviour).
One solution would be to write <script src="book/js/hello.js">, which should correctly resolve to http://localhost:8080/book/js/hello.js. Downside of this approach is, that the book service has to know, that he is running behind a zuul proxy and will not run without zuul anymore.
Don't know if there is a better solution for this problem.
Thanks for your reply @Koizumi85 , this solution works, but the html file could not be edited all the time, just to make sure the service name is right.
I think this is a common requirement, but it seems hard to solve it.
I got an idea this moment. You could move your javascript file to js/book/hello.js and add another route in zuul which also resolves to the book service like in the following example:
zuul:
routes:
book-service:
path: /book/**
url: http://localhost:8090
strip-prefix: true
book-js:
path: /js/book/**
url: http://localhost:8090
strip-prefix: false
If you then change your html file to
<html>
<body>
<script src="js/book/hello.js">
</script>
</body>
</html>
the browser would resolve the javascript file correctly over both ways: with and without zuul.
Thanks @Koizumi85 , it works, but the url of book-js should be changed to http://localhost:8090/js/book, this is much better than edit html, thank you!
you don't have to add the js/book/ in the url.
I added the "strip-prefix: false" part for that... so zuul does not remove the js/book/ part from the request url. It simply pass this through
ok, got it, thanks
it works for me thanks @Koizumi85
Closing this due to inactivity. Please re-open if there's more to discuss.
Most helpful comment
I got an idea this moment. You could move your javascript file to js/book/hello.js and add another route in zuul which also resolves to the book service like in the following example:
If you then change your html file to
the browser would resolve the javascript file correctly over both ways: with and without zuul.