Django Rest Framework deployed under subdirectory proxy urlpattern missmatch - django

I'm trying to deploy a small API using a subdirectory on the server. Usually if I do the following config it works fine:
location /iframe/api/ {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
client_max_body_size 0;
}
The funny thing is that for static files it seems to work, because I get back proper styling for the rest framework, but for every single urlpattern it also transfers the iframe/api which isn't ideal and basically not a single API route is matched.
I tried to add proxy_redirect off but still no avail. Any idea why isn't this working as expected? How should I deploy a rest framework API under a sub-directory?
Also tried to use FORCE_SCRIPT_NAME='/iframe/api', however I still get the same issue that the URL isn't matched The current path, iframe/api/, didn’t match any of these.

After quite a few hours spend debugging I was able to solve it kinda, but it required multiple ways to do it. If you have better solution feel free to post it!
First you need the trailing / for proxy_pass so the /iframe/api isn't transfered.
location /iframe/api/ {
proxy_pass http://127.0.0.1:8001/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
client_max_body_size 0;
}
This solved that part that the URLs wasn't being matched. For some strange reason I wasn't able to solve the static file sharing directly trough the reverse proxy, so what I ended up was to bind both media and static files to a volume on the main machine and serve them with alias
location /media {
alias /home/user/path_to_media/media;
try_files $uri $uri/ =404;
}
location /static {
alias /home/user/path_to_media/static;
try_files $uri $uri/ =404;
}
You also need to set the FORCE_SCRIPT_NAME in the settings file otherwise the urls generated by the rest framework won't work properly.

Related

Add global prefix for Django URLs

Instead of using subdomains I would like to add subfolders like:
burger.domain.com -> domain.com/organization/burger/
is there any way to add /organization/<name:str>/ globaly?
Well, if you have a reverse proxy server like NGINX/Apache, you can update the X-Script-Name. For example in NGINX:
location /organization {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Script-Name /organization;
}
In that way, django will be served at path /organization(as per configuration of X-Script-Name). You can also checkout this blog for more information regarding deploying django with Apache.
But, if you don't have such reverse proxy server, then you can use FORCE_SCRIPT_NAME. In django settings add the following line:
FORCE_SCRIPT_NAME = '/organization'

How to config nginx proxy pass using subfolder domain whith gunicorn Django

How can I configure ngnix to redirect a proxypass from a domain with subfolder to /?
Example:
https://example.com/yoursub/
to
localhost without /yoursub/ prefix
At the moment the direct access to the server ip http://xxx.xxx.xxx.xx/ from the intranet works without problems.
my nginx config file:
upstream app_server {
server unix:/home/myname/APP-Server/gunicorn/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
# add here the ip address of your server
# or a domain pointing to that ip (like example.com or www.example.com)
server_name 123.456.789.1;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/myname/APP-Server/logs/nginx-access.log;
error_log /home/myname/APP-Server/logs/nginx-error.log;
location /static/ {
alias /home/myname/APP-Server/static-root/;
}
# checks for static file, if not found proxy to app
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
If it's relevant: The backend is a django app with a gunicorn server. Do I have to consider anything about the redirect from https to http? I have no control over the base domain.
If I understand correctly, you want to remove the first part of the URI. There are multiple ways you can do that, but the easiest is probably with the alias directive, which will remove the portion of the URI that matches the current location block:
location /foo/ {
alias /home/myname/APP-Server/static-root/; # It doesn't really matter what you put here, since you're proxying everything.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
If your Nginx server is running on foobar.example and you request http://foobar.example/foo/bar, the upstream server will see a request for http://foobar.example/bar.
The alias directive can be a bit buggy/unintuitive, so it's best to keep your location directive top-level (not nested within other location blocks) and as simple as possible.
If instead you want to add a prefix to the URI, you can do that within the proxy_pass directive itself:
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server/foo$uri$is_args$args;
}
If your Nginx server is running on foobar.example and you request http://foobar.example/bar, the upstream server will see a request for http://foobar.example/foo/bar
Try this:
server {
...
location #proxy_to_app {
...
proxy_pass http://app_server/; # note the trailing slash
}
}
Explanation
As per the nginx docs:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
Since / location matches anything, then everything will be replaced by / (the trailing slash in proxy_pass) before being proxied to the upstream.
The way I handle this is with a rewrite rule:
location /yoursub {
rewrite /yoursub(.*) $1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
The rewrite will convert your url from /yoursub/path/to/view to /path/to/view for your app server.

nginx configuration location regular expression

I'm using an nginx server to proxy a request to another location, but I'm having trouble getting the regex to work. I think part of my issue is that I don't completely understand how the proxy works as I copy/pasted it from somewhere else I saw online.
Here is my nginx-config file:
server {
listen 8080;
location /api/data {
proxy_pass http://jsonserverlocation:3000;
proxy_set_header Host mywebsitewithjson.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
When I make requests like .../api/data/types the server redirects to the resource as expected. However, when I make requests like .../api/data/types/C I get a 404. How do I get the nginx to pass any request that begins with /api/data/ to redirect with my proxy, regardless of how many slashes there are after data?

Rewrite URL for reverse proxy

This is similar to nginx url rewrite for reverse proxy, but I don't know how I should apply that answer to my situation.
I am trying rewrite URLs for a reverse proxy, so that /pathA/something becomes /pathB/something when Nginx makes the request to the upstream server.
location ~ /pathA(/|$) {
proxy_pass http://www.example.com;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
rewrite ^/pathA(.*) /pathB$1;
}
I tried this, but I get an Nginx 404 page (and not the example.com 404 page).
I needed to add redirect (302) or permanent (301):
location ~ /pathA(/|$) {
proxy_pass http://www.example.com;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
rewrite ^/pathA(.*) /pathB$1 permanent;
}

Modifying a Location header with nginx proxy_pass

I have an nginx proxy_pass setup to pass every request on /api through to a backend Tomcat REST service. This service in some cases returns a Location header which varies according to the type of request, e.g., Location: http://foo.bar/baz/api/search/1234567 -- the baz part is due to it being hosted on Tomcat.
My current configuration rewrites the foo.bar host name correctly, but leaves the baz part intact. I'd like to strip this, but the proxy_pass options seem to be limited to clearing or setting a new value for the header.
Is there a way to modify headers dynamically before being passed on to the client, using a regex substitute, for instance? This is my nginx configuration:
location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
proxy_redirect off;
proxy_pass http://foo.bar:8080/baz/api;
}
You may be able to use regexp to modify it but a better way is to use a proxy redirect:
proxy_redirect http://foo.bar/baz/ /;
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
Any Location headers for foo.bar/baz/ will go to /
If you just want to redirect /baz/api, that'll work too.
If any redirects are also adding the port, you'll need to add http://foo.bar:8080/baz/ as well (separate redirect).
Hope this helps!