404 Not Found - nginx / 1.12.2 - Rails Api应用程序 - 路由错误

时间:2018-03-31 08:44:18

标签: ruby-on-rails api nginx capistrano passenger

我无法访问API。

我正在使用Nginx和Passenger。我正在使用Capistrano进行部署。

我在Nginx网站上的默认文件

##
##
# Default server configuration
#
server {
    listen 80;

    root /var/www/xyz/current/public;

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

    passenger_enabled on;
    rails_env    staging;
    server_name ec2-258-255-77-987.eu-west-2.compute.amazonaws.com;

    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 php7.0-cgi alone:
    # fastcgi_pass 127.0.0.1:9000;
    # # With php7.0-fpm:
    # fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

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

我无法从同一服务器访问API并获得404

请帮帮我

修改

我检查了错误日志,并显示了路由错误。

我的申请是在var / www / xyz / current

我的路线出了什么问题?

1 个答案:

答案 0 :(得分:0)

问题是您的NGINX配置不正确无法处理您应用的流量,您只是按照原来的方式为资产配置了它。您应该添加应用应该监听的上游套接字。首先,在upstream文件中添加指向应用程序套接字的default块:

## Start of file
upstream my_app {
    server unix:/tmp/capistrano.passenger.sock fail_timeout=0;
    ## You can use a HTTP address if you don't want your application listening into sockets.
    # server http://127.0.0.1:8000 fail_timeout=0;
}
# other stuff like: server { ...

然后将资源流和应用程序服务分开以分离其配置,并创建一个server范围try_files,包括您的应用程序上游:

server {
    # other stuff...
    location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }
    try_files $uri $uri/ @my_app;
    location @my_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://my_app;
    }
    # other stuff...
}

不要忘记删除现有的location / {...}阻止。

相关问题