nginx - 在 Nginx 中提供静态 HTML 文件而不在 url 中扩展

标签 nginx webserver httpserver

根目录 =/srv/myproject/xyz/main/

在“主”文件夹中,我有几个 *.html 文件,我希望它们都指向一个 url 说 /test/ (这与目录结构有很大不同)

这是我非常基本的 nginx 配置

server {
    listen                80;
    error_log   /var/log/testc.error.log;

    location /test/ {
         root   /srv/myproject/xyz/main/;
         #alias /srv/myproject/xyz/main/;
         default_type   "text/html";
         try_files  $uri.html ;
    }
}

如果我使用简单别名
location /test/ {
       alias /srv/myproject/xyz/main/;   
}

那么它的工作完美,我的意思是我可以通过 http://www.myurl.com/test/firstfile.html 访问这些 html 文件等等

但我不想要那个 html 扩展。

我试图遵循这些线程但没有成功
http://forum.nginx.org/read.php?11,201491,201494

How to remove both .php and .html extensions from url using NGINX?

how to serve html files in nginx without showing the extension in this alias setup

最佳答案

尝试这个

location ~ ^/test/(.*)$ {
    alias /srv/myproject/xyz/main/;
    try_files $1.html =404;
}

关于nginx - 在 Nginx 中提供静态 HTML 文件而不在 url 中扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28175222/

相关文章:

nginx - 使用 Nginx 在 Docker 容器中部署带有路由器的 Angular2

asp.net - XP 上使用 Visual Studio 的 IIS Developer Express

javascript - 与 http 服务器通信

php - NGINX/PHP - 无法通过 php 脚本提供 jpeg

安装乘客后 Nginx 重启命令失败

PhpStorm:无法评估表达式 '$_SERVER[' SERVER_PORT']'

python - 如果文件具有 CRLF 行结尾,为什么在类 unix 系统上通过 CGI/WSGI 运行的 python 脚本会失败?

apache - 谷歌在页面速度洞察工具中提到的往返行程是什么?

delphi - 无法从 delphi 中的 TIdHTTP 连接到 TIdHTTPServer

java - SSL协议(protocol)可以保护Web Service吗?