Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx - Rewrite the request_uri before uwsgi_pass

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?

like image 948
shkschneider Avatar asked Aug 21 '12 11:08

shkschneider


People also ask

When to use rewrite nginx?

NGINX rewrite rules are used to change entire or a part of the URL requested by a client. The main motive for changing an URL is to inform the clients that the resources they are looking for have changed its location apart from controlling the flow of executing pages in NGINX.

How to write rewrite rule in nginx?

Its syntax is simple enough: rewrite regex URL [flag]; But the first argument, regex , means that NGINX Plus and NGINX rewrite the URL only if it matches the specified regular expression (in addition to matching the server or location directive). The additional test means NGINX must do more processing.


2 Answers

location /one {   rewrite /one/(.+) /$1 break;   include uwsgi_params;   uwsgi_pass unix:///.../one.sock; } 
like image 51
CyberDem0n Avatar answered Oct 22 '22 23:10

CyberDem0n


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 
like image 37
maxdebayser Avatar answered Oct 23 '22 01:10

maxdebayser