【问题标题】:nginx redirect all urls to index.htmlnginx 将所有 url 重定向到 index.html
【发布时间】:2019-02-02 11:31:47
【问题描述】:

我正在尝试根据以下指南配置 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"

提前致谢。

【问题讨论】:

    标签: reactjs redirect nginx react-router


    【解决方案1】:

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

    我只是将 Dockerfile 第 3 行替换为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2016-05-04
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多