【问题标题】:Rails: 404 page not found using Nginx Web ServerRails:使用 Nginx Web 服务器找不到 404 页面
【发布时间】:2021-02-12 14:56:54
【问题描述】:

我正在使用 Nginx Web 服务器和 Puma Application Server 设置 Rails 6 应用程序。

我希望 Nginx 网络服务器提供静态网络文件,而 Puma 应用程序服务器将处理动态请求。

我已经设置了 Nginx,这是我的 Rails 6 应用程序的 Nginx 配置文件:

upstream railsserver {
        server 127.0.0.1:3000;
}

server {

        # Replace 'localhost' with your FQDN if you want to use
        # your app from remote or if you need to add a certificate 
        server_name localhost;

                root /home/deploy/my-website/public;

        # Define where Nginx should write its logs
        access_log /var/log/nginx/my-website/access.log;
        error_log /var/log/nginx/my-website/error.log;

        location / {
                try_files $uri $uri/ @railsserver;
        }
       
        location ~ ^/(assets/|robots.txt|humans.txt|favicon.ico) {
                expires max;
        }

        location @railsserver {
                proxy_set_header Host $http_host;
                proxy_set_header CLIENT_IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_read_timeout 300;
                proxy_pass http://railsserver;

                gzip on;
                gzip_types text/plain text/xml text/css image/svg+xml application/javas$
                gzip_proxied any;
        }
}

我也用命令重启了Nginx:

sudo systemctl restart nginx

但每次我尝试通过浏览器访问该网站时,都会出现错误:

404 页面未找到

我似乎无法弄清楚错误来自哪里。

【问题讨论】:

    标签: ruby-on-rails nginx puma


    【解决方案1】:

    我终于明白了。问题来自 Nginx 配置文件:

    我是这样解决的

    修改此指令:

    try_files $uri $uri/ @railsserver;
    

    到这里

    try_files $uri @railsserver;
    

    即从行中删除$uri/

    由于我们不会直接提供任何索引文件 (index.html),因此不需要它存在。它会导致 nginx 尝试加载 index 指令中指定的文件,或者如果不存在,则执行目录索引。

    由于 Nginx 配置是针对动态 Web 应用程序,我们的 Nginx 配置文件中不存在索引文件或不存在索引指令,因此 Nginx 返回错误404 page not found

    所以我们的配置将如下所示:

    upstream railsserver {
            server 127.0.0.1:3000;
    }
    
    server {
    
            # Replace 'localhost' with your FQDN if you want to use
            # your app from remote or if you need to add a certificate 
            server_name localhost;
    
                    root /home/deploy/my-website/public;
    
            # Define where Nginx should write its logs
            access_log /var/log/nginx/my-website/access.log;
            error_log /var/log/nginx/my-website/error.log;
    
            location / {
                     # First attempt to serve file(s) specified in the index directive,
                     # then the rails application directory
                     try_files $uri @railsserver;
            }
           
            location ~ ^/(assets/|robots.txt|humans.txt|favicon.ico) {
                    expires max;
            }
    
            location @railsserver {
                    proxy_set_header Host $http_host;
                    proxy_set_header CLIENT_IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_read_timeout 300;
                    proxy_pass http://railsserver;
    
                    gzip on;
                    gzip_types text/plain text/xml text/css image/svg+xml application/javas$
                    gzip_proxied any;
            }
    }
    

    接下来,使用命令重启 Nginx:

    sudo systemctl restart nginx
    

    您现在可以通过浏览器查看您的网站,现在应该一切正常。

    资源Does Nginx require an index file for the root directory?

    就是这样

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多