How to rewrite the domain part of Set-Cookie in a nginx reverse proxy?
-
I have a simple nginx reverse proxy: server { server_name external.domain.com; location / { proxy_pass http://backend.int/; } } The problem is that Set-Cookie response headers contain ;Domain=backend.int, because the backend does not know it is being reverse proxied. How can I make nginx rewrite the content of the Set-Cookie response headers, replacing ;Domain=backend.int with ;Domain=external.domain.com? Passing the Host header unchanged is not an option in this case. http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypassreversecookiedomain has had this feature for a while, see ProxyPassReverseCookieDomain, but I cannot seem to find a way to do the same in nginx.
-
Answer:
Starting in 1.1.15, proxy_cookie_domain option was added to address this issue. http://wiki.nginx.org/NginxHttpProxyModule#proxy_cookie_domain http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cookie_domain
Tobia at Server Fault Visit the source
Other answers
The above answer works fine with multiple Set-Cookie response headers, but it fails if there's just one. As agentzh points out at the end of the referenced thread, if type(cookies) ~= "table" then cookies = {cookies} end is needed to handle that case. Here's the whole thing: location / { proxy_pass http://backend.int/; header_filter_by_lua ' local cookies = ngx.header.set_cookie if not cookies then return end if type(cookies) ~= "table" then cookies = {cookies} end local newcookies = {} for i, val in ipairs(cookies) do local newval = string.gsub(val, "([dD]omain)=[%w_-\\\\.]+", "%1=external.domain.com") table.insert(newcookies, newval) end ngx.header.set_cookie = newcookies '; }
lhagan
This question came up in the nginx mailing list [1]. There's no way to directly do this in nginx. You have to resort to using the ngx_lua module (>=v0.3.1). The user "agentzh" has an example of what this would look like inlined in the config file: server_name external.domain.com; location / { proxy_pass http://backend.int/; header_filter_by_lua ' local cookies = ngx.header.set_cookie if not cookies then return end local newcookies = {} for i, val in ipairs(cookies) do local newval = string.gsub(val, "([dD]omain)=[%w_-\\\\.]+", "%1=external.domain.com") table.insert(newcookies, newval) end ngx.header.set_cookie = newcookies '; } [1] http://nginx.2469901.n2.nabble.com/Rewriting-the-domain-part-of-Set-Cookie-in-a-proxy-pass-td6453554.html
shamer
Related Q & A:
- How many regions are created by the set of all hyperplanes defined by a set of points?Best solution by Mathematics
- How to rewrite a URL using htaccess?Best solution by Stack Overflow
- How to make reverse proxy and a check page?Best solution by Stack Overflow
- How can I set music as a ringtone on my iPhone?Best solution by Yahoo! Answers
- How to set timing on a 94 honda civic?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.