如何解决Nginx上的404错误? [英] How to work around the 404 error on nginx?

查看:164
本文介绍了如何解决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错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆