Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using nginx with Rails' page caching to serve static files. In order to take advantage of static files plus being able to serve dynamic content, I'm using SSI to make nginx pull dynamic "partials" out of Rails and put them in the cached pages.

I want to use memcached to store Rails' authenticity_token, and then make nginx put it in static pages through the use of the session and nginx' memcache module.

Say /ssi/csrf_token returns the token for the current session, and it will also store the value in memcached using the key /ssi/csrf_token?session_id=41ded65....

Rails will send a cookie header like so:

Cookie: _myapp_session=41ded65...;other_cookies...

I need to let the HttpMemcachedModule retrieve the value for key /ssi/csrf_token?session_id=..., so I have to rewrite the URI /ssi/csrf_token to include the session id from the cookie. How would I go about this?

Let me try to explain this with an example so hopefully it's a bit more clear.

Say the static HTML page has the following code:

<meta name="csrf-token" content="<!--# include virtual="/ssi/csrf_token" -->"/>

So I guess the only thing I need is a rewrite rule that will turn /ssi/csrf_token into /ssi/csrf_token?session_id=41ded65..., so the memcached module will try to retrieve the proper key, and if it's not found it will go to Rails to get it.

I hope this isn't too contrived :)

share|improve this question
    
So I just tried rewrite /ssi/csrf_token$ /ssi/csrf_token?session_id=$cookie__my_app_session but it doesn't work (from wiki.nginx.org/HttpCoreModule#.24cookie_COOKIE) –  Ivan Jun 26 '11 at 1:11
    
Scratch that, it's $cookie__MY_APP_SESSION and it works! Now I only need to configure the memcached module. –  Ivan Jun 26 '11 at 1:14
    
Oh, so it gets even simpler, I can just store the token in a cookie and then I can use the echo SSI command (with $cookie_CSRF_TOKEN for example) instead of include. –  Ivan Jun 26 '11 at 1:16
add comment

migrated from stackoverflow.com Jun 26 '11 at 2:54

This question came from our site for professional and enthusiast programmers.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.