Rails应用程序适用于IP,但不适用于Nginx中的域名

时间:2015-02-17 19:15:11

标签: ruby-on-rails nginx unicorn

在CentOS 7.x中,我安装了一个Rails应用程序,我可以通过[ip]:8080访问它。它使用ip工作。 但是当我将域名设置为server_name(而不是localhost)时,mydomain.com给了我一个502 Bad Gateway问题。我仍然有效。

我已经重启了nginx。应用程序与unicorn_rails一起运行。我的rails日志没有显示。 PD:此应用来自此tutorial

  

/etc/nginx/conf.d/default.conf

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}

server {

    listen 80;
    server_name mydomain.com www.mydomain.com;

    # Application root, as defined previously
    root /var/www/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;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}
  

/var/www/my_app/config/unicorn.rb

# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/var/www/my_app"

# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/var/www/my_app/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/var/www/my_app/log/unicorn.log"
stdout_path "/var/www/my_app/log/unicorn.log"

# Unicorn socket
#listen "/tmp/unicorn.[app name].sock"
listen "/tmp/unicorn.myapp.sock"

# Number of processes
# worker_processes 4
worker_processes 2

# Time-out
timeout 30
  

这是我的/var/log/nginx/error.log显示的内容:

2015/02/17 14:06:34 [crit] 14512#0: *375 connect() to unix:/tmp/unicorn.myapp.sock failed (2: No such file or directory) while connecting to upstream, client: 123.45.678.91, server: mydomain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.myapp.sock:/", host: "mydomain.com"
2015/02/17 14:06:36 [crit] 14512#0: *375 connect() to unix:/tmp/unicorn.myapp.sock failed (2: No such file or directory) while connecting to upstream, client: 123.45.678.91, server: mydomain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.myapp.sock:/", host: "mydomain.com"

第二个问题,应用程序仅在我运行unicorn_rails时执行,如何让它永久运行?

2 个答案:

答案 0 :(得分:1)

查看之前的答案,因为它可能是您问题的解决方案:Nginx cannot find unix socket file with Unicorn (no such file or directory)

tl; dr版本是您可能需要在不同的位置创建您的独角兽套接字。可能是当您从“localhost”变为域名时,您告诉上游应用程序您不再来自本地文件系统;然而,这是猜想,其他人应该插话。

这篇文章指出你不能在/ tmp中放入进程间通信套接字:https://serverfault.com/questions/463993/nginx-unix-domain-socket-error/464025#464025

答案 1 :(得分:0)

似乎需要有关您的环境的其他信息:

1)namei -lm /tmp/unicorn.myapp.sock - 显示路径的所有权限

2)ps aux | grep nginx - 显示nginx所有者和群组

3)ps axu | grep unicorn - 显示独角兽所有者和群组

4)从/etc/nginx/nginx.conf提供user语句的值