简体   繁体   English

使用SSL和Nginx运行Puma

[英]Running Puma with SSL and Nginx

I have Angular apps running on a Rails API. 我有在Rails API上运行的Angular应用程序。

  • Web Server: Nginx Web服务器:Nginx
  • App Server: Puma 应用服务器:Puma
  • SSL Certificate: LetsEncrypt SSL证书:LetsEncrypt
  • Hosted on: AWS EC2 running Ubuntu 16.04.3 LTS 托管于:运行Ubuntu 16.04.3 LTS的AWS EC2

I am able to serve my Angular apps with SSL but I am having issues with running the Rails API on SSL. 我可以使用SSL为Angular应用提供服务,但是在SSL上运行Rails API时遇到问题。 In production.rb, I have added config.force_ssl = true . 在production.rb中,我添加了config.force_ssl = true I run the Rails server using RAILS_ENV=production rails server --binding=*public ip of instance* . 我使用RAILS_ENV=production rails server --binding=*public ip of instance*运行Rails服务器。 Here is my nginx config file for the api: 这是我的api的nginx配置文件:

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: 这是我包含的snakeoil.conf文件:

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. 当我向api.domain.com发出请求时,出现502错误网关错误。 Rails throws this 2018-02-19 07:07:02 +0000: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails. Rails抛出此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? 这是配置问题吗?还是我必须更改应用程序代码中的某些内容?

Puma understands only http requests but you are forcing it to handle https requests. Puma仅理解http请求,但是您正在强迫它处理https请求。

Replacing "proxy_pass https://app " with "proxy_pass http://app " should fix your problem. 用“ proxy_pass http:// app ”替换“ proxy_pass https:// app ”应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM