5

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

Community
  • 1
  • 1
user2609980
  • 10,264
  • 15
  • 74
  • 143
  • 1
    possible duplicate of [Access to the path "/etc/mono/registry" is denied](http://stackoverflow.com/questions/24872394/access-to-the-path-etc-mono-registry-is-denied) – Sami Kuhmonen Mar 31 '15 at 08:41
  • a NullReferenceException is always a programming defect that a developer has introduced, so you should file a bug or contribute a patch – knocte Apr 04 '15 at 13:05
  • The error is happening because something is unexpectedly null inside `PgSessionStateStoreProvider` - looking at the source at http://dev.nauck-it.de/attachments/5/PgSessionStateStoreProvider.cs I'm guessing it's `item`, but without a line number in the stack trace it's only a guess, and I don't know enough to say why this would be happening - just posting here in case it triggers something. – CupawnTae Apr 09 '15 at 16:08
  • I would suggest testing your application under Windows VM with IIS + postgreSql, that will be easier to debug your own application errors/exceptions. Once it is done, then you can think of deploying over linux. Also you need to log your exceptions using log4net or any other library and make sure all exceptions are logged including AppDomain UnhandledException event. nginx is a proxy server, it is difficult to test app behind proxy, I would suggest first test app under localhost with direct access to webserver. – Akash Kava Apr 10 '15 at 05:50
  • @MuratYıldız It was a clean server with a fresh install. How does your answer explain the fact that using a "Hello world" app does work? Something with PostgreSQL? Maybe you can elaborate a bit more. – user2609980 Mar 22 '16 at 11:57

2 Answers2

3

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.

Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
  • 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? – user2609980 Apr 07 '15 at 16:24
  • Please check the following links: http://serverfault.com/questions/632527/upstream-prematurely-closed-connection-while-reading-response-header-from-upstre http://forum.nginx.org/read.php?2,254031 https://www.digitalocean.com/community/questions/nginx-upstream-prematurely-closed-connection-while-reading-response-header-from-upstream – Murat Yıldız Apr 10 '15 at 08:45
1

Same error can appear in case if you are using MS SQL Server and found some exceptions in your application log, try to turn off Trusted_Connection option in SQL server connection string:

"ConnectionString": "Data Source=127.0.0.1;Database=db;User Id=dbuser;password=dbpassword;Trusted_Connection=false"
Tropin Alexey
  • 606
  • 1
  • 7
  • 16