服务器问答

nginx 返回静态文件配置问题
0
2021-05-18 16:31:12
idczone

如题, 目前网上搜索找到了以下两种方式返回静态文件

server {
    location ~ ^/a {
        default_type text/html;
        return 200 'asdfasdfasdfasdf';
    }
    
    location ~ ^/b {
        default_type application/json;
        add_header Content-T美国服务器ype 'text/html; charset=utf-8';
        alias /tmp/b.json;
    }
    
    if ($var = true) {
        return 200 "asdf";
    }
}

那么有没有办法在 if 结构中返回一个指定路径的文件内容呢, 而不是一个固定的字符串, 如下伪代码

server {
    if ($var = true) {
        return 200 /path/to/file;
    }
}

不一定必须 if 中实现, 只要是在满足变量$var=true 的情况下, 返回指定路径静态文件内容即可


proxy_pass 可以实现?

帮顶

```nginx
location /condition {
条件满足
rewrite /condition(.*) /first last;
}
location /first {
root html;
index 200 1.txt;
}
```
当访问 http://localhost/condition 的时候实际访问的是 fist 下的内容。
所以你可以按照这个思路,当某种条件满足的时候就将 url rewrite
@GGGG430

return 200 /path/to/file;
这个返回 200 的同时,并不是返回的文件内容,而是一个 “/path/to/file”字符串

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服