"upstream prematurely closed connection while reading response header from upstream" with nginx, PostgreSQL and Mono on Ubuntu MVC4 app

14,520

The problem might be caused from originally disabling the http port and allowing only https. If have not been enabled, try to enable http port, only allow requests through 127.0.0.1. In addition to this also check the "timeout" values in the configuration settings. Hope this helps...

Share:
14,520
user2609980
Author by

user2609980

Updated on June 04, 2022

Comments

  • user2609980
    user2609980 about 2 years

    /Edit I done the suggestion of the answer to the question mentioned above to give access rights to the registry as follows:

    sudo mkdir -p /usr/local/etc/mono/registry
    sudo chmod uog+rw /usr/local/etc/mono/registry
    

    This did not help, as the bad gateway problem only appears after having given access to the registry with the above commands (before that I got the registry error).

    Question: 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:

    wget https://bitbucket.org/mindbar/install-mono/raw/master/install-nginx- 
    mono.sh && sudo chmod +x install-nginx-mono.sh && ./install-nginx-mono.sh
    

    The script installed nginx and mono and setup the nginx.conf and other configuration files.

    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

    >502 Bad Gateway

    error.

    The log file /var/log/nginx/error.log says (for the first and subsequent requests):

    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"
    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"
    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"
    (...)

    So it does see that it needs to go to /Account/Login?ReturnUrl! Something is working.

    When I copy back a default app it works again.

    Configuration:

    /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:

    What I saw after a reboot

    This error always happens locally once, 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.

    /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.

    Here is the PostgreSQL log after a restart (at 14:25:23) and navigating to the website via a browser (at 14:24:36):

    2015-04-03 14:24:23 UTC LOG: database system is ready to accept connections
    2015-04-03 14:24:23 UTC LOG: autovacuum launcher started
    2015-04-03 14:24:23 UTC LOG: incomplete startup packet
    2015-04-03 14:24:36 UTC LOG: could not receive data from client: Connection reset by peer

    According to a reddit user:

    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.

    Does anyone know what is wrong here?

    PS I have all steps I've taken documented under the Deployment part in a README file.

  • user2609980
    user2609980 about 9 years
    Thanks. Could you specify where I can configure those settings? On Azure or in the Nginx settings? And if the latter, which file? And does this explain why it does work with an application without a database?
  • Murat Yıldız
    Murat Yıldız about 9 years