nginx;如果仅使用try\u文件存在文件,则返回响应代码

nginx;如果仅使用try\u文件存在文件,则返回响应代码,nginx,Nginx,因为我一直在尝试使用指令try_files设置配置,所以对于任何URI,都会显示维护页面和响应代码503,没有例外,即,如果存在维护文件,则包括php页面 我的配置有两个问题: php URI不显示维护页面 如果maintenance.html文件存在,则不会返回响应代码503 我见过类似的问题,但没有一个解决方案只使用try\u文件(与使用if指令相反),并且如果存在相应的文件,则无条件地提供响应代码为503的维护页面。这样的解决方案可能吗 下面是我当前的非工作conf文件。它不包含503响应

因为我一直在尝试使用指令
try_files
设置配置,所以对于任何URI,都会显示维护页面和响应代码503,没有例外,即,如果存在维护文件,则包括php页面

我的配置有两个问题:

  • php URI不显示维护页面
  • 如果maintenance.html文件存在,则不会返回响应代码503
  • 我见过类似的问题,但没有一个解决方案只使用
    try\u文件
    (与使用
    if
    指令相反),并且如果存在相应的文件,则无条件地提供响应代码为503的维护页面。这样的解决方案可能吗

    下面是我当前的非工作conf文件。它不包含503响应代码设置,因为我不知道它应该放在哪里才能像上面描述的那样工作

    worker_processes            1;
    
    error_log                   /var/log/nginx/error.log debug;
    
    events {
        worker_connections      1024;
    }
    
    http {
        include                 mime.types;
        default_type            application/octet-stream;
    
        index                   index.php index.html index.htm;
    
        server {
            listen                  80;
            server_name             rpi;
            root                    /www;
    
            location / {
                try_files           /maintenance.html $uri $uri/ /index.php?$args;
    
                # pass the PHP scripts to FastCGI server
                location ~ \.php$ {
                    try_files           $uri =404;
                    fastcgi_pass        unix:/run/php-fpm/php-fpm.sock;
                    fastcgi_index       index.php;
                    include             fastcgi.conf;
                }
            }
        }
    }
    
    我想我的问题也可以这样表述:
    try\u文件
    可以作为
    if
    控制结构使用吗?如果不是单独的,那么它是否可以与上述目标结合起来,与其他指令一起执行,而不包括
    If
    指令

    编辑:下面是一个解决方案,使用我当前使用的
    if
    ,将其包含在服务器部分:

    error_page              503 @maintenance;
    
    if (-f $document_root/maintenance.html) {
            return          503;
    }
    
    location @maintenance {
            try_files       /maintenance.html =404;
    }
    

    很好的问题!但这根本不可能,除非您调用某种脚本来设置正确的响应代码

    仅使用nginx且无if的工作解决方案 仅对最后一条语句执行内部重定向。但我们可以将其和强制内部重定向结合起来

    # This will be the HTML file to display for the 503 error.
    error_page 503 /maintenance/maintenance.html;
    
    # Let nginx know that this particular file is only for internal redirects.
    location = /maintenance/maintenance.html {
      internal;
    }
    
    # Any request that starts with the maintenance folder is 503!
    location ^~ /maintenance/ {
      return 503;
    }
    
    # Instead of checking if a file exists and directly delivering it we check
    # if a certain directory exists and trigger our index directive which will
    # perform an internal redirect for us.
    location / {
      expires epoch;
      try_files /maintenance/ $uri $uri/ /index.php?$args;
    }
    
    这种方法有什么缺点吗?
    • 实际301重定向到目录,而不是停留在相同的URL(搜索引擎)
    • 浏览器缓存301重定向可能是个问题,这就是为什么我在位置块中添加了
      expires epoch
    其他解决方案? 通过nginx配置文件 与其创建一个HTML文件,为什么不创建一个nginx配置文件并简单地重新加载该过程呢

    • 更好的表现
    • 更容易理解
    • 没有副作用
    nginx配置可能如下所示(请注意,如果一点也不邪恶,那么这个
    ):

    maintenance.conf
    文件的内容:

    set $maintenance 0;
    
    如果要激活维护模式(在shell中):

    为shell朋友提供更高级的功能 您甚至可以使用此扩展init脚本,例如,替换文件末尾的以下块:

    *)
      echo "Usage: ${NAME} {force-reload|reload|restart|start|status|stop}" >&2
      exit 1
    ;;
    
    使用以下块:

    maintenance)
      echo "set $maintenance 1;" > /etc/nginx/maintenance.conf && service nginx reload;
    ;;
    
    production)
      echo "set $maintenance 0;" > /etc/nginx/maintenance.conf && service nginx reload;
    ;;
    
    *)
      echo "Usage: ${NAME} {force-reload|reload|restart|start|status|stop|maintenance|production}" >&2
      exit 1
    ;;
    
    现在,您只需执行以下命令(包括自动完成)即可进入维护模式:

    service nginx maintenance
    
    或以下各项重新投入生产:

    service nginx production
    
    使用脚本/PHP文件 另一个非常简单的方法是使用一个PHP文件来处理它

    location / {
        try_files /maintenance.php $uri $uri/ /index.php?$args;
    }
    
    您的PHP文件看起来与HTML文件完全相同,您只需在其开头添加以下内容(假设为PHP 5.4+):

    
    
    service nginx production
    
    location / {
        try_files /maintenance.php $uri $uri/ /index.php?$args;
    }
    
    <?php http_response_code(503) ?><!doctype html>
    <html>
    <head>
    <!-- ... more html ... -->