How to work around the 404 error on nginx?

。_饼干妹妹 提交于 2019-12-02 04:43:16

问题


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

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.

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

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


回答1:


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

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



来源:https://stackoverflow.com/questions/47077428/how-to-work-around-the-404-error-on-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!