【问题标题】:Unable to access rails application with Nginx + Unicorn无法使用 Nginx + Unicorn 访问 rails 应用程序
【发布时间】:2015-05-22 04:59:48
【问题描述】:

昨天我可以看到默认页面,就像你看到的here

但今天我修改了 nginx 的配置以访问我在 unicorn 上运行的 rails 应用程序并开始得到 404。

/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

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;

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

  ##
  # Logging Settings
  ##

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

  ##
  # Gzip Settings
  ##

  gzip on;
  gzip_disable "msie6";

  ##
  # Virtual Host Configs
  ##

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

/etc/nginx/sites-available/default

upstream unicorn {
  server unix:/tmp/unicorn.integrity_matters.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost;
  root /home/ubuntu/integrity_matters/current/public;

  location ~ ^/assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 20M;
  keepalive_timeout 10;
}

MY_APP_ROOT/config/unicorn.rb

root = "/home/imdeploy/integrity_matters/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.integrity_matters.sock"
worker_processes 2
timeout 30

# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
  ENV\["BUNDLE_GEMFILE"\] = File.join(root, 'Gemfile')
end][2]

我还验证了附加到 EC2 的安全组允许 22、80 和 443 端口。请查找attached EC2 的安全规则。

我多次重启nginx和unicorn,验证nginx和unicorn运行正常。

我还验证了 nginx 访问和错误日​​志,但在那里看不到任何活动。

请帮忙,

【问题讨论】:

    标签: ruby-on-rails nginx amazon-ec2 unicorn


    【解决方案1】:

    nginx.conf 包含的配置文件路径是/etc/nginx/sites-enabled/*;,但您的默认配置文件在/etc/nginx/sites-available/default

    sites-enabled 更改为sites-available,然后重试。

    【讨论】:

      【解决方案2】:

      我终于可以修复它了。问题是 AWS 希望让 nginx SSL 感知。

      为了让它工作,我创建了自签名证书并修改了 nginx 配置。下面是最终配置。

      /etc/nginx/sites-available/default

      upstream integrity_matters_server {
        server unix:/tmp/unicorn.integrity_matters.sock fail_timeout=0;
      }
      
      server {
        listen   80;
        server_name ec2-52-10-245-227.us-west-2.compute.amazonaws.com;
        rewrite ^/(.*) https://ec2-52-10-245-227.us-west-2.compute.amazonaws.com permanent;
      }
      
      server {
        listen   443;
        server_name ec2-52-10-245-227.us-west-2.compute.amazonaws.com;
        root /home/ubuntu/integrity_matters/current/public;
      
        ssl on;
        ssl_certificate      /etc/nginx/ssl/nginx_im.crt;
        ssl_certificate_key  /etc/nginx/ssl/nginx_im.key;
        ssl_protocols        SSLv3 TLSv1;
        ssl_prefer_server_ciphers on;
      
        location ~ ^/assets/ {
          gzip_static on;
          expires max;
          add_header Cache-Control public;
        }
      
        try_files $uri @integrity_matters;
        location @integrity_matters {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto https;
          proxy_set_header Host $host;
          proxy_redirect off;
          proxy_pass http://integrity_matters_server;
        }
      
        error_page 500 502 503 504 /500.html;
        client_max_body_size 20M;
        keepalive_timeout 10;
      }
      

      这个post 在我们进行 SSL 配置时也很有用。

      【讨论】:

        猜你喜欢
        • 2014-12-27
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        • 2013-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多