我无法访问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

我的路线怎么了?

最佳答案

问题是您的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 / {...}块。

关于ruby-on-rails - 找不到404-nginx/1.12.2-Rails Api应用程序-路由错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49585955/

10-11 03:53
查看更多