You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx代理服务器无法为Rails应用提供静态资源

Fixing Nginx Static Assets Not Serving After Location Block Modification

It looks like the main issues with your current config are twofold: conflicting directives in your root location / block, and likely incorrect ordering or syntax in your static assets location rule. Let's break this down and fix it step by step.

The Core Problem in Your Current Config

Your root location / block has both proxy_pass and try_files, which don't play well together here. When you set proxy_pass first, Nginx is configured to send all requests to your backend on port 3000, but the try_files directive is overriding that by returning a 404 if the static file/directory isn't found—instead of falling back to the proxy. Additionally, if your static assets location rule is placed after this root block, it might not be prioritized correctly because Nginx processes location blocks in a specific order.

Corrected Config Example

Here's a revised version of your /etc/nginx/sites-enabled/default that will properly serve static assets from /var/www/html and proxy all other requests to your backend:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    # Serve static assets directly (highest priority)
    location ^~ /assets/ {
        try_files $uri $uri/ =404;
    }

    location ^~ /fonts/ {
        try_files $uri $uri/ =404;
    }

    # Match SW files (adjust regex as needed for your actual SW naming)
    location ~* \.sw$ {
        try_files $uri =404;
    }

    # Proxy all other requests to your backend
    location / {
        proxy_pass http://127.0.0.1:3000;
        # Optional: Add common proxy headers for proper backend handling
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Key Changes Explained

  • Static Asset Locations: Using the ^~ modifier for prefix locations like /assets/ and /fonts/ tells Nginx to prioritize these over any regex locations, ensuring static files are served directly without hitting the proxy. For SW files, the regex ~* \.sw$ matches any file ending with .sw (case-insensitive) and serves it from the root directory.
  • Root Location Cleanup: We removed the conflicting try_files from the root location / block, so all non-static requests are immediately proxied to your backend on port 3000.
  • Proxy Headers: Added common proxy headers to help your backend understand the original request details (like host and client IP).

Testing and Applying the Fix

  1. First, validate your Nginx config to catch any syntax errors:
    sudo nginx -t
    
  2. If the config is valid, reload Nginx to apply the changes:
    sudo systemctl reload nginx
    

Additional Notes

  • Make sure your static assets are actually located in /var/www/html/assets, /var/www/html/fonts, etc.—match the path in your location blocks to where your files are stored.
  • If your SW files have a different naming pattern (like service-worker.js), adjust the regex to ~* service-worker\.js$ or whatever fits your use case.

内容的提问来源于stack exchange,提问作者user3576036

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:02:14