http://nginx.org/en/docs/http/ngx_http_core_module.html#try_filesphp
- | 说明 |
---|---|
语法 | try_files file ... uri ; try_files file ... =code ; |
默认 | —— |
上下文 | server、location |
以指定顺序检查文件是否存在,并使用第一个找到的文件进行请求处理。处理将在当前上下文中执行。指向文件的路径根据 root 和 alias 指令从 file
参数构造。能够经过在名称末尾指定斜线来检查目录是否存在,例如,$URI/
。若是找不到任何文件,则内部重定向将指向最后一个参数中指定的 uri
。例如:html
location /images/ { try_files $uri /images/default.gif; } location = /images/default.gif { expires 30s; }
最后一个参数也能够指向一个命名的 location ,如如下示例。从 0.7.51 版本开始,最后一个参数也能够是一个 code
:nginx
location / { try_files $uri $uri/index.html $uri.html =404; }
代理 Mongrel 示例:git
location / { try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
Drupal/FastCGI 示例:服务器
location / { try_files $uri $uri/ @drupal; } location ~ \.php$ { try_files $uri @drupal; fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param QUERY_STRING $args; ... other fastcgi_param's } location @drupal { fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to/index.php; fastcgi_param SCRIPT_NAME /index.php; fastcgi_param QUERY_STRING q=$uri&$args; ... other fastcgi_param's }
在如下示例中ide
location / { try_files $uri $uri/ @drupal; }
try_files 指令至关于wordpress
location / { error_page 404 = @drupal; log_not_found off; }
还有一个示例代理
location ~ \.php$ { try_files $uri @drupal; fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; ... }
在将请求传递给 FastCGI 服务器以前,try_files 将检查 PHP 文件是否存在。code
Wordpress 与 Joomla 示例:server
location / { try_files $uri $uri/ @wordpress; } location ~ \.php$ { try_files $uri @wordpress; fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; ... other fastcgi_param's } location @wordpress { fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to/index.php; ... other fastcgi_param's }