Symfony 仅当文件存在时,Nginx如何使用try_文件?

Symfony 仅当文件存在时,Nginx如何使用try_文件?,symfony,nginx,Symfony,Nginx,我已经从[]为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 se

我已经从[]为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

另外,我需要像try_文件$uri/maintenance.html$is_args$args这样的行为;而不是try_files$uri/app_dev.php$is_args$args;,但只有当maintenance.html存在时,我才用以下方法解决它:

try_files $uri /maintenance.html$is_args$args /app_dev.php$is_args$args;
有点意见的解决方案

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;
}

我会用if来解决这个问题。@Smar谢谢,但我用try_文件$uri/maintenance.html$is_args$args/app_dev.php$is_args$args;它工作起来很有趣,但打破了我所有的样式、脚本和图像503状态代码。因此,它将样式和脚本替换为503个牙套!我只在app.php prod环境中使用它,它工作得非常好@斯玛:我不能,我说我可以在两天内接受:哎呀,我不知道^^