【发布时间】:2016-07-13 05:23:43
【问题描述】:
这是我的 nginx.conf:
if ($host ~ "^example.com$") {
rewrite . /index.php last;
}
如何让 nginx 在不重写 url 的情况下提供 /static/ 目录中的文件?
【问题讨论】:
标签: regex .htaccess nginx rewrite nginx-location
这是我的 nginx.conf:
if ($host ~ "^example.com$") {
rewrite . /index.php last;
}
如何让 nginx 在不重写 url 的情况下提供 /static/ 目录中的文件?
【问题讨论】:
标签: regex .htaccess nginx rewrite nginx-location
不确定为什么要测试$host 的值。区分主机名的常用方法是使用多个服务器块。详情请见this document。
try_files 指令用于首先检查静态文件是否存在,如果不存在则执行默认操作:
server {
...
root ...;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ { ... }
}
这适用于任何静态文件(不仅仅是/static 目录中的那些)。详情请见this document。
但是,如果您特别希望以不同方式处理 /static 目录,请使用带有 ^~ 修饰符的前缀位置:
location ^~ /static { }
详情请见this document。
【讨论】: