繁体   English   中英

nginx 将所有 url 重定向到 index.html

[英]nginx redirect all urls to index.html

我正在尝试根据以下指南配置 nginx 服务器: https : //gkedge.gitbooks.io/react-router-in-the-real/content/nginx.html

但看起来它不起作用,我正在尝试使用我的 nginx docker 映像。 所以,

Dockerfile:

FROM nginx:1.11.5

COPY /dist /usr/share/nginx/html
COPY /vhosts.conf /etc/nginx/conf.d/vhosts.conf

vhosts.conf:

server {
    listen 80 default_server;
    server_name /usr/share/nginx/html;

    root /usr/share/nginx/html;
    index index.html index.htm;      

    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs/static.log; # I don't usually include a static log
    }

    location ~* \.(?:css|js|json)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+\..+$ {
      try_files $uri =404;
    }

    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri $uri/ /index.html;
    }
}

试图打开一个不存在的页面。

GET localhost/somestupidrandomurl

nginx错误日志:

[error] 7#7: *3 open() "/usr/share/nginx/html/somestupidrandomurl" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /somestupidrandomurl HTTP/1.1", host: "localhost"

提前致谢。

经过深入搜索,我意识到问题是,在/etc/nginx/conf.d/路径下还有一个 default.conf 文件,该配置覆盖了我的配置。

我只是将 Dockerfile 第 3 行替换为

COPY /vhosts.conf /etc/nginx/conf.d/default.conf并像魔术一样工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM