【问题标题】:nginx serve request to a json api service via sockets configurationnginx 通过套接字配置向 json api 服务提供请求
【发布时间】:2015-01-08 07:40:39
【问题描述】:

我的葡萄应用程序在 localhost 上运行,如下所示: bundle exec rackup -p 9292

之后去http://localhost:9292/api/v1/ping

你会收到一个 json 响应 {"res":"pong"}

现在,我正在尝试在生产中进行设置。所以我决定在 puma 和 nginx 配置上运行它。 安装 puma 并在套接字上运行它。

⇒  bundle exec puma -b unix:///tmp/my_app.sock
Puma starting in single mode...
* Version 2.10.2 (ruby 2.1.2-p95), codename: Robots on Comets
* Min threads: 0, max threads: 16
* Environment: development
env :: development
Service started, go to town.
* Listening on unix:///tmp/my_app.sock
Use Ctrl-C to stop

使用如下配置设置 nginx:

events {
    worker_connections  1024;
}


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


    #access_log  logs/access.log  main;

    sendfile        on;

    upstream my_app {
        server unix:///tmp/my_app.sock;

   }
    server {
        listen       8989;
        server_name  my_app;


    root /Volumes/dev/my_app;

    access_log  /Volumes/dev/my_app/service/log/host.access.log;
    error_log  /Volumes/dev/my_app/service/log/error.access.log;

    location / {
       root              /Volumes/dev/my_app/;
       gzip_static       on;
       expires           max;
       add_header        Cache-Control public;
   }

}

我希望它也能起到同样的作用。我浏览到 http://localhost:8989/api/v1/ping 但是从 nginx 得到了404 Not Found

错误日志显示:

2015/01/07 21:51:15 [error] 62300#0: *2 open() "/Volumes/dev/my_app/api/v1/ping" failed (2: No such file or directory), client: 127.0.0.1, server: my_app, request: "GET /api/v1/ping HTTP/1.1", host: "localhost:8989"

也许它认为它是服务静态内容?

【问题讨论】:

    标签: ruby json sockets nginx puma


    【解决方案1】:

    在您的代码中,nginx 首先提供root 的静态内容,而不是upstream my_app。使用root 仅提供静态文件,使用upstream 将请求路由到应用服务器。这种简化的 nginx 配置可能会对您有所帮助:

    upstream my_app {
      server unix:/tmp/my_app.sock;
    }
    
    server {
      listen 80;
      ...
    
      location / {
        proxy_pass http://my_app;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 2016-11-01
      • 2017-09-21
      • 2015-02-16
      • 1970-01-01
      • 2013-11-23
      • 2012-10-27
      相关资源
      最近更新 更多