Rails: 使用 Nginx Web 服务器找不到 404 页面

Rails: 404 page not found using Nginx Web Server

我正在使用 Nginx Web 服务器和 Puma[= 设置 Rails 6 应用程序33=] 应用程序服务器。

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

我已经设置了 Nginx,这是我的 Rails6 应用程序的 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 page not found

我似乎无法弄清楚错误的来源。

我终于想通了。问题来自 Nginx 配置文件:

我是这样解决的:

修改此指令:

try_files $uri $uri/ @railsserver;

至此

try_files $uri @railsserver;

即从行中删除 $uri/

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

并且由于 Nginx 配置是针对动态 Web 应用程序,因此我们的 Nginx 配置文件中不存在索引文件或不存在索引指令,Nginx returns 错误 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?

就这些了

希望对您有所帮助