nginx 空白页与 puma 和 rails 在 beanstalk 上

nginx 空白页与 puma 和 rails 在 beanstalk 上

我尝试将 rails api 部署到 elastic beanstalk,并使用 nginx 和 puma 作为应用服务器。部署成功,数据库已创建并迁移。

但是,当访问网页时,它只显示白页。

我可以看到该请求记录在 nginx 访问日志中,但是它没有记录在 puma 日志中。

以下是 nginx 配置:

> user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024; 
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] 
"$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

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

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

index   index.html index.htm;

server {
    listen       80 ;
    listen       [::]:80 ;
    server_name  localhost;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    # redirect server error pages to the static page /40x.html
    #
    error_page 404 /404.html;
        location = /40x.html {
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

conf 使用 include 指令,其内容如下:upstream my_app { server unix:///var/run/puma/my_app.sock;}

log_format healthd '$msec"$uri"'
                '$status"$request_time"$upstream_response_time"'
                '$http_x_forwarded_for';

server {
  listen 80;
  server_name _ localhost; # need to listen to localhost for worker tier

  if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
  }

  access_log  /var/log/nginx/access.log  main;
  access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

  location / {
    proxy_pass http://my_app; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location /assets {
    alias /var/app/current/public/assets;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
  }

  location /public {
    alias /var/app/current/public;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
  }
}

我在这里遗漏了什么?

谢谢!

答案1

检查你的日志文件,可能是 nginx 配置不正确,如果是的话,你可以参考这些帖子寻找解决方案(https://stackoverflow.com/questions/49360715/elastic-beanstalk-customize-puma-configuration)(https://stackoverflow.com/questions/40938155/right-way-to-deploy-rails-puma-postgres-app-to-elastic-beanstalk)。基本上,您需要通过 ssh 进入您的实例并自定义存储在 /etc/nginx/nginx.conf 中的 nginx 配置文件,您需要在那里将 puma 服务器绑定到 unix 套接字,然后它与 NGINX 连接。NGINX 配置文件示例:

user  root;

error_log  /var/log/app-nginx-error.log;
pid        /var/run/app-nginx.pid;

events {
    worker_connections  8096;
    multi_accept        on;
    use                 epoll;
}

http {
    ....

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

    upstream appserver {
        server unix:///var/run/puma.sock;
    }

    .... 
}

您应该添加到 NGINX 配置文件的重要部分是:

upstream appserver {
  server unix:///var/run/puma.sock;
}

相关内容