docker - 如何在 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
也这样做吗?
解决方案
推荐阅读
- typescript - Firebase:获取 snapshot.val() 对象的子对象
- c++ - 是否有一个重要的例子 int* x = new int 实际上有用?
- excel - 此代码是使用 Microsoft Library 15.0 创建的,但不适用于 14.0 并突出显示“Sub”字样?
- php - Twig 在树枝数组路径中使用变量
- javascript - 在反应 js 中导航页面和处理后退按钮的最佳方式
- vba - 如果尚未在范围内,则将值从 Excel 2016 用户表单文本框传递到命名范围
- c++ - 具有两个动态维度和一个静态维度的多维数组
- python - TypeError:在标签上显示文本时,“NoneType”对象不可下标
- angular - 路由更改时 Chrome Redux DevTools 崩溃
- vue.js - 如何重置 vue-infinite-loading 元素?