部署 在搬瓦工 VPS 上个人项目部署后无法正常访问,Rails + Nginx + Passenger

tanqy · 2019年03月24日 · 最后由 tanqy 回复于 2019年03月26日 · 7287 次阅读

问题背景

我属于完全的小白,去年学习了 rails 相关的知识,最后把项目放在的 heroku 上。最近想到自己以前买的有个搬瓦工 VPS,想着能不能部署到上面,在网上搜索了相关的信息发现可以,自己的所有操作基于以下文章:

  1. 如何在 Ubuntu 云服务器上部署自己的 Rails 应用
  2. 在 Aliyun 上快速部署 Ruby on Rails
  3. Ubuntu 12.04 上使用 Nginx Passenger 部署 Ruby on Rails 以及
  4. passenger 的文档

部署了几次,一直不能正常访问。刚开始是按照社区上的方法一直用的 Ubuntu16.04,后面在 passenger 文档上按照 Ubuntu18.04。 所用工具如下:

  • Ubuntu 18.04.2 LTS
  • ruby 2.5.1p57
  • rvm 1.29.7
  • Rails 5.2.2
  • Phusion Passenger 6.0.2
  • nginx/1.14.0 (Ubuntu)

/etc/nginx/nginx.conf内容如下:

user deploy;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

/etc/nginx/sites-enabled/default文件已删除 /etc/nginx/sites-enabled/myapp.conf内容如下

server {
    listen 80;
    server_name 23.105.196.112;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/myapp/code/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /usr/bin/ruby2.5;
}

报错情况

以下为/var/log/nginx/access.log的内容:

[11/Mar/2019:07:34:40 -0400] "GET / HTTP/1.1" 301 0 "-" "curl/7.58.0"

以下为/var/log/nginx/error.log的内容:

[ N 2019-03-11 07:33:39.7092 18730/T1 age/Wat/WatchdogMain.cpp:1366 ]: Starting Passenger watchdog...
[ N 2019-03-11 07:33:39.7484 18733/T1 age/Cor/CoreMain.cpp:1339 ]: Starting Passenger core...
[ N 2019-03-11 07:33:39.7486 18733/T1 age/Cor/CoreMain.cpp:256 ]: Passenger core running in multi-application mode.
[ N 2019-03-11 07:33:39.7577 18733/T1 age/Cor/CoreMain.cpp:1014 ]: Passenger core online, PID 18733
[ N 2019-03-11 07:33:42.4774 18733/Ta age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
[ N 2019-03-11 07:33:44.0159 18759/T1 age/Wat/WatchdogMain.cpp:1366 ]: Starting Passenger watchdog...
[ N 2019-03-11 07:33:44.0536 18762/T1 age/Cor/CoreMain.cpp:1339 ]: Starting Passenger core...
[ N 2019-03-11 07:33:44.0538 18762/T1 age/Cor/CoreMain.cpp:256 ]: Passenger core running in multi-application mode.
[ N 2019-03-11 07:33:44.0625 18762/T1 age/Cor/CoreMain.cpp:1014 ]: Passenger core online, PID 18762
[ N 2019-03-11 07:33:46.7938 18762/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)

curl https://23.105.196.112报错如下: curl: (7) Failed to connect to 23.105.196.112 port 443: Connection refused

找了很久了原因,觉得是 ssl 的问题,尝试直接访问 http 而不是 https: curl http://23.105.196.112,没有反应 wget http://23.105.196.112结果如下:

--2019-03-11 09:24:16--  http://23.105.196.112/
Connecting to 23.105.196.112:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://23.105.196.112/ [following]
--2019-03-11 09:24:16--  https://23.105.196.112/
Connecting to 23.105.196.112:443... failed: Connection refused.

个人分析

我完全按照 passenger 的文档和参照社区的几篇文章来部署,但现在不知道是自己哪里出了问题

按照 passenger 的文档部署了三次,检查了确认和上面步骤一样且没报错。现在我检查实在发现不了问题所在,部署这个网站花了三天时间,我也在搜索了很多关于 nginx 和 passenger 成功启动但不能正常访问的问题。都没有得到解决方法。

我现在认为是 nginx 配置 ssl 的问题,但我看了很多认证 ssl 都需要域名,我只是尝试一下部署在网上,没准备买域名,而且看了前面连接给的方法也可以用 ip 直接访问。当然我分析的可能完全是错的。

希望各位高手能给出一点建议让我继续下去。

1 楼 已删除

应该是 VPS 的 80 端口防火墙没打开

bwg 用户路过,好像 bwg 不需要开 80 端口

http://23.105.196.112/
Connecting to 23.105.196.112:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently

证明 nginx 上 80 端口已经可以正常访问了

至于 ip 配置 443, 我这没听过给 ip 签名的,理论上证书是给域名签名的,而不是给 ip 地址签名,证书的 CA 那块不会过吧

我个人建议新手可以用 nginx + puma 的方式 至少不用编译 nginx ,免得带来各种麻烦

nginx 的配置你可以参考下 capistrano puma (https://github.com/seuros/capistrano-puma)

里面有一个 nginx 的自动配置 cap production puma:nginx_config

要是纯新手,只要使用 ubuntu apt 安装的 nginx, 不需要做其他配置

看到新人学习 rails 真好

是不是 production 环境开了 force_ssl,强制重定向到 https,但是 https 没配置好导致的。IP 地址且没配置 443 端口和证书,不能访问正常。

Rei 回复

谢谢,你说的对的,我把 force_ssl 设置 false 了能通过 http 访问了,非常感谢

jicheng1014 回复

谢谢你的建议,发现确实是 https 的问题,签名问题有域名了再签证书吧,你给的建议配置我就先不看了,之前这个太折腾了😂

huacnlee 回复

谢谢,楼下已解决

需要 登录 后方可回复, 如果你还没有账号请 注册新账号