nginxで複数の静的ディレクトリを設定する方法は何ですか?

複数の静的ディレクトリを設定するには、nginxの設定ファイルで複数のlocationディレクティブを使用する必要があります。以下は例です:

server {
    listen 80;
    server_name example.com;
    root /var/www;

    location / {
        index index.html;
    }

    location /static1 {
        alias /path/to/static1;
        expires 7d;
    }

    location /static2 {
        alias /path/to/static2;
        expires 7d;
    }
}

上記の構成では、/static1と/static2は異なるURLパスであり、それぞれの静的ファイルディレクトリは/path/to/static1と/path/to/static2に対応しています。これらの静的ファイルディレクトリはnginxサーバーによって提供され、レスポンスヘッダーのExpiresフィールドが7日に設定されています。

alias指令を使用して静的ファイルディレクトリを指定する際には、パスの末尾にスラッシュ/を入れることが必要です。そうしないと、nginxはパスの後ろにリクエストのURLパスを追加してしまいます。

bannerAds