[Flask] URL rewriting (url_for) issues with Flask 0.11.1 and fastcgi

Pete Forman petef4+usenet at gmail.com
Mon Oct 17 15:55:59 EDT 2016

Erik Lindqvist <eriksmejl at gmail.com>

> So I found a workaround, credit to http://stackoverflow.com/a/38662338
> TL;DR SCRIPT_NAME has to be set to "" in the environment before
> passing the app to WSGI.
> So the issue seems to be that "SCRIPT_NAME" in the environment is
> wrong somehow.
> Does annyone know where I should find an issue?
> mod_fastcgi?
> Werkzeug?
> Flup?
> Flask?
> Also, I had to change the alias.url path to make sure the local path
> has a trailing "/". Static files can't be accessed otherwise, will
> submit a PR for the documentation.

This Python code may help your diagnosis. I have used it to print
command lines and environment up the calling tree on Linux.

#!/usr/bin/env python
from __future__ import print_function
import os

def print_pids_etc():
    first = True
    for pid in get_ppids(os.getpid()):
        cmdline = get_cmdline(pid)
        env = get_env(pid)
        if first:
            first = False
        print('PID: {0}'.format(pid))
        print('COMMAND LINE:')
        print('\n'.join(cmdline) if cmdline else '(cmdline not readable)')
        print('\n'.join(env) if env else '(environ not readable)')

def get_ppid(pid):
    """Return the ppid for a pid.
    with open('/proc/{0}/stat'.format(pid)) as f:
        line = f.read()
    return int(line.split()[3])

def get_ppids(pid):
    """Return pid and all its ancestors.
    pids = [pid]
    failsafe = 100
    while int(pid) > 1 and failsafe > 0:
        failsafe -= 1
        pid = get_ppid(pid)
    return pids

def get_cmdline(pid):
    """Return the command line for a process ID.

    Returns a list of command line argument (including 0th). If cmdline is not
    readable then return None.
        with open('/proc/{0}/cmdline'.format(pid)) as f:
            e = f.read()
        return e.rstrip('\0').split('\0')
    except IOError:
        return None

def get_env(pid):
    """Return the environment for a process ID.

    Returns a sorted list of strings 'NAME=VALUE'. If environ is not readable
    then return None.
        with open('/proc/{0}/environ'.format(pid)) as f:
            e = f.read()
        return sorted(e.rstrip('\0').split('\0'))
    except IOError:
        return None

if __name__ == '__main__':

Pete Forman

More information about the Flask mailing list