部署 如何定位 Puma + Nginx 部署问题 404 not found (on Ubuntu 14.04)

caiqinghua · 2015年05月27日 · 最后由 caiqinghua 回复于 2015年05月27日 · 5313 次阅读

参考如下教程,完成后访问显示 404 错误 https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04

一、http://ip:3000/tasks 能正常访问

~$ cd pumarails/
~/pumarails$ RAILS_ENV=production rails server -b 0.0.0.0
=> Booting Puma
=> Rails 4.2.1 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.11.3 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://0.0.0.0:3000

二、nginx 能正常访问 index.html

http://ip/tasks 访问显示 404 错误

404 Not Found

nginx/1.8.0

三、PUMA 配置

deploy@iZ94ra5tl2iZ:~/pumarails$ cat config/puma.rb 
# Change to match your CPU core count
workers 1

# Min and Max threads per worker
threads 1, 6

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"

# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app

on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
deploy@iZ94ra5tl2iZ:~/pumarails$ 

四、nginx 配置

deploy@iZ94ra5tl2iZ:~$ cat /etc/nginx/sites-available/default

upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:/home/deploy/pumarails/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name localhost;

    root /home/deploy/pumarails/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}
deploy@iZ94ra5tl2iZ:~$ 


deploy@iZ94ra5tl2iZ:~/pumarails$ cat /etc/nginx/sites-available/default 
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:///home/deploy/pumarails/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    #root /var/www/html;
    root /home/deploy/pumarails/public;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
deploy@iZ94ra5tl2iZ:~/pumarails$ 

五、如何定位此问题

server {
    listen 80;
    server_name localhost;

    root /home/deploy/appname/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}
  1. 这段配置没加。
  2. 用这篇教程提供的 upstart 脚本启动 puma。

#1 楼 @rei 这段配置在 nginx 配置里面有,puma 已经启动了

#2 楼 @caiqinghua 你没贴出来啊。

#3 楼 @rei 在第四条里面“四、nginx 配置”

#4 楼 @caiqinghua 你搜索一下有没有 location @app 字段。

#5 楼 @rei 谢谢,我太挫了,确实是这个问题,配置不正确 改为和文章一样问题解决了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号