Nginx as a frontend for Elasticsearch on Ubuntu 14.04

Many of these steps come from this wonderful blog post here.

  1. Follow steps one through six here for installing Nginx.
  2. Install Java, either OpenJDK (#1) or Oracle Java (#2)
    1. apt-get install openjdk-6-jre
    2. sudo add-apt-repository ppa:webupd8team/java
      apt-get update
      sudo apt-get install oracle-java7-installer
  3. Test that Java is installed:
    java -version
  4. Download the deb file for Elasticsearch (below is 1.3.2, make sure you get the most recent one from the official location)
    wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.3.2.deb
  5. Install the deb file
    dpkg -i elasticsearch-1.3.2.deb
  6. Set Elasticsearch to start on boot
    sudo update-rc.d elasticsearch defaults 95 10
  7. Start Elasticsearch
    sudo service elasticsearch start
  8. Test to see if it is running. NOTE: Elasticsearch sometimes takes a couple of seconds to spin up. If you get a message saying Failed to connect to localhost port 9200: Connection refused wait a couple of seconds and try again.
    curl localhost:9200
  9. Secure Elasticsearch to only allow local connections
    sudo vi /etc/elasticsearch/elasticsearch.yml
    1. Comment out (if they aren’t already) these two lines:
      #network.bind_host: #some_value
      #network.publish_host: #some_other_value 
    2. Uncomment and set this line:
      network.host: localhost
  10. Restart elasticsearch
    sudo service elasticsearch restart
  11. For Nginx we’re just going to modify the default site
    sudo vi /etc/nginx/sites-enabled/default
  12. Replace everything with the below, changing example.com with your domain
    server {
        listen 80;
        server_name example.com;
        location / {
            rewrite ^/(.*) /$1 break;
            proxy_ignore_client_abort on;
            proxy_pass http://localhost:9200;
            proxy_redirect http://localhost:9200 http://example.com/;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header  Host $http_host;
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.