SERA integration on Nginx

Kindly follow the below examples to setup the routing of bot traffic to SERA.

Prerequisites#

  • This configuration will only work if you have set proxy_ssl_server_name on; in server block
  • All your page requests must be reaching nginx
    • Please check your caching and cache-variation policies for that
  • If caching is enabled, Users and Bots should have a separate cache-variations configured. This is to not serve the SERA’s bot-optimized-content to users.

Steps#

  • Change SERA_ENDPOINT to the SERA endpoint shared with you
  • Change YOUR_TOKEN the SERA token shared with you
  • Change example.com for server_name to your actual domain
  • Change /path/to/your/root to the correct value
  • Keep the relevant bot user-agent regex condition mentioned below - to control the traffic being sent to SERA

worker_processes  auto;

events {
	worker_connections  1024;
}

http {
	include       mime.types;
	default_type  application/octet-stream;

	sendfile             on;
	keepalive_timeout    65;
	map_hash_bucket_size 1024;

	map $http_user_agent $sera_ua_check { #added for sera
		default                     0;
		"~*sera"                    0;
		"~(^|&)_escaped_fragment_=" 0;

		#USE THIS - DURING POC PHASE for bot User-Agent regex check :
		"~*(n7TestUserAgent|google\ page\ speed|chrome-lighthouse|google-inspectiontool|PTST|GTmetrix|n7ua)" 1;
		#USE THIS - ON GOING LIVE ON SERA for bot User-Agent regex check :
		#"~*(n7TestUserAgent|google\ page\ speed|chrome-lighthouse|google-inspectiontool|PTST|GTmetrix|n7ua|googlebot|facebookexternalhit|bingbot)" 1;
		#USE THIS - ON GOING LIVE ON SERA for AI bots for User-Agent regex check :
		#"~*(openai|gptbot|claudebot|claude-user|perplexity-user|perplexity-bot|google-extended|google-cloudvertexbot|google-notebooklm|amazonbot|duckassistbot|metaai|metabotic|meta-externalagent|copilot|baiduspider)" 1;
	}

	map $request_method $sera_method_check { #added for sera
		default  $sera_ua_check;
		POST     0;
		PUT      0;
		DELETE   0;
		OPTIONS  0;
		HEAD     0;
		PATCH    0;
		CONNECT  0;
		TRACE    0;
	}

	map $request_uri $sera_ext_check { #added for sera
		default $sera_method_check;
		"~*\.(ai|ashx|asmx|aspx|avi|avif|bmp|css|csv|dat|dmg|doc|eot|exe|flv|gif|ico|iso|jpeg|jpg|js|json|jsp|less|m4a|m4v|mov|mp3|mp4|mpeg|mpg|ogg|otf|pdf|php|png|ppt|psd|rar|rss|svg|swf|tif|torrent|tf|txt|wav|webm|webp|wmv|woff|woff2|xls|xml|zip)" 0;
	}

	map $request_uri $sera_url_check { #added for sera
		default $sera_ext_check;
		"~*.*(/static/|/_next/|/api/|/login|/logout|/account/|/wishlist/|/cart/|/checkout/|/payment/).*" 0;
	}

	map $args $sera_qry_check { #added for sera
		default $sera_url_check;
		"~*\bnsbp=" 0;
	}

	map $http_x_nv_app $sera_app_check { #added for sera
		default $sera_qry_check;
		"~.+" 0;
	}

	map $http_x_nv_sera_bypass $sera_final_check { #added for sera
		default $sera_app_check;
		"~.+" 0;
	}

	server {
		listen       80;

		server_name  example.com;
		proxy_ssl_server_name on;

		root   /path/to/your/root;

		location / {
			if ($sera_final_check = 1) { #added for sera
				rewrite (.*) /sera_target/$1 last;
			}

			#existing configuration
			#try_files $uri /index.html = 404;
		}

		location /sera_target { #added for sera
			#forward request to sera to serve the optimized page
			resolver 8.8.8.8 1.1.1.1;
			set $sera_host SERA_ENDPOINT;
			proxy_set_header host $sera_host;
			proxy_set_header x-nv-sera-token YOUR_TOKEN;
			proxy_set_header x-nv-access-key YOUR_N7_ACCESS_KEY;
			set $args ""; #REMOVE THIS IF YOUR SITEMAP PAGE URLS HAVE QUERYSTRING
			rewrite /sera_target/(.*) $1 break;
			proxy_pass https://$sera_host;
		}
	}
}

x-nv-app and x-nv-sera-bypass headers mentioned in logic are added on request when SERA connects to your server to fetch page content for optimizing and caching, and also when it bypasses the requests to origin on failover scenario.
These requests are bypassed to your server.

Changes needed at GMC end#

GMC requests should always be served with fresh content, else Google flags them with content-mismatch.
So, make sure all the product URLs in your GMC feed file have nsbp query parameter.
Above condition will bypass SERA for such requests, and route them to your origin.

Testing and Refinement#

  • You can test the Nginx configuration on Fiddle

  • Keep refining the logic by adjusting user-agent list, and SERA exclusion-patterns as needed.

  • Test thoroughly:

    • WPT and GTMetrix will automatically receive the response from SERA due to the routing rule set above.

      • Steps to validate page using WPT
    • To check the SERA rendered page on browser, you can use DevTools > Network conditions facility , or some browser extension (like Simple modify headers ) to manipulate the browser user-agent.
      If in testing phase, use these user-agent values:
      For desktop:

      n7TestUserAgent

      For mobile:

      n7TestUserAgent; Android

      If SERA is live, you can use these actual bot user-agent values:
      For desktop:

      Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +<http://www.google.com/bot.html)> Chrome/W.X.Y.Z Safari/537.36

      For mobile:

      Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +<http://www.google.com/bot.html)>
  • On go-live day, set the user-agent condition regex to actual bot user-agent values. Additionally, ensure that your condition is case-insensitive.

  • After go-live, in any dashboards created for monitoring the “actual user traffic” e.g. Google analytics, exclude the requests with user-agent string containing word “Nitrogen SERA” to get correct understanding about user-traffic.

Notes#

  • Go through the different scenarios mentioned here
  • Also Refer the Frequently Asked Questions about SERA here