首页 > 解决方案 > Nginx 没有错误页面显示,无论我使用什么配置

问题描述

我正在尝试在 nginx 中为 500 和 502 错误代码配置错误页面,我尝试了许多不同的配置选项和解决方案,但没有一个对我有用。

问题本身是,无论我如何进行配置,我总是得到带有 502 bad gateway 的通用 Nginx 错误页面。

以下 docker 堆栈与这些容器一起运行:

一个 TYPO3 系统在 php/composer 容器后面运行。

我正在使用 Nginx 而不是 Apache Web 服务器。下面你可以看到我当前的 nginx 配置。

server {
    listen 80;

    root /var/www/html/public;

    index index.php index.htm index.html;

    # Make site accessible from http://localhost/
    server_name _;

    # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
    sendfile off;
    error_log /dev/stdout info;
    access_log /var/log/nginx/access.log;

    # NGINX - Provide error page
    error_page 500 502 /error.html;
    location = /error.html {
        internal;
    }

    ## provide a health check endpoint
    location /healthcheck {
        access_log off;
        stub_status     on;
        keepalive_timeout 0;    # Disable HTTP keepalive
        return 200;
    }

    location / {
        absolute_redirect off;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on socket
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass ${PHP_DOMAIN}:9000;
        fastcgi_buffers 16 128k;
        fastcgi_buffer_size 128k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_intercept_errors off;
        # fastcgi_read_timeout should match max_execution_time in php.ini
        fastcgi_read_timeout 600;
        fastcgi_param SERVER_NAME $host;
        fastcgi_cache_bypass $http_x_blackfire_query;
    }

    # Expire rules for static content
    # Feed
    location ~* \.(?:rss|atom)$ {
        expires 1h;
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    # Prevent clients from accessing hidden files (starting with a dot)
    # This is particularly important if you store .htpasswd files in the site hierarchy
    # Access to `/.well-known/` is allowed.
    # https://www.mnot.net/blog/2010/04/07/well-known
    # https://tools.ietf.org/html/rfc5785
    location ~* /\.(?!well-known\/) {
        deny all;
    }

    # Prevent clients from accessing to backup/config/source files
    location ~* (?:\.(?:bak|conf|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    # TYPO3 - Block access to composer files
    location ~* composer\.(?:json|lock) {
        deny all;
    }

    # TYPO3 - Block access to flexform files
    location ~* flexform[^.]*\.xml {
        deny all;
    }

    # TYPO3 - Block access to language files
    location ~* locallang[^.]*\.(?:xml|xlf)$ {
        deny all;
    }

    # TYPO3 - Block access to static typoscript files
    location ~* ext_conf_template\.txt|ext_typoscript_constants\.(?:txt|typoscript)|ext_typoscript_setup\.(?:txt|typoscript) {
        deny all;
    }

    # TYPO3 - Block access to miscellaneous protected files
    location ~* /.*\.(?:bak|co?nf|cfg|ya?ml|ts|typoscript|dist|fla|in[ci]|log|sh|sql)$ {
        deny all;
    }

    # TYPO3 - Block access to recycler and temporary directories
    location ~ _(?:recycler|temp)_/ {
        deny all;
    }

    # TYPO3 - Block access to configuration files stored in fileadmin
    location ~ fileadmin/(?:templates)/.*\.(?:txt|ts|typoscript)$ {
        deny all;
    }

    # TYPO3 - Block access to libaries, source and temporary compiled data
    location ~ ^(?:vendor|typo3_src|typo3temp/var) {
        deny all;
    }

    # TYPO3 - Block access to protected extension directories
    location ~ (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ {
        deny all;
    }

    if (!-e $request_filename) {
        rewrite ^/(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ /$1.$3 last;
    }

    #Include development locations if needed
    include /etc/nginx/conf.d/locations/*.conf;
}

我认为问题本身不是来自配置,而是来自其他任何地方,但我不知道在哪里..我找不到问题。

希望你们能帮助我,顺便说一句,这是我的第一个堆栈溢出问题:D

编辑:

刚刚添加了下面的配置来测试错误代码,但不幸的是我仍然得到一个 502 Bad Gateway,可能是本地设置的问题。令我惊讶的是,健康检查的配置位置正在工作,只是错误页面没有。

location /get_error {
        return 500;
    }

更新:

配置本身是正确的,我刚刚部署了对开发系统所做的更改,它就可以正常工作!我不知道为什么以及问题出在哪里,但它不适用于我的本地开发环境。

标签: phpazuredockernginxconfiguration

解决方案


推荐阅读