django - Rewrite `example.com/foo/bar` to `foo.example.com/bar` with nginx. Redirect loop -


i have django app serves user content in iframes. avoid session hijacking want serve different subdomain.

i have site hosted @ subdomain.example.com particular iframe content @ subdomain.example.com/foo/bar/id. i'm trying set nginx make specific content accessible through foo.example.com/bar/id instead.

my nginx setup:

server {     listen 80;     server_name subdomain.example.com;      location / {         include proxy_params;         #served gunicorn:         proxy_pass http://unix:/home/example/example.sock;     } }  server {     listen 80;     server_name foo.example.com;      location / {         include proxy_params;          #this key:         rewrite ^/(.*?) /foo$request_uri last;          #served gunicorn:         proxy_pass http://unix:/home/example/example.sock;     } } 

this gives me following error in nginx error.log:

2016/01/24 14:10:41 [error] 24286#0: *17 rewrite or internal redirection cycle while processing "/foo/bar/66", client: xx.xx.xx.xx, server: foo.example.com, request: "get /bar/66 http/1.1", host: "foo.example.com"

which makes sense; redirect being redirect again , again because matches rewrite regex.

i tried adding

proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header host $http_host; proxy_redirect off; 

to no avail.

without redirect host correctly, i'd avoid having url foo.example.com/foo/bar/id.

is there different way approach better? or way stop redirects?

ps: bonus question? next blocking subdomain.example.com/foo/bar , forcing access through foo.example.com/bar

edit: solved question dropping rewrite , take care of subdomains in django using django-subdomains (https://django-subdomains.readthedocs.org)

this might solution bonus question (i.e. how block /foo/bar www.example.com): https://stackoverflow.com/a/4677893/2319697

basically return 404 if pattern/location matched


Comments