【Nginx】转:Nginx try_files

原来的配置是这样的:

        location / {
            try_files $uri $uri/ /index.php;
            index  index.html index.htm index.php;
        }
        location ~ \.php$ { ... }

修改成了

        location / {
            try_files $uri $uri/ /index.php =404;
            index  index.html index.htm index.php;
        }
        location ~ \.php$ { ... }

增加的这个 =404,似乎是让 index.php 找不到的时候返回 404。但这么一改,形如https://servers.blog.ustc.edu.cn/2014/09/ustc-telecom-link-failure/ 这样的链接返回的竟然是 index.php 的源码!这是怎么回事呢?

这要从 nginx try_files 的工作原理说起。

以 try_files $uri $uri/ /index.php; 为例,当用户请求 http://servers.blog.ustc.edu.cn/example 时,这里的 $uri 就是 /example

try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是 WordPress 的安装目录)的文件就直接把这个文件的内容发送给用户。显然,目录中没有叫 example 的文件。

然后就看 $uri/增加了一个 /,也就是看有没有名为/$root/example/ 的目录。又找不到,

就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于 nginx 发起一个 HTTP 请求到http://servers.blog.ustc.edu.cn/index.php

这个请求会被 location ~ \.php$ { ... }catch 住,也就是进入 FastCGI 的处理程序。而具体的 URI 及参数是在 REQUEST_URI 中传递给 FastCGI 和 WordPress 程序的,因此不受 URI 变化的影响。

配置修改之后,/index.php 就不在 fall back 的位置了,也就是说 try_files 在文件系统里找到 index.php 之后,就会直接把文件内容(也就是 PHP 源码)返回给用户。这里的坑就是 try_files 的最后一个位置(fall back)是特殊的,它会发出一个内部 “子请求” 而非直接在文件系统里查找这个文件。

时间: 2024-08-06 11:03:23

【Nginx】转:Nginx try_files的相关文章

Nginx配置指令try_files

try_files指令是按顺序检测文件的存在性,并且返回第一个找到文件的内容,如果第一个找不到就会自动找第二个,依次查找.其实现的是内部跳转.以下举例说明: 案例1(跳转到变量): server {   listen 8000;   server_name 121.10.143.66;   root html;   index index.html index.php; location /abc { try_files /4.html /5.html @qwe;      --检测文件4.ht

laravel部署在nginx 出现 nginx 403 forbidden 错误的处理

laravel部署在nginx 总是出现 nginx 403 forbidden 如果不是权限问题,也不是索引文件的问题.那就是,laravel的主目录指定错了.原来不能指定laravel程序的根目录.要指定在public目录. Nginx 服务器 location / { try_files $uri $uri/ /index.php?$query_string; } 版权声明:本文为博主原创文章,未经博主允许不得转载.

linux下Nginx配置文件(nginx.conf)配置设置详解(windows用phpstudy集成)

linux备份nginx.conf文件举例: cp /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf-20171111(日期) 在进程列表里 面找master进程,它的编号就是主进程号. ps -ef | grep nginx 查看进程 cat /usr/local/nginx/nginx.pid 每次修改完nginx文件都要重新加载配置文件linux命令: /usr/local/nginx -t //验证配置文件是否合法 若ngin

深刻理解Nginx之Nginx完整安装

1.   Nginx安装 1.1预先准备 CentOS系统下,安装Nginx的库包依赖.安装命令如下: sudo yum groupinstall "DevelopmentTools" sudo yum install pcre pcre-devel sudo yum install zlib zlib-devel yum install perl-ExtUtils-Embed sudo yum install openssl openssl-devel 1.2 安装 最重要的特性和基

nginx之 nginx虚拟机配置

1.配置通过域名区分的虚拟机[[email protected] nginx]# cat conf/nginx.confworker_processes 1; events { worker_connections 1024;} http { include mime.types; default_type application/octet-stream; server { listen 80; server_name www.nginx01.com; location / { root ht

<nginx+PHP>nginx环境下配置支持php7

[[email protected] ~]# wget http://am1.php.net/get/php-7.1.2.tar.gz/from/this/mirror [[email protected] ~]# tar xzvf php-7.1.2.tar.gz [[email protected] ~]# cd php-7.1.2/ [[email protected] ~]# ./configure--prefix=/usr/local/php --enable-fpm [[email 

解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid

重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)错误,进到logs文件发现的确没有nginx.pid文件 [[email protected] sbin]# ./nginx -s reload nginx: [err

Nginx Announcing NGINX Plus R7

https://www.nginx.com/blog/nginx-plus-r7-released/?_ga=1.70204696.981062698.1445605275 https://www.nginx.com/ Announcing NGINX Plus R7 NGINX, Inc. is proud to announce the availability of NGINX Plus Release 7 (R7), the latest release of our applicati

深刻理解Nginx之Nginx与Python(1)

6 Python和Nginx 6.1 介绍FastCGI FastCGI(Fast Common Gateway Interface)是基于CGI上的改进,是CGI的一种演变产物.尽管目的是保持相同的,FastCGI在CGI上提供了重大的提升,通过建立起下面的原则. l  代替对于每个请求孵化一个新进程,FastCGI采用持久化进程,伴随着能够处理多个请求的能力. l  Web服务器和网关应用程序通过使用sockets比如TCP或者POSIXT 本地IPC sockets来交流.其结果是,它们的

使用Nginx、Nginx Plus抵御DDOS攻击

原创 2015-10-16 陈洋 运维帮 DDOS是一种通过大流量的请求对目标进行轰炸式访问,导致提供服务的服务器资源耗尽进而无法继续提供服务的攻击手段. 一般情况下,攻击者通过大量请求与连接使服务器处于饱和状态,以至于无法接受新的请求或变得很慢. 应用层DDOS攻击的特征 应用层(七层/HTTP层)DDOS攻击通常由木马程序发起,其可以通过设计更好的利用目标系统的脆弱点.例如,对于无法处理大量并发请求的系统,仅仅通过建立大量的连接,并周期性的发出少量数据包来保持会话就可以耗尽系统的资源,使其无