25

I have a Nginx vhost than is configured as such:

...
location /one {
  include uwsgi_params;
  uwsgi_pass unix:///.../one.sock;
}
location /two {
  include uwsgi_params;
  uwsgi_pass unix:///.../two.sock
}
...

This is a simplified configuration of course

When I request /one/something I would like my Python script to receive /something as request_uri.

I'm using BottlePy but would like this to be handled by Nginx and not in my Python code.

Can I do something like uwsgi_param REQUEST_URI replace($request_uri, '^/one', '')?

Edit

Here is the request from my Python code: [pid: 30052|app: 0|req: 1/1] () {42 vars in 844 bytes} [Tue Aug 21 14:22:07 2012] GET /one/something => generated 0 bytes in 4 msecs (HTTP/1.1 200) 2 headers in 85 bytes (0 switches on core 0)

So Python is OK but uWSGI is not.

How to fix that?

shkschneider
  • 16,338
  • 12
  • 55
  • 107

4 Answers4

39
location /one {
  rewrite /one/(.+) /$1 break;
  include uwsgi_params;
  uwsgi_pass unix:///.../one.sock;
}
CyberDem0n
  • 13,057
  • 1
  • 29
  • 21
  • if one uses this rule: `rewrite /one/?(.*)$ /$1 break;`, the route `https:///one` (without trailing `/`) will also work. – netzego Apr 23 '20 at 19:34
  • @netzego - Eh, neither `/one` nor `/one/` are valid URLs though, so why permit it? They want to end up receiving the `/something` that comes afterwards. IDK, just thinking about my own application right now. – ArtOfWarfare Oct 13 '20 at 23:21
3

I know this thread is old, but there is another way to solve this if you are using uWSGI to run your python app.

[uwsgi]
route-uri = ^/one/(.*) rewrite:/$1
maxdebayser
  • 1,066
  • 7
  • 10
1

I just met the same problem, and here is a solution

location /one {
    include uwsgi_params;
    uwsgi_pass unix:///.../one.sock;
    uwsgi_param SCRIPT_NAME /one;
    uwsgi_modifier1 30;
}

You can found more about uwsgi_modifier1 here: http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html#hosting-multiple-apps-in-the-same-process-aka-managing-script-name-and-path-info

Daoctor
  • 324
  • 3
  • 7
  • 4
    The documentation that's linked in your answer now explicitly says *not* to do what you've shown (and it says "ancient uWSGI versions used to support " it, suggesting it's no longer supported). – Scott Stevens Aug 18 '16 at 12:21
0

I've solved that in another way:

[uwsgi]
module = wsgi:application
master = true
processes = 10
socket = 127.0.0.1:9090
mount = /one=customscript.py
manage-script-name = true

nginx

location /one {
  include uwsgi_params;
  uwsgi_pass 127.0.0.1:9090;
}