Search code examples
javaspring-bootspring-mvcnginx-reverse-proxyzk

zk spring boot behind a proxy ( nginx )


I created zk application using zk spring boot starter. Everything works correctly.

SpringBootApplication.java:

@SpringBootApplication
@Controller
public class SpringBootApplication {
    @GetMapping("${application.base-path:}/{page}")
    public String view(@PathVariable String page) {
        return page;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootBesGuiApplication.class, args);
    }
}

project's structure:

.....................
src/main/resources
 |
  -- web
      js
        - script1.js
        - script2.js
      - page1.zul
      - page2.zul
.....................

page1.zul:

...................
<?script src="~./js/script1.js"?>
...................

http://my-server:8081/page1 is displaying correctly.

And after that I tried to run it behind a proxy ( nginx ). And the page is displaying nothing. There are errors in a browser console:

GET http://my-server/zkau/web/162740bd/_zkiju-sapphire/zul/css/zk.wcs net::ERR_ABORTED 502 (Bad Gateway)    
........................................................................................
GET http://my-server/zkau/web/162740bd/js/script1.js net::ERR_ABORTED 502 (Bad Gateway)
........................................................................................

It seems that there are wrong url's generated for *.wcs, *.wpd and my *.js files. What am I doing wrong? How can I repair it?

nginx.conf:

server {
listen 80;
server_name my-server;

........................................
    
location / {
proxy_pass http://my-server:8080;
fastcgi_read_timeout 300;
}
    
location /app {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://my-server:8081;
fastcgi_read_timeout 300;
}
.............................................
}

application.properties:

application.base-path=/app

Solution

  • I resolve the issue by changing configs:

    application.properties

    server.servlet.context-path=/app
    

    nginx.conf

    ..........
    proxy_pass http://my-server:8081/app;
    ..........
    

    Thanks, @cor3000 and @protonchang. Yes, It's not ZK issue.