简体   繁体   English

Nginx + Unicorn无法访问Rails应用程序

[英]Unable to access rails application with Nginx + Unicorn

Yesterday I was able to see default page like you see here . 昨天我可以看到像您在这里看到的默认页面。

But today I modified config for nginx to access my rails application which is running on unicorn and started getting 404. 但是今天,我修改了nginx的配置,以访问在独角兽上运行的Rails应用程序并开始获取404。

/etc/nginx/nginx.conf /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 / 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 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]

I also verified that the security groups attached to EC2 allows 22, 80 and 443 ports. 我还验证了连接到EC2的安全组允许22、80和443端口。 Please find attached security rules for EC2. 请找到随附的EC2安全规则。

I restarted nginx and unicorn many times and verified that nginx and unicorn are running properly. 我多次重启了nginx和unicorn,并验证了nginx和unicorn是否正常运行。

I also verified nginx access and error logs but could not see any activity there. 我还验证了nginx的访问和错误日​​志,但是在那里看不到任何活动。

Please help, 请帮忙,

nginx.conf included config file path is /etc/nginx/sites-enabled/*; nginx.conf包含的配置文件路径是/etc/nginx/sites-enabled/*; but your default config file in /etc/nginx/sites-available/default . 但您的默认配置文件位于/etc/nginx/sites-available/default

Change sites-enabled to sites-available , and try again. sites-enabled sites-available更改sites-enabled sites-available ,然后重试。

Finally I am able to fix it. 最后,我能够修复它。 The issue was AWS was expecting to make nginx SSL aware. 问题是AWS希望使nginx SSL感知。

So to make it work I created self signed certificate and modify nginx configuration. 为了使其正常工作,我创建了自签名证书并修改了Nginx配置。 Below is the final configuration. 下面是最终配置。

/etc/nginx/sites-available/default / 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;
}

This post also useful while we were making SSL configuration. 在我们进行SSL配置时,此帖子也很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM