Nginx Rails 部署 Nginx 奇怪 404 问题

匿名 · 2012年02月20日 · 最后由 chinacheng 回复于 2015年07月02日 · 8129 次阅读

最近遇到一个 rails app 部署到 nginx 的问题 问题如下: 当使用 ssh 登录到服务器的时候,访问 web 一切正常 但是当从 ssh 退出的时候就会报 404 错误,这个是 nginx 错误 log

2012/02/19 19:49:50 [error] 13959#0: *227 "/home/fin/app/current/public/index.html" is not found (2: No such file or directory), client: 192.168.0.1, server: localhost, request: "GET / HTTP/1.1", host: "server_ip_address", referrer: "http://server_ip_address/users/sign_in"
2012/02/19 19:49:51 [error] 13959#0: *227 "/home/fin/app/current/public/index.html" is not found (2: No such file or directory), client: 192.168.0.1, server: localhost, request: "GET / HTTP/1.1", host: "server_ip_address", referrer: "http://server_ip_address/users/sign_in"
2012/02/19 19:49:52 [error] 13959#0: *227 "/home/fin/app/current/public/index.html" is not found (2: No such file or directory), client: 192.168.0.1, server: localhost, request: "GET / HTTP/1.1", host: "server_ip_address", referrer: "http://server_ip_address/users/sign_in"
2012/02/19 19:49:53 [error] 13959#0: *227 "/home/fin/app/current/public/index.html" is not found (2: No such file or directory), client: 192.168.0.1, server: localhost, request: "GET / HTTP/1.1", host: "server_ip_address", referrer: "http://server_ip_address/users/sign_in"

环境信息

服务部署方式是 nginx + passenger【采用 nginx + unicorn 部署同样会出现这个问题】 rbenv, ruby1.9.2, rails3.1

有朋友遇到过类似的情况么?如需要可以再补出 nginx 配置信息,但是我觉得这个应该不是 nginx 配置的问题,因为当 ssh 登录后访问是正常的,寻 google,stackoverflow 无果

有遇到过类似问题的朋友么?提供一些解决问题的线索? 谢谢

ps 一些其他信息:服务器本身没有独立 ip,是内外网 ip 映射的

/home/fin/app/current/public/index.html 是什么来的……还是贴 nginx 配置出来看看吧~

匿名 #2 2012年02月20日

@iwinux nginx 配置

user  fin;
worker_processes  4;

events {
    worker_connections  1024;
}

http {
   passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11;
      passenger_ruby /usr/local/bin/ruby;

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

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

   server {
      listen 80;
      server_name localhost;
      root /home/fin/app/current/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;
   }
}

我理解的这个错误的意思是 passenger 没起作用,但是不知道为什么

有几个问题:

  1. Ruby 是通过 RVM 安装的么?

  2. 你试试用另外一个用户 SSH 登录进主机,看看是否正常?(排除用户 .bash_profile 和 .bashrc 的影响)

匿名 #4 2012年02月20日

我试着往 public 里面扔了一个 index.html 进去,当 ssh 连接的情况下没问题,会成功访问到这个文件 但是只要我 ssh 一断开,就还是报 404 错误,error log 和上面一摸一样,居然还是说找不到 index.html

这个会和内外网 ip 映射的设置有关系么?

匿名 #5 2012年02月20日

多谢@iwinux 的分析 之前出现这个错误是 rbenv 的方式安装的 ruby 环境,后来换成 rvm 还是这个错误,现在是直接安装的 ruby,rvm 和 rbenv 都已经清理干净了,.bashrc 中也没有任何涉及 ruby 环境的代码了

这个是 bashrc 文件,该注掉的都注掉了

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
#[[ -s "/usr/share/ruby-rvm/scripts/rvm" ]] && source "/usr/share/ruby-rvm/scripts/rvm"  # This loads RVM into a shell session.
export PATH=$PATH:$HOME/bin:/sbin

# for examples
##export PATH="$HOME/.rbenv/bin:$PATH"
#eval "$(rbenv init -)"

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

#4 楼 @29decibel Sorry 刚刚没注意到内外网映射……你的内外网映射是通过什么来设置的?

匿名 #7 2012年02月20日

@iwinux 额,这是个客户项目,我明天联系他们确定一下,你问的“通过什么来设置”具体指的什么?什么软件设置?什么方式?

说实话不太懂 ip 映射的设置

#7 楼 @29decibel

试试配置一个静态目录,看看没有 SSH 连接的情况下能否正常访问,比如这样:

user  fin;
worker_processes  4;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

   server {
      listen 80;
      server_name localhost;
      root /tmp/test;
      index index.html;
   }
}

还有就是,server_name 是真的写了 localhost 么……

匿名 #9 2012年02月20日

@iwinux 照你说得做了(做了个静态文件,passenger 关闭) 果然还是这个现象,ssh 连接的时候 ok,一断开还是 404(server_name 确定是 localhost)

所以能断定是 ip 映射设置的问题? 我百思不得其解的就是这个和 ssh 有什么关系?需要贴 ssh 设置出来么?

#9 楼 @29decibel 这我就不清楚了,所以刚才我才问 IP 映射是怎么设置的……

#9 楼 @29decibel

还有几个问题:

  1. 你是从外网访问这台服务器?

  2. 内网访问的时候是否正常?

  3. nginx server 的 server_name 是 localhost?

匿名 #12 2012年02月20日

多谢@iwinux 帮助分析问题,明天看完 ip 映射设置后再分析,早点休息吧

匿名 #13 2012年02月20日

@iwinux

  • 1.没错是从外网访问
  • 2.内网访问也会有同样的错误
  • 3.确定一定以及肯定 server_name 是 localhost

另外检查一下是不是设置了用户目录加密。这样断开连接时目录内容变掉会导致文件找不到。

匿名 #15 2012年02月20日

@ashchan 貌似是加密了的

.ecryptfs -> /home/.ecryptfs/fin/.ecryptfs

可以直接删除这个来去除加密么? 我 google 了一下 http://askubuntu.com/questions/4950/how-to-stop-using-built-in-home-directory-encryption 还需要 remove 几个包,需要这么繁琐么?

#15 楼 @29decibel

呃,这个问题从 passenger 到 RVM 到 nginx 到 IP 映射最后居然是目录加密的问题 = =!!!!

匿名 #17 2012年02月20日

多谢@ashchan 已经解决,确实就是加密造成的

没想到一个简单的设置会影响这么多问题

之前 SSH authorized_keys 不起作用也是这个造成的(http://superuser.com/questions/61057/ssh-with-authorized-keys-to-an-ubuntu-system-with-encrypted-homedir

匿名 #18 2012年02月20日

@iwinux 呵呵,多谢帮忙跟踪问题,浪费大家时间了 :-)

@Guest 也遇到这个问题,直接删除/home/.ecryptfs/user/.ecryptfs 的这个能解决吗?

#19 楼 @yutian

估计你早就解决了吧

答案

http://askubuntu.com/questions/4950/how-to-stop-using-built-in-home-directory-encryption

Backup the home directory while you are logged in  sudo cp -rp /home/user /home/user.backup
1.1. Check that your home backup has everything!!!
reboot into root via grub
Delete your home directory rm -rf /home/user
Remove the packages apt-get remove ecryptfs-utils libecryptfs0
Restore your home directory mv /home/user.backup /home/user
reboot
Remove any of those .Private .ecryptfs folders rm -rf ~/.Private rm -rf ~/.ecryptfs
Yay!
需要 登录 后方可回复, 如果你还没有账号请 注册新账号