Assuming cookie in which state name is stored is called “state” itself.
You can add this cookie to fastcgi cache key as a parameter.
By default your nginx config will have something like:
Just change it to:
This will create cached version of page for every unique state value. Users from same state will benefit from cache.
If you want get city involved, you can create:
This will create a lot more versions for every page. Nginx can handle them nicely.
Only issue - nginx-helper cannot purge all cached version when a page gets modified. So we need to purge entire cache.
We are using this method on a large where entire site content changes based on marketplace. But we have 8-9 marketplaces (very limited).
In your case, at city-level, cache may get fragmented as it will benefit returning users from same city only.
If you need server side solution strictly, you may use nginx’s geoip module - http://nginx.org/en/docs/http/ngx_http_geoip_module.html and substituion module - http://nginx.org/en/docs/http/ngx_http_sub_module.html
Please let me know which you take and if you need any additional inputs in getting it working.