0

I have a django-cms application running behind a nginx server. I am using proxy_pass to send traffic to the cms application. I am using location /django-cms , so when I go to https://nginxserver/django-cms It actually works and send the traffic to the CMS server, however the CMS application is sending back a 302 response and the response contains Location: en/ , so the browser tries to hit https://nginxserver/en/ instead f https://nginxserver/django-cms/en. This obviously results in a 404 error. How can I make sure that everything meant for the CMS server hits https://nginxserver/django-cms/ ?

Here is the relevant section from the nginx.conf file.

location /django-cms {
    auth_request /request_validate;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://10.0.2.29:8000;
}
user1510223
  • 11
  • 2
  • 6
  • The CMS server knows he is /django-cms or he only knows he is nginxserver ? – flaixman Nov 21 '19 at 11:51
  • When the request (proxied by the nginx server) hits the CMS server , the CMS server sees it as a request coming to http://10.0.2.29:800/. It responses with a redirect location of /en which without the nginx server will translate to http://10.0.2.29:8000/en and subsequent urls are like http://10.0.2.29:8000/en/admin etc. – user1510223 Nov 21 '19 at 18:11
  • However when nginx returns the response back to the browser , the browser sends back a request to https://nginxserver/en. So nginx cannot proxy it (as to proxy it needs https://nginxserver/django-cms/en) so it gives a 404 error. I have tried to use proxy-redirect directive to send a location of /django-cms/en but then the subsequent requests (ie to /en/admin etc) still fails. – user1510223 Nov 21 '19 at 18:11

1 Answers1

0
location /django-cms {
  proxy_pass http://10.0.2.29:8000;
  proxy_redirect ~^/(.*) scheme://$http_host/django-cms/$1;
}

The proxy redirect instruction may help you, can you try it? It adds the django-cms to any redirect the backend(cms) gives you.

It's my first time using it, but it looks like that's how it's used in the nginx documentation.

(Found another question that has kind of the same problem as you):

Intercepting backend 301/302 redirects (proxy_pass) and rewriting to another location block possible?

Just in case you also want to check it :D

flaixman
  • 702
  • 6
  • 14