【问题标题】:Rails application works with ip, but not with domain name in NginxRails 应用程序可以使用 ip,但不能使用 Nginx 中的域名
【发布时间】:2015-04-18 14:39:05
【问题描述】:

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

我已经重启了 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时才会执行,我怎样才能让它永久运行?

【问题讨论】:

  • 你怎么在端口和 sock 文件上都运行 unicorn,检查你的 unicorn 设置并确保它默认使用 sock 文件,否则将你的上游更改为传递到端口 8080
  • 你在运行sudo service nginx restart 后看到了什么,可能有帮助。还要检查文件/tmp/unicorn.myapp.sock,它是否可读和可写。
  • 现在我看到了,该文件不存在。 ls 仅显示那些文件夹:缓存 pids 会话套接字。任何提示为什么没有创建那只袜子?
  • 不,域名我不包括端口。这只是 mydomain.com。

标签: ruby-on-rails nginx unicorn


【解决方案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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多