sashimi4’s diary

日々の雑多なメモを書きます

Nginx の Fancy Index を最も手っ取り早く使う方法

Fancy Index について: Fancy Index | NGINX

自分で Nginx のソースと当該モジュールを持ってくる必要があると思いがちだが、実は既にバンドルされているパッケージが存在する。

パッケージ名は nginx-extras 、apt (apt-get) でそのままインストール可能だ。 Debian -- sid の nginx-extras パッケージに関する詳細
Ubuntu – xenial の nginx-extras パッケージに関する詳細

確認していないが、RHEL系でも同様のパッケージが提供されていると思われる。

Fancy Index の設定

既に nginx が導入済みの場合でも、 apt -y install nginx-extras でそのまま導入することができる。

location コンテキスト内に下記のように記載するだけ。なお autoindex系の設定は削除して置く必要がある。

# cat /etc/nginx/conf.d/foo.conf
location /foo {
        # ...

        fancyindex on;                     # Enable fancy indexes.
        fancyindex_exact_size off;  # Output human-readable file sizes.
        fancyindex_localtime on;

        # ...
}

# ...

systemctl reload nginx.service で設定を再読み込みすれば、下図のようにFancyな表示が行われる。 f:id:sashimi4:20200424130353p:plain

おまけ

リンクが見ずらいので雑にスタイルを当ててみる。

# cat /usr/share/nginx/html/fancy_href.css
a:-webkit-any-link {
    color: -webkit-link;
    cursor: pointer;
    text-decoration: underline;
}
a:-webkit-any-link:focus {text-decoration: underline; color: #f4ee41;}
a:-webkit-any-link:hover {color: #e33;}
td:hover {
  background-color: #FFEEFF;
}
# cat /etc/nginx/conf.d/foo.conf
server {

    # ...

    location /foo {
        # ...

        fancyindex_css_href /fancy_href.css;

        # ...
    }
}

以上。