1

I have Angular apps running on a Rails API.

  • Web Server: Nginx
  • App Server: Puma
  • SSL Certificate: LetsEncrypt
  • Hosted on: AWS EC2 running Ubuntu 16.04.3 LTS

I am able to serve my Angular apps with SSL but I am having issues with running the Rails API on SSL. In production.rb, I have added config.force_ssl = true. I run the Rails server using RAILS_ENV=production rails server --binding=*public ip of instance*. Here is my nginx config file for the api:

upstream app{
    server localhost:3000;
}
server {
    listen 80;
    listen [::]:80;

    server_name api.domain.com;
    return 302 https://$server_name$request_uri;
}
server{

    #SSL Configuration

    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/snakeoil.conf;

    server_name api.domain.com;
    location / {
            proxy_pass https://app;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

And this is the snakeoil.conf file that I have included:

ssl_certificate /etc/letsencrypt/live/www.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.domain.com/privkey.pem;

When I make a request to api.domain.com, I get a 502 Bad Gateway error. Rails throws this 2018-02-19 07:07:02 +0000: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.

Is this an issue with the configuration or do I have to change something in the application code?

2
  • if you are hit api vai http then replace it with https as you are using SSL. Commented Feb 19, 2018 at 7:17
  • 1
    I am hitting with https. Moreover, the first server block will anyway redirect request to https. Commented Feb 19, 2018 at 7:27

1 Answer 1

3

Puma understands only http requests but you are forcing it to handle https requests.

Replacing "proxy_pass https://app" with "proxy_pass http://app" should fix your problem.

1
  • Great. You saved me two hours.
    – Lane
    Commented Apr 17 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.