map $sent_http_content_type $enableerror {
    default                    on;
    application/json          off;
}

#map $http_accept $errortype {
#    default                    /error-500.html;
#    ~application/json          /500.json;
#}

map $upstream_http_content_type $errortype {
    default                    /error-500.html;
    ~application/json          /500.json;
}

map $upstream_http_content_type $isdatatypejson {
    default                    no;
    ~application/json          yes;
}

server   {
      listen       80 default_server;
      server_name  gsmatest.mydomain.com;
      return 301 https://$server_name$request_uri;

location /status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
}

}


server {
        listen 443 ssl;
        server_name gsmatest.mydomain.com;
        ssl_protocols          TLSv1.3 TLSv1.2;
        ssl_stapling on;
        ssl_stapling_verify on;
        ssl_certificate /etc/letsencrypt/live/gsmatest.mydomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/gsmatest.mydomain.com/privkey.pem;
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
	root /var/www/html/gsmatest/web;
        ssl_prefer_server_ciphers on;
        ssl_dhparam /etc/pki/tls/certs/gsmatest.mydomain.com/dhparam.pem;
        add_header Strict-Transport-Security: max-age=63072000;
        index index.php index.html index.htm;
        #add_header "X-Debug"  $http_accept$errortype always;
        add_header "X-Debug" $http_accept$upstream_http_content_type always;
        proxy_intercept_errors on;
        error_page 502 @json502error;
        error_page 500 @json500error;

      #  location /solr {
      #      proxy_set_header Host $host;
      #     proxy_set_header X-Real-IP $remote_addr;
      #     proxy_pass "https://localhost:8983";
      # }

	     fastcgi_intercept_errors on;
             # if ($errortype = "/500.json") {
	     #fastcgi_intercept_errors off;
             #include conditionalerror.conf;
             #}
             error_page  502  504 /50x.html;

             error_page 500 $errortype;
             #error_page 500 /500.html;
             location = /500.json {
	     fastcgi_intercept_errors off;
             fastcgi_pass 127.0.0.1:9000;
                 add_header "X-Debug-2"  $sent_http_content_type;
                 add_header "X-Debug-JSON-ERROR" $http_accept$upstream_http_content_type always;
                #return 500 '{"error": {"status_code": 500,"status": "Internal Server error"}}';
                 #return 500 '{"error":{"code":500,"message":"Internal Server Error"}}';
             }

             location = /error-500.html {
                 add_header "X-Debug-2"  $sent_http_content_type always;
           #  if ($enableerror = "on") {
             #add_header 'Content-Type' 'application/json';
             root   /var/www/html/gsmatest/web/servererrorpages/error-pages-500-503/html;
           #  }
           #  add_header 'Content-Type' 'application/json';
           #  root   /var/www/html/gsmatest/web/servererrorpages/error-pages-500-503/json;
           #  if ($enableerror = "off") {
           #  add_header 'Content-Type' 'application/json';
           #  return 500 '{"error": {"status_code": 500,"status": "Internal Server error"}}';
           #  }
             }


            error_page   503 /error-503.html;
            location = /error-503.html {
            root      /var/www/html/gsmatest/web/servererrorpages/error-pages-500-503/html;
            }

        #location /apis {
        #       add_header 'Content-Type' 'application/json charset=UTF-8';
        #       return 500 '{"error": {"status_code": 500,"status": "Internal Server error"}}';
        #   }


        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
	        fastcgi_intercept_errors on;
                error_page 500 /error-500.html;
        location = /error-500.html {
             root   /var/www/html/gsmatest/web/servererrorpages/error-pages-500-503/html;
        }
                try_files $uri /index.php?$query_string;
        }

         location /apis {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
	        fastcgi_intercept_errors off;
		add_header "X-Debug-JSON-APIS" $upstream_http_content_type"abc" always;


             #   if ($isdatatypejson = "no") {
              #   add_header 'Content-Type' 'application/json' always;
              #   add_header 'Content-Type-2' $upstream_http_content_type$isdatatypejson"OK" always;
              #   return 502 '{"errors": {"status_code": 502,"status": "php-fpm server is down"}}';
            # }


		#try_files $uri /index.php?$query_string;
                #error_page 555 /dummyfile;
                #if ($errorintercept = "off") {
                fastcgi_pass 127.0.0.1:9000;
                include fastcgi.conf;
                fastcgi_param SCRIPT_FILENAME $document_root/index.php;
                #}
		add_header "X-Debug-JSON-APIS2" $upstream_http_content_type"abc" always;
                if ($upstream_http_content_type ~* "application/json") {
                add_header 'Content-Type' 'application/json' always;
                 add_header 'Content-Type-2' $upstream_http_content_type$isdatatypejson"OK" always;
                 return 502 '{"errors": {"status_code": 502,"status": "php-fpm server is down"}}';
             }
                if ($upstream_http_content_type = "") {
                add_header 'Content-Type' 'application/json' always;
		add_header 'Content-Type-3' $upstream_http_content_type$isdatatypejson"OK" always;
                 return 502 '{"errors": {"status_code": 502,"status": "php-fpm server is down"}}';
             }

        }

#        location @json502error {
#        default_type application/json;
#        return 502 '{"errors": "php-fpm server is down"}';
#    }
#        location @json500error {
#        default_type application/json;
#        return 500 '{"errors": "MySQL DB Server is down"}';
#    }
        location ^~ /simplesaml {
         alias /var/www/html/gsmatest/simplesamlphp/www;
         location ~ ^(?<prefix>/simplesaml)(?<phpfile>.+?\.php)(?<pathinfo>/.*)?$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_split_path_info ^(.+?\.php)(/.+)$;
            fastcgi_param SCRIPT_FILENAME $document_root$phpfile;
            fastcgi_param PATH_INFO       $pathinfo if_not_empty;
         }
        }
#        }



        location ~ \.php$ {
        include fastcgi.conf;
        #fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_read_timeout 300s;
        }

       location ^~ /core/install.php {
        deny all;
    }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}
