为什么自定义错误页面在我的nginx配置中不起作用?

时间:2014-07-16 06:08:26

标签: nginx config

我有以下服务器阻止:

server {
    listen 80;
    server_name utsav-dev.domain.org;
    root /var/www/domain-utsav-dev/web;
    access_log /var/log/nginx/domain-utsav-dev-access.log;
    error_log  /var/log/nginx/domain-utsav-dev-error.log error;
    error_page 404 = /var/www/domain/web/50x.html;

    set $thttps $https;
    set $tscheme $scheme;
    if ($http_x_forwarded_proto = http) {
      set $thttps off;
      set $tscheme "http";
    }
    if ($http_x_forwarded_proto = HTTP) {
      set $thttps off;
      set $tscheme "http";
    }



    index app.php index.html index.htm;
    try_files $uri $uri/ @rewrite;
    location @rewrite {
        rewrite ^/(.*)$ /app.php/$1;
    }
    # CSS and Javascript
    location ~* \.(?:css|js)$ {
      expires 86400;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~ \.php {
        fastcgi_index app.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION "www";
        fastcgi_param APPLICATION_ENV "dev";
    fastcgi_param PDFLIBLICENSEFILE "/etc/php5/fpm/pdflib.license";
        fastcgi_param   HTTPS           $thttps;
    }

    location ~ /\.ht {
        deny all;
    }
   chunked_transfer_encoding off;

}

现在根据脚本如果找不到页面,nginx应该在内部重定向到50x.html但是当我尝试打开 http://utsav-dev.domain.org/blah.html 时,它会给我错误&#34 ;找不到文件" ,而不是我期待的自定义404页面。为什么呢?

1 个答案:

答案 0 :(得分:1)

尝试在fastcgi位置设置fastcgi_intercept_errors on;

fastcgi_intercept_errors on | off; Determines whether FastCGI server responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.

访问http://utsav-dev.domain.org/blah.html时:

  • 首先检查每个位置。没有人打
  • 它通过try_files。点击@rewrite位置。
  • uri改为/app.php/blah.html
  • 点击fastcgi位置。

您应该设置fastcgi_intercept_errors on;error_page工作