nginx try_files 详解

server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root /data/wwwroot;
        location  /wordpress{
                <span style="color:#ff0000;"><strong><em>try_files $uri $uri/ /wordpress/index.php?$args;</em></strong></span>
        }
        location ~ .*\.(php|php7)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        #伪静态设置路径 和日志文件路径
        #include /etc/nginx/rewrite/default.conf;
        access_log  /data/wwwlogs/wordpress_access.log;
        error_log /data/wwwlogs/wordpress_error.log;
}

根据上面的标准红色的字体“try_files $uri $uri/ /wordpress/index.php?$args;”为例,我们做如下说明:

当用户请求 http://blog.csdn.net/example 时,这里的 $uri 就是 /example。try_files 会到硬盘里尝试找这个文件。

如果存在名为 /$root/example(其中 $root 是 WordPress 的安装目录)的文件,就直接把这个文件的内容发送给用户。

如果不存在名为 叫 example 的文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。又找不到,就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于 nginx 发起一个 HTTP 请求到 http://blog.csdn.net/index.php。这个请求会被 location ~ \.php$ { ... } catch 住,也就是进入 FastCGI 的处理程序。而具体的 URI 及参数是在 REQUEST_URI 中传递给 FastCGI 和 WordPress 程序的,因此不受 URI 变化的影响。

喜欢这篇文章?欢迎打赏~~

 

posted @ 2018-12-27 16:17  苍青浪  阅读(2751)  评论(0编辑  收藏  举报