Nginx如何仅在文件存在时使用try_files?

How can Nginx use try_files only if file exists?

我已经在 ()[].

上为 Symfony 上的项目完成了虚拟主机示例

有一个密码:

server {
    listen  80;

    server_name localhost www.localhost;
    root /vagrant/web;

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app_dev.php$is_args$args;
    }

    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        #internal;
    }
}

但我想为我改进一下。如何默认加载 maintenance.html 而不是 app_dev.php

P.S。我需要像 try_files $uri /maintenance.html$is_args$args; 而不是 try_files $uri /app_dev.php$is_args$args; 这样的行为,但是 ONLY 如果 maintenance.html 存在

我解决了:

try_files $uri /maintenance.html$is_args$args /app_dev.php$is_args$args;

来自 here 的解决方案,带有一些评论

location / { #can be any location or even in server
    if (-f $document_root/maintenance.html) {
        return 503;     #503 for search engines
    }
    ... # the rest of your config goes here
}

error_page 503 @maintenance;
location @maintenance {
    rewrite ^(.*)$ /maintenance.html break;
}