【问题标题】:Nginx configuration for static html-css静态 html-css 的 Nginx 配置
【发布时间】:2023-01-20 15:39:50
【问题描述】:

我有以下 nginx 配置,

server {
    listen 80;

    root /vol/www/home;
    index /index.html;

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

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

这很好用,但是当我在主页中选择一些路径时说关于我们页面,uri 带有完整的文件名,包括结尾 .html。通过在 nginx 配置中添加一个单独的位置并将所有 href 链接更改为该位置而不是文件名,我能够摆脱这些问题。因此,这是一种解决方法,但无法在没有服务器的情况下在本地运行。

我在 nginx 中看到了一个 rewrite 配置,但我没有成功。所以我在这里问我如何设置没有 html 部分的 uri,并且不修改 uri 的 href 路径而不是文件名。

我的配置是这样的,但它不起作用

server {
    listen 80;

    root /vol/www/home;
    index /index.html;

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    location / {
        rewrite ^\/(.*)(\.html)?$ /$1 last;
        try_files $uri $uri/ /index.html =404;
    }
}

抛出 500 错误:处理“/”时重写或内部重定向循环

【问题讨论】:

    标签: html nginx nginx-location


    【解决方案1】:

    将其替换为

       rewrite ^/(.*)(.html)?$ /$1 last;
            try_files $uri $uri/ /index.html =404;
    

    这个

    rewrite ^(/.*).html(?.*)?$ $1$2 permanent;
    rewrite ^/(.*)/$ /$1 permanent;
    
    try_files $uri/index.html $uri.html $uri/ $uri =404;
    

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 2015-02-19
      • 2013-05-22
      • 2017-01-17
      • 2023-04-04
      相关资源
      最近更新 更多