12

Trying to set up Nginx and uWSGI on Ubuntu 13.10.

When I try to access the website, all I get is "502 Bad Gateway".

Ran apt-get install nginx uwsgi uwsgi-plugin-python3 to install nginx/uwsgi.

/etc/nginx/sites-enabled/webpage.com:

server {
        listen          80;
        server_name     webpage.com;
        access_log /var/log/nginx/webpage.com_access.log;
        error_log /var/log/nginx/webpage.com_error.log;


        location / {
            uwsgi_pass      /var/run/webpage.com.uwsgi.socket;
            include         uwsgi_params;
            uwsgi_param     Host $host;
            uwsgi_param     X-Real-IP $remote_addr;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE nginx/$nginx_version;     
        }
}

/etc/uwsgi/apps-enabled/webpage.com

[uwsgi]
vhost = true
plugin = python3
socket = /tmp/webpage.com.sock
master = true
enable-threads = true
processes = 2
home = /var/www/webpage.com/env
wsgi-file = /var/www/webpage.com/env/hello.py
virtualenv = /var/www/webpage.com/env
chdir = /var/www/webpage.com/env
touch-reload = /var/www/webpage.com/reload

/var/log/nginx/webpage.com_error.log

2014/01/17 16:28:58 [error] 25073#0: *13 connect() to unix:///var/run/webpage.com.uwsgi.socket failed (111: Connection refused) while connecting to upstream, client: 83.109.132.224, server: webpage.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///var/run/webpage.com.uwsgi.socket:", host: "webpage.com"

hello.py is just a simple hello world app.

Have been struggling with this for several hours... Now I need help :)

  • 2
    very probably your uWSGI instance is not running, check it with a "ps aux" and check uWSGI logs too – roberto Jan 17 '14 at 16:20

5 Answers5

7

Looking at the config files posted here you reference the socket in nginx as:

uwsgi_pass      /var/run/webpage.com.uwsgi.socket;

and in uwsgi as

socket = /tmp/webpage.com.sock
Paolo Casciello
  • 7,982
  • 1
  • 43
  • 42
4

I realize this doesn't have anything to do with the OP's problem, but since this is the top hit for that error message in Google, I would like to note what fixed the problem for me.

I was following a tutorial which advised to put uwsgi_pass 127.0.0.1:9090; into the nginx configuration for a Python script that had been set up in the uwsgi configuration with http-socket = :9090. the error log /var/log/nginx/error.log showed the problem: 2015/08/13 02:16:04 [error] 12566#12566: *2 upstream prematurely closed connection while reading response header from upstream, client: ::1, server: ~^(www\.)?(.+)$, request: "GET /hello/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:9090", host: "kybyz" the browser, meanwhile, was giving me the 502 Bad Gateway error.

there were two ways (at least) to fix it. the first was to change http-socket in the uwsgi configuration to simply socket (as, it turned out, the tutorial recommended; I just hadn't read it carefully enough). however, that would no longer allow me to test the script directly by pointing my browser at http://127.0.0.1:9090/, since the script now spoke uwsgi protocol instead of http. so I changed back to http-socket and changed, in the nginx configuration, the uwsgi_pass line to proxy_pass http://127.0.0.1:9090;.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • I missed that part myself following my own tutorial ... but your answer here helped me find issue. Thanks. – szabgab Mar 25 '16 at 08:18
1

This doesn't answer the OP's original question, but I had the same error in nginx, connect() to unix:///tmp/uwsgi_dev.sock failed (13: Permission denied) while connecting to upstream, and I was able to fix it by just restarting the uwsgi process completely. It's a production server, so I was hesitant to do a hard restart, but just reloading the uwsgi process didn't do the trick. Hope that helps someone.

jstaab
  • 3,449
  • 1
  • 27
  • 40
0

Typically this is a file permission problem, i.e. the uwsgi socket file is not readable by the nginx process. Check the socket file's permission and its parent folder and its grandparent folder, etc. You can do this with one command (suppose your nginx process is running by user nginx):

su nginx -c "[[ -r sockfile ]] && echo ok"
Cyker
  • 9,946
  • 8
  • 65
  • 93
0

For my Python Django app on AWS, it's just overloaded.

First I added more servers and noticed that C-instances (compute) are better than general purpose (M) -instances since the M -instances had 66% of CPU load running as steal (used by some other customers) after running out the CPU credits.

But after running the app for some time (and after having the number of incoming requests grown 5x in short time period), I also checked the database performance (RDS), and it was running at 100%. I also grew the Database instance size from 4 CPU to 8 CPU and now it works again without errors.

PHZ.fi-Pharazon
  • 1,479
  • 14
  • 15