1
votes

I'm sending an AJAX DELETE request through my Nginx server to rails, I'm able see that the db is getting updated correctly and the response from the rails server is as expected. But the response status is not a success but an error status.

When I alert the xhr.status it says 0.

I have the following load balancing setup using Nginx -

I have 2 rails servers on ports 3000 and 3001. I need to accept https requests from the browser and redirect them to the rails servers. I have the following Nginx Conf file -

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    passenger_root /home/hkr/.rvm/gems/ruby-2.2.0@global/gems/passenger-5.0.6;
    passenger_ruby /home/hkr/.rvm/wrappers/ruby-2.2.0/ruby;

include       mime.types;
default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

gzip  on;

upstream web-cluster {
    ip_hash;
    server localhost:3001;
    server localhost:3000;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

server {
    listen       443 ssl;
    server_name  ruby-trail.com;
    root PATH_TO_PROJECT/public;
    passenger_enabled on;
    rails_env development;

    ssl_certificate      /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key  /etc/nginx/ssl/nginx.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    error_log /var/log/nginx/my-error.log;
    access_log /var/log/nginx/my-access.log;        

    location / {

        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        index  $uri $uri/ index.html index.htm;
        proxy_pass http://web-cluster$request_uri;
        proxy_redirect  http://localhost:3001 https://ruby-trail.com;
    }
}   

When I'm sending an ajax DELETE request the rails server is responding with the correct expected response but the response is not getting an OK 200 status.

This is from the NGINX error log -

2015/04/21 20:28:07 [error] 23316#0: *18 upstream sent no valid HTTP/1.0 header while reading response header from upstream, client: 127.0.0.1, server: ruby-trail.com, request: "DELETE /links/48.json HTTP/1.1", upstream: "http://127.0.0.1:3001/links/48.json", host: "ruby-trail.com", referrer: "https://ruby-trail.com/groups/18"

This is from the NGINX access log -

127.0.0.1 - - [21/Apr/2015:20:28:07 -0500] "DELETE /links/48.json HTTP/1.1" 009 1044 "https://ruby-trail.com/groups/18" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36"

There is no issues when I hit the rails servers directly its only with the Nginx setup I'm getting the upstream sent no valid HTTP/1.0 header error.

Please let me know where I'm making a mistake. Thanks in advance.

1

1 Answers

3
votes

The issue had nothing to do with the Nginx configuration. The server was responding with a 0 status as the backend controller code in Rails was incorrect.

On a delete request I was returning an incorrect Json response as below.

def destroy     
    @link = Links.find(params[:id])
    @link.destroy    
    respond_to do |format|
    format.json { render json: @link, status: "success" } // incorrect
end

The json response needs to be as follows -

def destroy     
        @link = Links.find(params[:id])
        @link.destroy    
        respond_to do |format|
        format.json { head :no_content } //correct
end

This was the reason why the Jquery Ajax call was executing the error code block instead of the success code.