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

如何在含@proxy_to_app的Nginx配置中集成Prerender.io?

Solution to Integrate Prerender.io with Existing Nginx + Gunicorn Setup

Got it, let's work through integrating Prerender.io into your current Nginx config without breaking the existing @proxy_to_app setup. The core issue is that Prerender's default config tries to add its own named location, but we can merge its logic into your existing flow smoothly.

Step 1: Add Prerender Upstream (in http block)

First, define the Prerender service as an upstream in your http section—this makes it easier to manage if you ever switch between the official Prerender.io service or a self-hosted instance:

http {
    # ... keep all your existing http config here ...

    # Add this upstream block for Prerender
    upstream prerender {
        # Use official Prerender.io service
        server prerender.io:80;
        # OR if you're running a self-hosted Prerender instance:
        # server 127.0.0.1:3000;
        keepalive 64;
    }
}

Step 2: Add Prerender Named Location (in server block)

Next, add a named location to handle forwarding requests to Prerender. This includes setting the necessary headers (don't forget to replace YOUR_PRERENDER_TOKEN if you're using the official service):

server {
    # ... keep all your existing server config here ...

    # Add this named location for Prerender
    location @prerender {
        # Only needed if using official Prerender.io service
        proxy_set_header X-Prerender-Token "YOUR_PRERENDER_TOKEN";
        
        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;

        proxy_pass http://prerender;
    }
}

Step 3: Modify Root Location to Include Prerender Logic

Update your existing location / block to check if a request needs Prerendering before falling back to static files or Gunicorn. This replaces the need for Prerender's default root location logic and plays nicely with your current setup:

location / {
    # Flag to track if we need to use Prerender
    set $prerender 0;

    # Trigger Prerender for crawler user agents
    if ($http_user_agent ~* "baiduspider|googlebot|bingbot|yandex|yahoo|sogou|360spider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
        set $prerender 1;
    }

    # Trigger Prerender for requests with _escaped_fragment_ parameter
    if ($args ~ "_escaped_fragment_") {
        set $prerender 1;
    }

    # Skip Prerender for static assets (no need to render these)
    if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
        set $prerender 0;
    }

    # If Prerender is needed, forward to the @prerender location
    if ($prerender = 1) {
        rewrite .* /$scheme://$host$request_uri? break;
        proxy_pass http://prerender;
    }

    # Fallback to your existing logic: check static files, then proxy to Gunicorn
    try_files $uri @proxy_to_app;
}

Step 4: Clean Up @proxy_to_app Location

Make sure your @proxy_to_app location includes all necessary headers (matching what you have in your /auth/* locations) to ensure Gunicorn receives correct request metadata:

location @proxy_to_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    # Use the upstream we defined earlier instead of hardcoding localhost:8181
    proxy_pass http://app_server;
}

Key Notes

  • Token Replacement: If you're using the official Prerender.io service, replace YOUR_PRERENDER_TOKEN with the token from your Prerender dashboard. Skip this header if you're running a self-hosted Prerender instance.
  • Static Asset Exclusion: The regex skips Prerendering for all common static file types—you can add/remove extensions here based on your app's assets.
  • Crawler UA List: The user agent list covers major search engines and social crawlers; you can extend it if needed for niche crawlers.

This setup keeps your existing static file serving and Gunicorn API forwarding intact while adding Prerendering only when necessary.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:24:52