【问题标题】:Nginx proxy for Puma: Ignoring my config, not serving from socketPuma 的 Nginx 代理:忽略我的配置,不从套接字提供服务
【发布时间】:2017-04-26 23:03:47
【问题描述】:

我正在尝试关注这篇文章: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04

使用全新的 Amazon Linux EC2 实例。我正在使用开箱即用的 /etc/nginx/nginx.conf 文件,并将我的配置文件添加到 /etc/nginx/sites-default/default

Puma 似乎运行良好:

/home/ec2-user/flviewer/shared/log/puma_error.log: [8006] * 听着 unix:///home/ec2user/flviewer/shared/sockets/tmp/puma.sock

但这显示在 /var/log/nginx/error.log 中:

2016/12/12 05:33:00 [错误] 11018#0: *1 open() “/usr/share/nginx/html/flviewer”失败(2:没有这样的文件或 目录),客户端:173.73.119.219,服务器:localhost,请求:“GET /flviewer HTTP/1.1”,主机:“54.86.222.53”

为什么它应该在查看我打开的套接字时却查看“/usr/share/nginx/html/flviewer”?

这是我的 'nginx -T' 转储的配置:

# configuration file /etc/nginx/sites-available/default:
upstream app {
  # Path to Puma SOCK file, as defined previously
  server unix:/home/ec2-user/flviewer/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost;

  root /home/ec2-user/flviewer/current/public;

  try_files $uri/index.html $uri @app;

  location @app {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    #proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
    #autoindex on;
  }

   location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
   }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

【问题讨论】:

  • /usr/share/nginx/html 是 nginx 的默认目录。尝试运行sudo service nginx startsudo service nginx configtest
  • 是的,我确实重启了 nginx。通过将一些垃圾注入我的站点可用/默认文件并让它捕获错误的语法来测试它是否正确重启。
  • 套接字似乎工作正常。跑这个:# socat - UNIX-CONNECT:/home/ec2-user/flviewer/shared/tmp/sockets/puma.sock GET / RESPONSE >> HTTP/1.1 400 Bad Request

标签: ruby-on-rails nginx puma


【解决方案1】:

没有任何效果。我将 /etc/nginx.conf 剥离为仅此,并且启动并运行。我不得不扔掉 nginx.conf 中的所有样板文件。这有效:

配置文件:

# Run nginx as a normal console program, not as a daemon
daemon off;
user ec2-user;

# Log errors to stdout
error_log /dev/stdout info;

events {} # Boilerplate

http {

  # Print the access log to stdout
  access_log /dev/stdout;

  # Tell nginx that there's an external server called @app living at our socket
  upstream app {
    server unix:/home/ec2-user/flv/shared/tmp/sockets/puma.sock fail_timeout=0;
  }

  server {

    # Accept connections on localhost:2048
    listen 80;
    server_name localhost;

    # Application root
    root /home/ec2-user/flv/shared/public;

    # If a path doesn't exist on disk, forward the request to @app
    try_files $uri/index.html $uri @app;

    # Set some configuration options on requests forwarded to @app
    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app;
    }

   location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;

  }
}

【讨论】:

    【解决方案2】:

    我认为这与使用默认的 nginx 配置文件有关。尝试将/etc/nginx/sites-available/default 移动到/etc/nginx/sites-enabled/flviewer

    $ mv /etc/nginx/sites-available/default /etc/nginx/sites-enabled/flviewer
    

    然后重新加载并重启nginx。

    【讨论】:

    • 刚才试过了,谢谢建议,结果还是一样:2016/12/14 15:57:50 [notice] 19689#0: start worker processes 2016/12/14 15 :57:50 [通知] 19689#0: 启动工作进程 19690 2016/12/14 15:57:56 [错误] 19690#0: *1 open() "/usr/share/nginx/html/flviewer" 失败(2:没有这样的文件或目录),客户端:66.194.23.234,服务器:localhost,请求:“GET /flviewer HTTP/1.1”,主机:“54.86.222.53”
    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 2019-12-23
    • 2014-01-13
    • 2013-11-23
    • 1970-01-01
    • 2019-07-15
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多