如果文件存在,Nginx如何才能使用try_files?

时间:2015-03-20 09:17:09

标签: symfony nginx

我已经完成了Symfony from()[]项目的虚拟主机示例。

有一个代码:

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;的行为,但如果maintenance.html存在则

2 个答案:

答案 0 :(得分:2)

我解决了它:

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

答案 1 :(得分:2)

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