小编Dou*_*hen的帖子

使用 nginx 重写“隐藏”.html 文件扩展名

我正在通过 nginx 提供一个静态站点,我的目标是替换如下所示的 URL:

http://foo.com/bar.html

http://foo.com/bar

关键是没有尾部斜杠。我目前正在使用位置别名做类似的事情,但这很乏味,因为它需要每个文件的位置块,并且它还附加一个斜杠,因为 nginx 将别名视为目录:

    location / {
        root    /srv/www/foo/public_html;
        index   index.html;
    }

    location /bar1 {
        alias /srv/www/foo/public_html/;
        index bar1.html;
    }

    location /bar2 {
        alias /srv/www/foo/public_html/;
        index bar2.html;
    }
Run Code Online (Sandbox Code Playgroud)

等等。我已经通读了有关重写的文档,但似乎无法将所说的内容与我需要做的事情综合起来。我不是来自 Apache 背景;nginx 是我第一次涉足网络服务器,所以我确定我错过了一些明显的东西,因为我的 HTTP 背景很弱。在此先感谢您提供的任何帮助。

rewrite nginx

17
推荐指数
2
解决办法
2万
查看次数

虚拟主机的 nginx 自定义 404 错误页面

我在 nginx 上运行了一些虚拟主机,我希望它们每个都有自己的自定义 404 页面。每个虚拟主机都有自己的 nginx.conf,我包括我的全局 nginx.conf。

在我目前无法正常工作的特定虚拟主机的配置文件中,配置块如下所示:

server{
    listen 80;
    server_name www.foo.com;
    expires     1M;
    access_log  /srv/www/foo/logs/acces.log;
    error_log   /srv/www/foo/logs/error.log;

    error_page 404 /404.html;

    location / {
        root    /srv/www/foo/public_html;
        index   index.html;
        rewrite ^/(.*)/$ /$1 permanent;
        try_files "${uri}.html" $uri $uri/ =404;
    }

    location = /404.html {
        internal;
    }       
}
Run Code Online (Sandbox Code Playgroud)

404'ing 本身没问题,但是当返回 404 状态代码时,将显示默认的 nginx 404 页面,而不是位于站点根目录中的自定义页面 404.html。

我已经尝试通过文件系统和站点的 URI 绝对路径到错误页面,并且我已经尝试将 error_page 指令放在位置块中。这些都没有奏效。我怀疑问题出在我用来“美化”我的 URL 的重写和 try_files 组合中。

nginx virtualhost custom-errors

3
推荐指数
1
解决办法
7695
查看次数

标签 统计

nginx ×2

custom-errors ×1

rewrite ×1

virtualhost ×1