nginx

nginx怎么配置多个静态目录

小亿
484
2024-01-05 02:35:25
栏目: 编程语言

要配置多个静态目录,你需要在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路径。

0
看了该问题的人还看了