'Use Nginx as reverse proxy, mount server at subfolder and add all links with the subfolder

I am running a Webserver with Python on localhost:5000. I would like to use Nginx as a reverse proxy to serve that website on localhost:80/subdirectory. That is because I have other stuff running on the root directory.

So far it works but sadly everything linked in my html document doesn't get changed to the subdirectory. localhost/subdirectory gets me the correct html-file but included links go to localhost/subsubdirectory instead of localhost/subdirectory/subsubdirectory.

According to this I tried to use sub_filter in Nginx. Sadly it doesn't work. What am I doing wrong?

Here is my nginx.conf :

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        include      mime.types;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /subdir {
            proxy_pass http://localhost:5000/;
            proxy_set_header Accept-Encoding "";
            #proxy_set_header X-Real-IP $remote_addr;
            #root   html;
            #index  index.html index.htm;
            sub_filter "http://localhost:5000/" "http://localhost/subdir/";
            sub_filter_types *;
            sub_filter_once off;
            include       mime.types;
        }
    }
}

Edit


Basically the sub_filter method works, but it can't handle links dynamically generated in python. So I need to find a way to tread links like:

<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/style.css') }}" media="screen" />



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source