title: "upstream prematurely closed connection while reading response header from upstream" with nginx, PostgreSQL and Mono on Ubuntu MVC4 app
body: <p>/Edit I  done the suggestion of <a href="https://stackoverflow.com/questions/24872394">the answer to the question mentioned above</a> to give access rights to the registry as follows:</p>
  
  <pre><code>sudo mkdir -p /usr/local/etc/mono/registry
  sudo chmod uog+rw /usr/local/etc/mono/registry
  </code></pre>
  
  <p>This did not help, as the bad gateway problem only appears <em>after</em> having given access to the registry with the above commands (before that I got the registry error).</p>
  
  <p><strong>Question</strong>:
  I want to deploy a MVC 4 app running on Mono runtime with nginx to an Ubuntu server of Azure. By selecting such a Ubuntu server and running an installer script I managed to have a default web application running and visible on oogstplanner.cloudapp.net. The installer worked as follows:</p>
  
  <pre><code>wget https://bitbucket.org/mindbar/install-mono/raw/master/install-nginx- 
  mono.sh &amp;&amp; sudo chmod +x install-nginx-mono.sh &amp;&amp; ./install-nginx-mono.sh
  </code></pre>
  
  <p>The script installed nginx and mono and setup the nginx.conf and other configuration files.</p>
  
  <p>What I then did was remove this installation, installed postgresql and setup the database, and replaced the files in $HOME/www/ with my own MVC 4 web application which works locally by copying it there. Then when running it I see a</p>
  
  <p><img src="https://i.stack.imgur.com/z9aHf.png" alt="&gt;502 Bad Gateway"> </p>
  
  <p>error. </p>
  
  <p>The log file /var/log/nginx/error.log says (for the first and subsequent requests):</p>
  
  <blockquote>
    <p>2015/04/03 14:24:36 [error] 1305#0: *6 upstream prematurely closed connection while reading response header from upstream, client: my.ip.207, server: localhost, request: "GET /Account/Login?ReturnUrl=%2f HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "oogstplanner.cloudapp.net"<br>
    2015/04/03 14:26:05 [error] 1305#0: *8 connect() failed (111: Connection refused) while connecting to upstream, client: my.ip.207, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "oogstplanner.cloudapp.net"<br>
    2015/04/03 14:29:24 [error] 1305#0: *10 connect() failed (111: Connection refused) while connecting to upstream, client: my.ip.58, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "oogstplanner.cloudapp.net" <br> (...)</p>
  </blockquote>
  
  <p>So it does see that it needs to go to /Account/Login?ReturnUrl! Something <em>is</em> working.</p>
  
  <p>When I copy back a default app it works again.</p>
  
  <p>Configuration:</p>
  
  <ul>
  <li><a href="https://gist.github.com/erooijak/8b1f361ef38e44b6dc30" rel="nofollow noreferrer">Here is my /etc/nginx/nginx.conf</a>.</li>
  <li><a href="https://gist.github.com/erooijak/daf7f433672b453f68cb" rel="nofollow noreferrer">Here is my /etc/nginx/sites-available/mono-default</a> (same for sites-enabled/monodefault)</li>
  </ul>
  
  <p>/edit I have a strong suspicion that it has to do with the PostgreSQL database. This is because I just rebooted the server, and saw this NullReferenceException being thrown by NauckIt.PostgreSQLProvider:</p>
  
  <p><img src="https://i.stack.imgur.com/Pk9Vk.png" alt="What I saw after a reboot "></p>
  
  <p>This error always happens locally <em>once</em>, and then after a refresh the app works. But on the server the app does not work, but after not having the PostgreSQL provider throw a NullReferenceException I get the 502 error. So that is why I think the bad gateway might come from the PostgreSQL connection.</p>
  
  <p>/Edit2 The plot thickens: when I look into the database I see (some) sessions being created in the Sessions table. I don't see any logic when that happens and have been unable to create a new one. But there has been a connection from the web app to the database sometimes.</p>
  
  <p>Here is the PostgreSQL log after a restart (at 14:25:23) and navigating to the website via a browser (at 14:24:36):</p>
  
  <blockquote>
    <p>2015-04-03 14:24:23 UTC LOG:  database system is ready to accept connections<br>
    2015-04-03 14:24:23 UTC LOG:  autovacuum launcher started<br>
    2015-04-03 14:24:23 UTC LOG:  incomplete startup packet<br>
    2015-04-03 14:24:36 UTC LOG:  could not receive data from client: Connection reset by peer<br></p>
  </blockquote>
  
  <p>According to a reddit user: </p>
  
  <blockquote>
    <p>502 Bad Gateway is an HTTP status code and cannot be returned by postgres because postgres doesn't speak HTTP. Similarly, NullReferenceException is a Mono exception and isn't thrown by postgres, so that's not a postgres problem either. Your application is attempting to send some data to postgres but crashes before it can finish doing so. This is an application problem, not a postgres problem. Hope this helps.</p>
  </blockquote>
  
  <p>Does anyone know what is wrong here?</p>
  
  <p>PS I have all steps I've taken documented under the Deployment part in a <a href="https://github.com/erooijak/zaaikalender/blob/master/README.md" rel="nofollow noreferrer">README</a> file.</p>