【问题标题】:404 Not Found - nginx/1.12.2 - Rails Api application - Routing Error404 未找到 - nginx/1.12.2 - Rails Api 应用程序 - 路由错误
【发布时间】:2018-03-31 08:44:17
【问题描述】:

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

我的路线有什么问题?

【问题讨论】:

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


    【解决方案1】:

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

    ## 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 / {...} 块。

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2021-01-08
      • 2020-10-26
      • 1970-01-01
      • 2015-06-15
      • 2017-03-12
      • 1970-01-01
      • 2022-10-01
      • 2022-12-25
      相关资源
      最近更新 更多