我正在尝试在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应用程序连接的配置是在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应用程序的其中一个资源,例如localhost:8080 / contacts我可以在日志中看到:
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应用程序“可见”.当然…
原文链接:https://www.f2er.com/nginx/434483.html