Elasticsearch very unreliable in returning results with NGINX and UWSGI
Question: **Summary** : _I have a website with a search-as-you type search through Flask
and Elasticsearch. This works perfectly on my local machine, and on my server
(a Vultr droplet) when I run flask directly. However, when I run the website
through Nginx and uWSGI it suddenly becomes unreliable, returning some results
but not others. I have no idea how to troubleshoot this and could use some
suggestions or pointers!_

I'll try to provide as much info as possible with regards to my setup:

## Elasticsearch

Server health:

    
    
       {
          "cluster_name" : "elasticsearch",
          "status" : "green",
          "timed_out" : false,
          "number_of_nodes" : 1,
          "number_of_data_nodes" : 1,
          "active_primary_shards" : 1,
          "active_shards" : 1,
          "relocating_shards" : 0,
          "initializing_shards" : 0,
          "unassigned_shards" : 0,
          "delayed_unassigned_shards" : 0,
          "number_of_pending_tasks" : 0,
          "number_of_in_flight_fetch" : 0,
          "task_max_waiting_in_queue_millis" : 0,
          "active_shards_percent_as_number" : 100.0
        }
    

Here is pastebin with [info from my index](http://pastebin.com/gTurY1wG)

## Javascript, AJAX and Flask

As the user starts typing, jQuery autocomplete sends out an AJAX request that
gets forwarded to elasticsearch (through Flask), where a query is run. The
results are returned to javascript and added to the HTML.

I doubt there's a problem here since it works perfectly until I use Nginx, but
regardless here is the info:

  * [This is the javascript that uses jQuery autocomplete and AJAX with GET](http://pastebin.com/UgFJByrj). 
  * This calls get_search_results() in the [views.py class](http://pastebin.com/XHfPJ1ye) with an AJAX call
  * Lastly get_search_results() gets autocomplete results through the [search_database() function](http://pastebin.com/6Bz51XtC)

## Working correctly

When I run my flask application through my debugging script it works
perfectly:

    
    
    #!flask/bin/python
    from app import application
    application.run(host='0.0.0.0', debug=True)
    

Whenever I start typing autocomplete works promptly and returns the correct
results.

## The problem

However, when I run the website as a service through Nginx and uWSGI search
results work sometimes, but not others times. Some names are returned, but
most are not. Partial strings are almost never returned. I have my server
setup in the following way (I basically followed [this
tutorial](https://www.digitalocean.com/community/tutorials/how-to-serve-flask-
applications-with-uwsgi-and-nginx-on-ubuntu-14-04)):

This runs the **flask** application:

    
    
    #!flask/bin/python
    from app import application
    
    if __name__ == "__main__":
        application.run()
    

This module gets called by the following **uWSGI** .ini that creates a socket:

    
    
    [uwsgi]
    module = run_wsgi
    
    master = true
    processes = 5
    
    socket = transfer_website.sock
    chmod-socket = 660
    vacuum = true
    
    die-on-term = true
    

uWSGI gets launched with this .ini by an **upstart script** :

    
    
    description "uWSGI server instance configured to serve Transfer Website"
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    setuid admin
    setgid www-data
    
    env PATH=/home/admin/transfer_website/transfer-virt-env/bin
    chdir /home/admin/transfer_website
    exec uwsgi --ini transfer_website.ini
    

Lastly the server accepts requests through **Nginx** that forwards requests to
the website socket. This file is in /etc/nginx/sites (I've redacted the IP
address of the website):

    
    
    server {
        listen 80;
        server_name /*my.server.ip.address*/;
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/home/admin/transfer_website/transfer_website.sock;
        }
    }
    

I've checked the elasticsearch logs and there does not seem to be anything
related to this problem at all. My guess is that the problem lies somewhere in
the forwarding and AJAX calls but I have no idea how to debug this.

Sorry if this post is vague or if I left our crucial information, I'd be happy
to provide it.


Answer: Alright I finally solved it.

First I isolated the problem by looking at the chrome inspect -> network tab.
The results that weren't returned were producing a 500 error.

After some quick googling, I then [found a
way](https://www.digitalocean.com/community/questions/how-to-check-error-logs-
for-flask-uwsgi-nginx-app) to debug and log the uwsgi run flask application
(and the 500 error). I added the following line to my uwsgi .ini file:

    
    
    #location of log files
    logto = file_name.log
    

This logs all the output from the Flask application to this logfile.

However I still wasn't getting the error, instead the application seemed to
ignore it and continue. [This was ultimately
fixed](http://stackoverflow.com/a/18063897/4772958) by setting
`PROPAGATE_EXCEPTIONS` to `True` in my config file.

If `Debug = False` the exceptions don't propagate in the interest of not
crashing the server. So it has to be explicitly set to force the errors to
show up in the log.

Turns out I had some silly error where I tried printing a string that was
giving a unidecode error because of encoding problems. Why this wasn't a
problem on the local version I'll never know

