zk spring boot在代理(nginx)后面

huangapple go评论135阅读模式
英文:

zk spring boot behind a proxy ( nginx )

问题

我使用 zk spring boot starter 创建了 zk 应用程序。一切正常工作。

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);
    }
}

项目结构:

.....................
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 正常显示。

之后,我尝试在代理(nginx)后运行它。但页面什么都没有显示。浏览器控制台中出现错误:

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)
........................................................................................

似乎为 .wcs、.wpd 和我的 *.js 文件生成了错误的 URL。我做错了什么?我该如何修复它?

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
英文:

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

SpringBootApplication.java:

@SpringBootApplication
@Controller
public class SpringBootApplication {
	@GetMapping(&quot;${application.base-path:}/{page}&quot;)
    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:

...................
&lt;?script src=&quot;~./js/script1.js&quot;?&gt;
...................

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

答案1

得分: 3

我通过更改配置解决了这个问题:

application.properties

server.servlet.context-path=/app

nginx.conf

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

谢谢,@cor3000 和 @protonchang。是的,这不是ZK的问题。

英文:

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.

答案2

得分: 1

与其他教程进行比较(例如 https://clouding.io/hc/en-us/articles/360010806999-How-to-Deploy-Spring-Boot-Application-with-Nginx-on-Ubuntu-18-04),至少看起来在以下部分可能缺少一个尾随斜杠 '/':

proxy_pass http://my-server:8081

对比:

proxy_pass http://my-server:8081/

希望一些 nginx 专家能够确认这一点,或者提供更多细节。从 ZK 方面来看,当使用代理处理 Web 应用程序时,无需进行任何配置。

英文:

comparing it to other tutorials (e.g. https://clouding.io/hc/en-us/articles/360010806999-How-to-Deploy-Spring-Boot-Application-with-Nginx-on-Ubuntu-18-04) it looks like at least a trailing '/' might be missing on the

proxy_pass http://my-server:8081

vs

proxy_pass http://my-server:8081/

I hope some nginx-experts can confirm this, or give more details. From the ZK side there's nothing you can/have to configure when using a proxy around a webapplication.

答案3

得分: 1

你可以尝试启用 access_logerror_log,如果有代理传递上游的错误,你应该能够识别出来。

根据你提供的配置,你可以尝试使用 @cor3000 的答案。
你当前的配置将会把 /zkau/web/* 路由到端口 8080,请确保你可以从该 Spring Boot 实例获取这些数据。

英文:

You can try turning on access_log and error_log, if there's a proxy_pass upstream error, you should be able to identify that.

From the configuration you provided, you can try using @cor3000's answer.
Your current configuration will route /zkau/web/* to port 8080, so make sure you can get those data from that springboot instance.

huangapple
  • 本文由 发表于 2020年7月24日 16:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63069985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定