我尝试获取一个与 try_files 配合使用的 nginx 静态配置,但遇到了一些问题。以下是我目前正在使用的内容(但不处理目录、显示索引等)。我想看看是否可以获得一个与 try_files 配合使用的版本...
server {
listen 80;
server_name foo.com;
rewrite ^ http://www.foo.com$uri permanent;
}
# the server directive is nginx's virtual host directive.
server {
# port to listen on. Can also be set to an IP:PORT
listen 80;
# Set the charset
charset utf-8;
# Set the max size for file uploads to 10Mb
client_max_body_size 10M;
# sets the domain[s] that this vhost server requests for
server_name www.foo.com;
# doc root
root /var/www/foo.com;
# vhost specific access log
access_log /var/log/nginx_access.log main;
# Set image format types to expire in a very long time
location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
access_log off;
expires max;
}
# Set css and js to expire in a very long time
location ~* ^.+\.(css|js)$ {
access_log off;
expires max;
}
location /blog {
rewrite ^ http://blog.foo.com permanent;
}
# Catchall for everything else
location / {
root /var/www/foo.com;
access_log off;
index index.html index.shtml;
expires 1d;
#try_files $uri $uri/;
if (-f $request_filename) {
break;
}
}
location ~* ^/(pkg|rpms)/ {
autoindex on;
}
error_page 500 502 503 504 /500.html;
error_page 404 /404.html;
}
答案1
从http://wiki.nginx.org/NginxHttpMainModule#try_files:
如果没有找到文件,则调用内部重定向到最后一个参数。
如果 末尾没有 URI try_files
,它将无法正常工作。