我无法让 Passenger 在重新启动 nginx 时启动我的 Nodejs (iojs) 应用程序。我已按照教程操作并安装了先决条件:
$ nginx -V
nginx 版本:nginx/1.8.0
配置参数:--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat ……… --add-module=/tmp/buildd/nginx-1.8.0/debian/modules/passenger/ext/nginx
在http
启用的块中:
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;
passenger_nodejs /usr/local/bin/node;
passenger_default_user staging;
passenger_default_group www-data;
然后在服务器块中:
upstream instance {
# point to node instance
server 127.0.0.1:9000;
}
# for websockets
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 3000;
server_name localhost;
set $app_root /home/staging/public_html;
root $app_root/tmp;
passenger_enabled on;
# point to where the app.js file is
passenger_app_root $app_root;
passenger_sticky_sessions on;
location / {
proxy_pass http://instance;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Forwarded-Proto $scheme;
}
# all static non-scripts are here
location /public/ {
alias $app_root/app/assets/;
}
}
然后重新启动 nginx 我尝试加载页面并在错误日志中看到:
… Starting Passenger watchdog...
… Starting Passenger core...
… Passenger core running in multi-application mode.
… Passenger core online, PID 26585
… Starting Passenger UstRouter...
… Passenger UstRouter online, PID 26590
… [error] 26736#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 188.102.152.94, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:9000/", host: "staging.chatterplot.com:3000"
访问该 URL 会出现 502 错误网关的情况。
我还没有看到任何类似的 nginx 服务器块示例,所以我觉得我做的一些事情非常错误。
答案1
事实证明,乘客无法启动的原因是,它似乎需要location /
阻止代理请求,而不是自己处理请求。以下配置解决了这个问题。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 3000;
server_name localhost;
set $app_root /home/user/public_html/dist;
root $app_root/public;
passenger_enabled on;
passenger_sticky_sessions on;
passenger_app_type node;
passenger_app_root /home/user/public_html/dist;
passenger_app_env production;
}
一开始我并不清楚的另一件事是,passenger/nginx 占用公共目录,并使其中的所有内容都可以访问没有 /public
在网址中。
因此,任何请求都/public/images/logo.png
只是/images/logo.png