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

Nginx未代理静态文件反而尝试本地打开问题求助

Hey there, let's work through this static resource proxy issue you're facing! It sounds like Nginx is trying to pull static files from its local filesystem instead of forwarding those requests to your proxied site, even after tweaking trailing slashes and $request_uri—super frustrating, I get it.

Core Issue Breakdown

The root problem here is almost always Nginx's location matching priority or incorrect proxy_pass configuration. When Nginx receives a request for a CSS/JS file, it's matching against a local filesystem rule (like a root or alias directive) instead of your proxy rule.

Step-by-Step Fixes

1. Block Local Static Resource Rules from Intercepting Proxied Requests

If you have a global rule for static assets (like below), it has higher priority than prefix-based proxy rules because regex matches take precedence:

# This will override your proxy rule for any CSS/JS files
location ~* \.(css|js|png)$ {
    root /var/www/html;
    expires 30d;
}

Fix this by using ^~ on your proxy location to force it to take priority over regex rules:

# This rule will catch ALL requests starting with /proxied-site/ (including static assets)
location ^~ /proxied-site/ {
    proxy_pass http://your-backend-server/;
    # Add necessary proxy headers
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

# Now this only applies to your main site's static assets, not the proxied one
location ~* \.(css|js|png)$ {
    root /var/www/main-site;
    expires 30d;
}

2. Double-Check Your proxy_pass Trailing Slash Logic

The trailing slash on proxy_pass changes how Nginx constructs the forwarded URL:

  • With trailing slash: Nginx strips the matched location prefix before forwarding.
    Example: If location /proxied-site/ and proxy_pass http://backend/, a request for /proxied-site/css/style.css becomes http://backend/css/style.css.
  • Without trailing slash: Nginx keeps the full request path.
    Example: Same location, but proxy_pass http://backend, the request becomes http://backend/proxied-site/css/style.css.

Make sure this matches how your backend site serves assets. If your proxied site expects assets at its root, use the trailing slash; if assets are under a matching subpath, omit it.

3. Test and Verify

  • Validate your config first:
    nginx -t
    
  • Reload Nginx if no errors:
    nginx -s reload
    
  • Use curl to test a static asset request and check where it's going:
    curl -I http://your-domain/proxied-site/css/style.css
    
    Look for X-Forwarded-For headers or check your backend server logs to confirm the request is reaching it instead of being served locally.

Full Example Config

Here's a complete setup for merging two sites where one is proxied:

server {
    listen 80;
    server_name your-domain.com;

    # Main site root
    root /var/www/main-site;
    index index.html;

    # Proxy rule for the second site (takes priority over static rules)
    location ^~ /second-site/ {
        proxy_pass http://second-site-backend:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # Static assets for the main site only
    location ~* \.(css|js|jpg|png)$ {
        expires 7d;
        add_header Cache-Control "public, immutable";
    }
}

Troubleshooting Tip

Check Nginx's error log (usually at /var/log/nginx/error.log)—it will show exactly which local path Nginx is trying to access for missing assets. This helps confirm whether the request is falling through to a local rule instead of your proxy.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 06:23:22