Mereithhh/van-nav

直接访问端口可以打开,通过nginx转发后访问失败

Closed this issue · 4 comments

server {
    listen       80;
    server_name  localhost;
 
    charset utf-8; # 防止中文显示出现乱码

	# 添加头部信息
    proxy_set_header  Cookie $http_cookie;
    proxy_set_header  X-Forwarded-Host $Host;
    proxy_set_header  proxy_set_Server  $Host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host             $host:$server_port;
	
    # 访问IP/eat,则会自动访问对应地址IP:8822
    location /eat/ {
        proxy_pass http://localhost:8822/;
    }  

    # 访问IP/eat,则会自动访问对应地址IP:6412
    location /navtool/ {
        proxy_pass http://localhost:6412/;
    }  
}

博主的两个docker项目我都部署了

  • eat项目是可以通过IP/eat访问的
  • 但nav项目只能通过IP:6412访问,如果通过IP/navtool访问,无法获取到对应的文件
    image
    只获取到了其中的一个文件,其他三个都获取失败

nginx的日志为

2023/02/10 12:26:48 [error] 8506#8506: *40 open() "/usr/share/nginx/html/static/js/2.3b4ef65b.chunk.js" failed (2: No such file or directory), client: 46.232.120.169, server: localhost, request: "GET /static/js/2.3b4ef65b.chunk.js HTTP/1.1", host: "我的IP", referrer: "http://IP/navtool/"
2023/02/10 12:26:48 [error] 8506#8506: *39 open() "/usr/share/nginx/html/static/js/main.6a5ad524.chunk.js" failed (2: No such file or directory), client: 46.232.120.169, server: localhost, request: "GET /static/js/main.6a5ad524.chunk.js HTTP/1.1", host: "我的IP", referrer: "http://IP/navtool/"
2023/02/10 12:26:48 [error] 8506#8506: *41 open() "/usr/share/nginx/html/static/css/main.cb5e9b77.chunk.css" failed (2: No such file or directory), client: 46.232.120.169, server: localhost, request: "GET /static/css/main.cb5e9b77.chunk.css HTTP/1.1", host: "我的IP", referrer: "http://IP/navtool/"

猜测是不是因为文件在docker容器的/usr/share/nginx/html/static目录中,转发无法直接访问到容器内部,只能通过端口访问到容器内部。
但博主给出的配置案例也是通过路由转发,不是很明白这一点

    # 访问IP,则会自动访问对应地址IP:6412
    location / {
        proxy_pass http://localhost:6412/;
    } 

我改成了这样,就可以直接访问IP进行转发了,好奇怪

如果想通过子路径访问,还应该修改一些 nginx 配置,之前我踩过坑,记不太清楚了。但原理大概是要保证把你访问的 url 中的 pathname 中截取制定的前缀后,再反代到处理后的 pathname。

我建议你通过不同的域名进行反向代理比较合适。如果非要这么做,暂时我没有现成的方案(配置文件)可以给你直接用,等我有时间的话,我会研究一下加到对应项目的文档里(可能要一阵子才会有时间做这个了)

好的好的,我把nav项目配置成用域名直接访问就OK的,博主别再弄这个啦,有点浪费时间hh

好的