【问题标题】:How to serve static assets with Nginx in maintenance mode (503) [duplicate]如何在维护模式下使用 Nginx 提供静态资产(503)[重复]
【发布时间】:2014-08-23 09:22:53
【问题描述】:

我在我的网站服务器上使用 Nginx 作为前端代理。 当我处于维护模式时,我想用它来将用户重定向到我的 web 应用程序或维护 php 页面。

这是我的服务器指令:

server {
    listen               443;

    return 503;
    error_page           503 @maintenance;
    root                /usr/maintenance;
    location @maintenance {
        fastcgi_pass     php-fpm;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include          /etc/nginx/fastcgi_params;
    }

    location / {
        proxy_pass       https://webapp;
    }
}

如果我取消注释 return 503,则会向客户端发送 503 响应,否则会发送 Web 应用。

我的 PHP 503 错误页面按预期显示,但问题是它具有静态资产(css、图像、js),当 Chrome 尝试加载它们时我得到 503 返回码。资产位于根目录中。

我该如何解决这个问题? 有没有比评论/取消评论return 503 更好的方法来处理维护和实时模式?

谢谢

【问题讨论】:

    标签: nginx


    【解决方案1】:

    经过一番研究,我找到了一种优雅的方法。 来源:http://blog.mythictechnologies.com/2011/02/10/setting-a-maintenance-page-with-nginx/

    这是我的新配置

    server {
        listen               443;
    
        error_page           503 @maintenance;
        root                /usr/maintenance;
        location @maintenance {
            fastcgi_pass     php-fpm;
            fastcgi_index    index.php;
            fastcgi_param    SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include          /etc/nginx/fastcgi_params;
        }
    
        location ~* \.(css|png|js|jpg|jpeg) {
            # The file will be returned
        }
    
        location / {
            return           503;
            proxy_pass       https://webapp;
        }
    }
    

    您可以随意调整正则表达式 \.(css|png|js|jpg|jpeg),但将文件列入白名单似乎是个好主意。

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2018-07-17
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多