events {
  worker_connections 768;
}

http {
  gzip on;
  gzip_proxied any;
  gzip_vary on;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_min_length 1000;

  proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:30m;

  # Define a server for odoo backend (port 8069, as configured on the odoo.conf file (odoo file) )
  upstream odoo-backend {
    server 127.0.0.1:8069;
  }

  # Define a server for longpolling (port 8072, as configured on the odoo.conf file (odoo file) )
  upstream odoo-lp{
    server 127.0.0.1:8072;
  }

  server {
    listen 8039;
    proxy_http_version 1.1;

    location /longpolling {
      proxy_connect_timeout 600s;
      proxy_send_timeout 600s;
      proxy_read_timeout 600s;
      send_timeout 600s;
      proxy_pass http://odoo-lp;
    }

    location /websocket {
        proxy_redirect off;
        proxy_pass http://odoo-lp;
    }

    location ~ /[a-zA-Z0-9_-]*/static/ {
      proxy_pass http://odoo-backend;
      proxy_cache_valid 200 60m;
      proxy_buffering on;
      expires 864000;
    }

    location / {
      # Reject requests with unsupported HTTP method
      if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
        return 405;
      }
      proxy_pass http://odoo-backend;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-NginX-Proxy true;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-Host $host;
      add_header Content-Security-Policy "upgrade-insecure-requests";
      add_header Strict-Transport-Security max-age=15768000;
      add_header X-Static no;
      proxy_redirect off;
    }

  }
}