【问题标题】:Nginx locations: Exempt certain location from maintenance modeNginx 位置:使某些位置免于维护模式
【发布时间】:2018-01-26 08:58:18
【问题描述】:

我有一个在 nginx 上运行的 Symfony 3.4 应用程序,并且在 nginx 中通过以下方式实现了维护模式:

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

 }

 location ~ ^/(app)\.php(/|$) {

            if (-f "/var/www/mysite.com/web/maintenance.html") {
                   return 503;
            }
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
 }

error_page 503 @maintenance;

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

我想使某个位置免于维护模式,以便 Symfony 应用程序继续为该位置提供服务。这样mysite.com/still/available/route 的请求就不会被重定向到维护页面。

如何做到这一点?

【问题讨论】:

    标签: symfony nginx


    【解决方案1】:

    我找到了答案,其实很明显。只需将“if”放入根位置块 (/) 并添加另一个位置块,其路径应该在维护模式期间仍可访问。当然,这里不能出现 if 子句:

    location / {
                # try to serve file directly, fallback to app.php
                if (-f "/var/www/mysite.com/web/maintenance.html") {
                       return 503;
                }
                try_files $uri /app.php$is_args$args;
    
     }
    
    location /still/available/route {
    
                try_files $uri /app.php$is_args$args;
    
     }
    
    location ~ ^/(app)\.php(/|$) {
    
    
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
     }
    
    error_page 503 @maintenance;
    
    location @maintenance {
               rewrite ^(.*)$ /maintenance.html break;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 2020-02-08
      • 2011-10-14
      • 1970-01-01
      • 2021-10-05
      • 2015-10-04
      相关资源
      最近更新 更多