apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
  namespace: laravelapitemplates
data:
  nginx.conf : |
    user  nginx;
    worker_processes  auto;

    events {
      worker_connections 4096;
      multi_accept on;
      use epoll;
    }

    http {
      include       mime.types;
      server_tokens off;
      default_type  application/octet-stream;

      client_body_buffer_size 10K;
      client_header_buffer_size 1k;
      client_max_body_size 10m;
      large_client_header_buffers 4 16k;

      access_log off;
      error_log /dev/stderr;

      sendfile on;

      keepalive_timeout  65;
      keepalive_requests 100;

      log_format json_combined escape=json
        '{'
          '"time_local":"$time_local",'
          '"remote_addr":"$remote_addr",'
          '"remote_user":"$remote_user",'
          '"request":"$request",'
          '"status": "$status",'
          '"body_bytes_sent":"$body_bytes_sent",'
          '"request_time":"$request_time",'
          '"http_referrer":"$http_referer",'
          '"http_user_agent":"$http_user_agent"'
        '}';

      server {
        listen 80;
        server_name laravelapitemplates.com.br www.laravelapitemplates.com.br;
        index index.php index.html;
        root /usr/share/nginx/html;

        if ($host = "www.laravelapitemplates.com.br") {
          rewrite ^ https://laravelapitemplates.com.br$request_uri? permanent;
        }

        if ($http_x_forwarded_proto = "http") {
          rewrite ^ https://laravelapitemplates.com.br$request_uri? permanent;
        }

        add_header 'Access-Control-Allow-Origin:' '*' 'always';
        add_header 'Access-Control-Allow-Credentials:' 'true' 'always';
        add_header 'Access-Control-Allow-Methods:' 'GET, HEAD, POST, PUT, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers:' 'Authorization, Cache-Control, Content-Type, Keep-Alive, Origin, X-Requested-With' 'always';
        add_header 'Referrer-Policy' 'same-origin';
        add_header 'Feature-Policy' "geolocation 'none'; vibrate 'none'";
        add_header 'Strict-Transport-Security' 'max-age=31536000; includeSubdomains; preload';
        add_header 'X-Content-Type-Options' 'nosniff';
        add_header 'X-Frame-Options' 'SAMEORIGIN';
        add_header 'X-XSS-Protection' '1; mode=block';

        gzip on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;

        location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|ttf|woff|woff2|svg|xml|json)$ {
          root /static;
          expires 365d;
          access_log off;
          etag on;
          if_modified_since exact;
          add_header Pragma "public";
          add_header Cache-Control "max-age=31536000, public";
          add_header Access-Control-Allow-Origin *;
          try_files $uri =404;
        }

        location / {
          try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass app:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
          fastcgi_read_timeout 180;
          proxy_set_header Host            $http_host;
          proxy_set_header X-Real-IP       $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page 503 @503_json;
        error_page 502 @502_json;
        error_page 500 @500_json;
        error_page 404 @404_json;

        location @503_json {
          return 503 '{"data": {"message": "The server is temporary unable to serve your request"}, "meta": {"timestamp": $msec}}';
        }

        location @502_json {
          return 502 '{"data": {"message": "The server encountered a temporary error and could not complete your request"}, "meta": {"timestamp": $msec}}';
        }

        location @500_json {
          return 500 '{"data": {"message": "There was an error. Please try again later"}, "meta": {"timestamp": $msec}}';
        }

        location @404_json {
          return 404 '{"data": {"message": "The requested resource was not found"}, "meta": {"timestamp": $msec}}';
        }
      }
    }
