Nginx FastCGI-strip from location prefix?

I am writing a web application in Python using web.py, spawn_fcgi and nginx.

Let's say I have this config block in nginx:

location / {
    include fastcgi.conf;
    fastcgi_pass 127.0.0.1:9001;
}

      

If I then access, say, http://example.com/api/test

the FastCGI application gets the /api/test

request as its location. The web.py framework will use this location when determining which class to execute. For example:

urls = ( "/api/.*", myClass )

      

The problem arises if I need to place this script elsewhere on the website. For example:

location /app {
    include fastcgi.conf;
    fastcgi_pass 127.0.0.1:9001;
}

      

Now when I access the http://example.com/app/api/test

FastCGI application gets it /app/api/test

as its location.

Of course it can be located anywhere: http://example.com/sloppy_admin/My%20Web%20Pages/app/api/test

eg. :-)

I would like the application to be relocatable as installing it on other servers might require it (for example, it has to share the server with something else). It also seems a bit stubborn to insist that every server put it in the same "virtual subdirectory".

Right now, my workaround was to do something like this:

URL_PREFIX = "/app" # the same as the nginx location parameter
urls = ( URL_PREFIX+"/api/.*",myClass )

      

Here are the problems: 1) this means that the script still needs to be edited to the site (not necessarily terrible, but at least inconvenient) and 2) that the variable URL_PREFIX

must be globally available for the entire collection of scripts - because, for example, any class or the function may need to access the location, but will not need to include the prefix.

I am using custom python packages (for example, directories containing init .py scripts ) to make it easier to manage the various scripts that make up the application, but the problem is passing this URL_PREFIX parameter. Example:

app.py:

from myapp import appclass
import web

URL_PREFIX = "/app" # the same as the nginx location parameter
urls = ( URL_PREFIX+"/api/.*",appclass.myClass )
app = web.application(urls,globals())
if __name__ == "__main__":
    web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
    app.run()

      

MyApp / appclass.py:

class myClass:
    def GET(self):
        global URL_PREFIX # this does not work!
        return URL_PREFIX

      

Is there an nginx option to make the path sent to FastCGI relative to location, or a more elegant way to handle this in web.py?

+3


source to share


1 answer


fastcgi.conf

All configuration parameters must be specified in the file . You can also look at the directive fastcgi_split_path_info

.



Nginx FastCGI Documentation .

+2


source







All Articles