如何解决Nginx上的404错误?

编程入门 行业动态 更新时间:2024-10-24 02:02:45
本文介绍了如何解决Nginx上的404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Angle 4 SPA应用程序,我正在使用docker进行生产.到目前为止看起来还不错.通过终端,我进入/dist文件夹,然后使用以下命令让docker指向dist的内容: docker run -d -p 9090:80 -v $(pwd):/usr/share/nginx/html nginx:alpine

I have an angular 4 SPA app and I'am using docker for production. Looks fine so far. Via terminal I go to /dist folder and from there I let docker point to the content of dist with the following command: docker run -d -p 9090:80 -v $(pwd):/usr/share/nginx/html nginx:alpine

我在浏览器上致电:localhost:9090,并且可以访问该应用程序. 问题是当我再次重新加载页面和/或作为特定路由重新加载页面时,我得到了404 Not Found和nginx版本,例如nginx/1.13.5.

I call: localhost:9090 on the browser and can access the app. The issue is when I reload the page once again and/or as specific route, then I get 404 Not Found and the nginx version e.g. nginx/1.13.5.

我一直在搜索并发现此问题:在Nginx Ask上找不到404错误,但不幸的是没有解决办法.

I have been searching and found this issue: 404 not found error on Nginx Ask, but unfortunately no solution.

如何解决此问题并避免404错误?是我必须编辑的配置步骤吗?

How to work around this and avoid the 404 error? Is it a config steps which I do have to edit?

推荐答案

问题在于,在默认的nginx配置中,它仅使用index指令.您需要的是 try_files 指令,该指令将首先尝试uri,它将转到index.html

The issue is that in the default nginx config it only uses the index directive. What you need is the try_files directive which will first try the uri then it will go to the index.html

为此,您需要传递自己的默认虚拟主机配置.像下面这样的东西应该起作用.这基于 Nginx的Docker Github

To do this you need to pass in your own default virtual host config. Something like the following should work. This is based on Nginx's Docker Github

server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html index.htm; location / { try_files $uri /index.html; } }

在那之后,应该只是将该文件作为卷放置在正确的位置.

After that is it should be just making that file as a volume in the right spot.

docker run -d -p 9090:80 -v $(pwd):/usr/share/nginx/html -v (path_to_your_config):/etc/nginx/conf.d/default.conf nginx:alpine

更多推荐

如何解决Nginx上的404错误?

本文发布于:2023-08-07 14:18:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   错误   Nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!