Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了NGINX在HTML页面以外的所有页面上都有404错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我将Nginx设置为docker容器中的反向代理,以链接到容器外部的站点.我有一个vhost配置设置如下(我尝试在home-assistant的位置之前添加^〜):

server { # simple reverse-proxy
    listen       80;

    LOCATIOn / {
        proxy_pass http://192.168.1.99:6789;
    }

    LOCATIOn ^~ /home-assistant {
        proxy_pass http://192.168.1.99:8123/;
    }

    LOCATIOn /calibre-web/ {
        proxy_pass http://192.168.1.99:8181/;
    }

  }

对于home-assistant和caliber-web站点,页面加载,但我得到404错误的所有其他项目(图像,CSS等…).如果我尝试单击应用程序中的链接,它会将我链接到192.168.1.99/file而不是192.168.1.99/site-folder/file.以下是日志中的一些记录(请注意一个200响应,其他404响应).我在这做错了什么?在此先感谢您的帮助.

192.168.1.6 - rAndy [15/Aug/2016:03:15:42 +0000] "GET /frontend/panels/dev-template-0a099d4589636ed3038a3e9f020468a7.html http/1.1" 404 199 "http://192.168.1.99/home-assistant/" "Mozilla/5.0 (X11; CrOS x86_64 8350.60.0) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/52.0.2743.85 Safari/537.36"

192.168.1.6 - rAndy [15/Aug/2016:03:15:42 +0000] "GET /frontend/panels/logbook-66108d82763359a218c9695f0553de40.html http/1.1" 404 199 "http://192.168.1.99/home-assistant/" "Mozilla/5.0 (X11; CrOS x86_64 8350.60.0) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/52.0.2743.85 Safari/537.36"

192.168.1.6 - rAndy [15/Aug/2016:03:17:54 +0000] "GET /home-assistant/ http/1.1" 200 1654 "-" "Mozilla/5.0 (X11; CrOS x86_64 8350.60.0) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/52.0.2743.85 Safari/537.36"

192.168.1.6 - rAndy [15/Aug/2016:03:17:55 +0000] "GET /static/icons/favicon-192x192.png http/1.1" 404 91 "http://192.168.1.99/home-assistant/" "Mozilla/5.0 (X11; CrOS x86_64 8350.60.0) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/52.0.2743.85 Safari/537.36"

192.168.1.6 - rAndy [15/Aug/2016:03:17:55 +0000] "GET /static/core-457d5acd123e7dc38947c07984b3a5e8.js http/1.1" 404 91 "http://192.168.1.99/home-assistant/" "Mozilla/5.0 (X11; CrOS x86_64 8350.60.0) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/52.0.2743.85 Safari/537.36"

Here is a copy of my root nginx.conf.我猜这个错误就在这里,我只是想弄清楚在哪里.

最佳答案
您正在尝试从原始应用程序未知的位置发出proxy_pass请求.在docker容器应用程序中仍然认为它是从网站根目录(http://192.168.1.99:8123/)访问的,因此它生成相对于/的所有URL,而不是您的/ home-assistant /或/ caliber-web / .

有很多方法可以解决这个问题,你应该选择最适合你的方法:

1.替换代理html中的链接

首先,它是在用ngx_http_sub_module向客户端提交结果之前替换从代理应用程序接收的每个链接.默认情况下没有启用它,所以如果你的发行版没有它,你可能需要compile nginx from source(检查这个的可用性)模块运行Nginx -V,它应该在配置参数的某处显示–with-http_sub_module.

如果选择此方法,请将以下指令添加到配置中:

LOCATIOn ^~ /home-assistant {
    rewrite ^/home-assistant(/.*)$$1 break;
    proxy_pass http://192.168.1.99:8123/;
    sub_filter "<>

2.更改容器内的应用程序路径

将docker容器内的应用程序移动到/ home-assistant而不是root,因此它将生成与该路径相关的服务内容中的所有URL而不是root /.这是一个显而易见的(也许是最简单的)解决方案,它根本不需要触摸Nginx配置(而是在docker中编辑配置)

3.根据Referer标头选择上游

根据Referer标头确定应使用哪个上游. Taken from here.看起来非常聪明,然我自己也不会在生产中使用它(注意它只适用于css / js文件).

LOCATIOn ~* ^/(css|js)/.+\.(css|js)${
    #checking if referer is from home-assistant
    if ($http_referer ~ "^.*/home-assistant"){
        return 417;
    }

    #checking if referer is from calibre-web
    if ($http_referer ~ "^.*/calibre-web"){
        return 418;
    }
}

error_page   417  /home-assistant$request_uri;
error_page   418  /calibre-web$request_uri;

LOCATIOn ^~ /home-assistant {
    proxy_pass http://192.168.1.99:8123/;
}

LOCATIOn /calibre-web {
    proxy_pass http://192.168.1.99:8181/;
}

如果我是你,我会选择#2 – 在容器内移动应用程序作为最简单的&最不@R_52_10197@的方式来获得你需要的东西.

附:我完全忘了你可以直接从你的硬盘驱动器提供静态文件,因为你的后端和Nginx在同一台服务器上.它将需要一些依赖于应用程序的配置,但结果您将获得最有效的解决方案..

大佬总结

以上是大佬教程为你收集整理的NGINX在HTML页面以外的所有页面上都有404错误全部内容,希望文章能够帮你解决NGINX在HTML页面以外的所有页面上都有404错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: