What does try_files do in this nginx configuration?

the*_*fog 9 nginx

I copied this config when setting up a basic Nginx / PHP-FPM webserver

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以正常工作,但我不明白try_fileslocation ~ \.php$ { .... }处理对 php 文件的请求时in块是如何工作的,例如domain.com/test.php.
我以为这条线

try_files $uri =404; 
Run Code Online (Sandbox Code Playgroud)

告诉 nginx 继续尝试提供静态文件 - 即附加$uriroot目录,如果文件存在 - nginx 将简单地发送静态文件并且请求将结束,不是吗?因此fastcgi_pass不会发生?但php-fpm确实得到它并执行脚本。
为什么不try_files阻止fastcgi_pass

Ric*_*ith 12

try_files不告诉nginx服务静态文件。在没有任何其他操作的情况下到达右大括号会导致它为静态文件提供服务。try_files测试文件在本地文件系统中是否存在,并可能重写 URL。

在将 URL 发送到上游解释器之前,try_files $uri =404;通过确保 PHP 文件是真实文件来克服特定脚本注入漏洞的许多常见技巧之一也是如此。