Nginx在本地将Puma作为Rails应用程序与Puma一起作为OS X的开发环境

我正在尝试在MAC上使用Rails和Puma在本地设置Nginx来练习一些设置。 最终,我将有两个应用程序运行,所以Nginx会负载平衡它们。

对于Rails和Puma我有下一个。

Procfile与:

web: bundle exec puma -C config/puma.rb 

puma.rb与:

 threads 0,16 workers 1 port 3000 environment "production" bind "unix:///path_to_my_app/tmp/sockets/puma.sock" preload_app! 

“工头开始”启动应用程序

 09:28:15 web.1 | started with pid 31442 09:28:16 web.1 | [31442] Puma starting in cluster mode... 09:28:16 web.1 | [31442] * Version 2.13.4 (ruby 2.2.1-p85), codename: A Midsummer Code's Dream 09:28:16 web.1 | [31442] * Min threads: 0, max threads: 16 09:28:16 web.1 | [31442] * Environment: production 09:28:16 web.1 | [31442] * Process workers: 1 09:28:16 web.1 | [31442] * Preloading application 09:28:19 web.1 | [31442] * Listening on tcp://0.0.0.0:3000 09:28:19 web.1 | [31442] * Listening on unix:///path_to_my_app/tmp/sockets/puma.sock 09:28:19 web.1 | [31442] Use Ctrl-C to stop 09:28:19 web.1 | [31442] - Worker 0 (pid: 31444) booted, phase: 0 

该应用程序正在运行,我可以使用127.0.0.1:3000,0.0.0.0:3000或localhost:3000访问它

现在我想用Nginx来描述它。

我用brew安装了Nginx并得到了1.8.0版本

nginx.conf位于/ usr / local / etc / nginx,如下所示。 为了便于阅读,我删除了一些注释行:

 worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8080; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } include servers/*; } 

然后真正应该与rails应用程序连接的configuration是在servers文件夹中。 在那里我有一个名为micro.conf的文件。

 upstream app { server 127.0.0.1:3000; #server unix:///path_to_my_app/tmp/sockets/puma.sock fail_timeout=0; } server { listen 8080 default; root /path_to_my_app/public; try_files $uri/index.html $uri @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; } } 

现在,如果我去localhost:8080不应该最终服务于Rails应用程序? 它只是去欢迎Nginx页面。

如果我去的Rails应用程序的资源之一,如本地主机:8080 /联系人,我可以看到这在日志中:

 2015/10/13 10:10:19 [error] 31614#0: *2 open() "/usr/local/Cellar/nginx/1.8.0/html/contacts" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /contacts HTTP/1.1", host: "localhost:8080" 

这清楚地表明它不是在正确的地方寻找应用程序。

我得到了在上游与套接字相同的结果。 任何想法得到这个非常基本的设置工作?

我发现了这个问题。 问题是,在nginx.conf和micro.conf中定义的服务器共享相同的端口8080.所以它从来没有去预期的服务器,因为另一个捕获请求。 为每个服务器设置不同的端口使Rails应用程序“可见”。 当然…