Nginx try_files
2016-05-10 19:21
615 查看
以下引自官方文档:
Checks the existence of files in the specified order and usesthe first found file for request processing; the processingis performed in the current context.The path to a file is constructed from the
and
alias directives.It is possible to check directory’s existence by specifyinga slash at the end of a name, e.g. “
made.For example:
The last parameter can also point to a named location,as shown in examples below.Starting from version 0.7.51, the last parameter can also be a
Example in proxying Mongrel:
Example for Drupal/FastCGI:
In the following example,
the
And here,
Example for Wordpress and Joomla:
syntax: | try_files;[/code]file... [code]uri try_files;[/code]file... =[code]code |
---|---|
Default: | — |
Context: | server, location |
fileparameteraccording to theroot
and
alias directives.It is possible to check directory’s existence by specifyinga slash at the end of a name, e.g. “
$uri/”.If none of the files were found, an internal redirect to the
urispecified in the last parameter is
made.For example:
location /images/ { try_files $uri /images/default.gif; } location = /images/default.gif { expires 30s; }
The last parameter can also point to a named location,as shown in examples below.Starting from version 0.7.51, the last parameter can also be a
code:
location / { try_files $uri $uri/index.html $uri.html =404; }
Example in proxying Mongrel:
location / { try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
Example for 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 }
In the following example,
location / { try_files $uri $uri/ @drupal; }
the
try_filesdirective is equivalent to
location / { error_page 404 = @drupal; log_not_found off; }
And here,
location ~ \.php$ { try_files $uri @drupal; fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; ... }
try_fileschecks the existence of the PHP filebefore passing the request to the FastCGI server.
Example for Wordpress and Joomla:
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 }
相关文章推荐
- nginx配置alias和root的区别
- nginx php-fpm 小VPS 优化
- Linux下安装nginx
- 脚本控制nginx的启动,关闭,重载等
- nginx的启动 重启 关闭
- nginx location优先级详解
- Nginx部署Thinkphp 404错误PathInfo的解决方案
- 配置Nginx+PHP的正确思路与过程
- Nginx + CGI/FastCGI + C/Cpp
- nginx 并发数问题思考:worker_connections,worker_processes与 max clients
- nginx虚拟主机配置
- 使用nginx支持utf8中文
- nginx安装详解
- Nginx 获取真实 IP 方案
- ubunt,cent OS安装配置Nginx
- 阿里云Nginx绑定多个域名的方法
- LNMP(linux+nginx+mysql+php)服务器环境配置
- ubuntu安装nginx
- 用于wordpress站点的nginx服务器配置(PHP)
- PHP7+Nginx的配置与安装教程详解