首页 > 解决方案 > 如何在 nginx docker 容器中将干净的 url 重写为 .html?

问题描述

我可以在 Docker 容器中运行 nginx,端口为 4000。它应该为具有以下结构的静态网站提供服务:

/usr/share/nginx/html/
  index.html
  app.html
  app/
    some-other.html

该端口映射到本地主机上的端口 3000。现在我正在尝试使用以下干净的网址:http://localhost:3000/app. 但是,当我尝试访问此 url 时,它会将我重定向到http://localhost:4000/app/(注意后缀/)和 status code 301 - Moved Permanently

我试过try_files在不同的位置和顺序使用(下面有评论)。没有任何效果。另外,我也用过一个rewrite没有成功的。总是导致相同的重定向。

站点配置文件

server {
    listen       4000;
    server_name  localhost;

    root /usr/share/nginx/html;

#    location / {
#      index index.html
#      try_files $uri $uri.html $uri/ =404;
#    }

    location / {
      index index.html
      try_files $uri @htmlext $uri/;
    }

    location ~ \.html$ {
      try_files $uri =404;
    }

    location @htmlext {
      rewrite ^(.*)$ $1.html last;
    }

    error_page 404 /404;
}

我希望 nginx 在app.html不以任何形式触摸 url 的情况下返回内容。我不能不添加location /app也这样做吗?

标签: dockernginx

解决方案


推荐阅读