# Rewriting Base URL when passing

**URL:** <https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851>\
**Category:** NGINX\
**Created:** [February 15, 2012, 6:33pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851 "2012-02-15T18:33:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 15, 2012, 6:33pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/1 "2012-02-15T18:33:00Z")

</div>

I’m trying to setup Django through UWSGI using Nginx.

I got the UWSGI pass to work using this function

```
            location / {
                    include uwsgi_params;
                    uwsgi_pass 127.0.0.1:9001;
            }

```

Unfortunately when I visit /django/admin. I get an error

Page not found (404) Request Method: GET Request URL:  
[http://69.x.x.x/django/admin](http://69.x.x.x/django/admin) Using the URLconf defined in Django.urls,  
Django tried these URL patterns, in this order: ^admin/

How can I have nginx rewrite the url to not pass the /django part?

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 6:46pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/2 "2012-02-15T18:46:01Z")

</div>

On 15 Fev 2012 17h32 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Page not found (404) Request Method: GET Request URL:  
> [http://69.x.x.x/django/admin](http://69.x.x.x/django/admin) Using the URLconf defined in  
> Django.urls, Django tried these URL patterns, in this order: ^admin/
> 
> How can I have nginx rewrite the url to not pass the /django part?

Mind you, my knowledge of django is within the neighborhood of  
0. Probably the easiest way is just to “rewrite” the uwsgi parameters  
so that the django part is not passed.

I have yet to try uWSGI ☹

— appa

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 7:08pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/3 "2012-02-15T19:08:57Z")

</div>

On Wednesday 15 February 2012 21:32:30 adam estes wrote:

> Page not found (404) Request Method: GET Request URL:  
> [http://69.x.x.x/django/admin](http://69.x.x.x/django/admin) Using the URLconf defined in Django.urls,  
> Django tried these URL patterns, in this order: ^admin/
> 
> How can I have nginx rewrite the url to not pass the /django part?

You can use “rewrite” directive:

[Module ngx\_http\_rewrite\_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)

Or regexp “location” with captures and correspond uwsgi\_param setting.

[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)

i.e.:

location /django/ {

```
 location ~ ^/django(?P<adm_path>/admin/.+)$ {
     uwsgi_param PATH_INFO $adm_path;
     ...
 }

```

}

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 7:54pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/4 "2012-02-15T19:54:23Z")

</div>

The problem is that its not going to be just /admin

There are around 10 urls defined.

I tried

location ~ ^/django(?P\<django\_path\>.\*?)$ {  
uwsgi\_param PATH\_INFO $django\_path;  
uwsgi\_pass 127.0.0.1:9001;  
include uwsgi\_params;  
}

Which according to RegSkinner, would match anything after /django

But this did not work. Its still passing the django parth. What do I do?

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 9:06pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/5 "2012-02-15T21:06:30Z")

</div>

I removed that from both the uwsgi\_params and uwsgi\_params.default.  
Still  
isn’t working.

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 9:16pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/6 "2012-02-15T21:16:27Z")

</div>

It seems to actually be rewriting the url now. The issue is with how its  
doing it

when I visit 69.x.x.x./django/admin/

it rewrites it to 69.x.x.x/admin/

which is then processed by nginx again I’m guessing because it loads the  
IPB admin folder, and not the django admin url like it should if it was  
being handled by django.

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 9:27pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/7 "2012-02-15T21:27:36Z")

</div>

On Thursday 16 February 2012 00:06:06 adam estes wrote:

> I removed that from both the uwsgi\_params and uwsgi\_params.default. Still  
> isn’t working.

Did you reload your nginx? Are you sure that it’s not your browser  
cache?

Could you provide debug log?

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 9:36pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/8 "2012-02-15T21:36:17Z")

</div>

On Thursday 16 February 2012 00:15:51 adam estes wrote:

> 

If you didn’t set any “rewrite” then nginx doesn’t rewrite url and it  
doesn’t  
process it again. The “location” and “uwsgi\_param” directives can’t do  
that.

Could you show your full config?

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 8:49pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/9 "2012-02-15T20:49:41Z")

</div>

On Wednesday 15 February 2012 22:53:56 adam estes wrote:

> ```
> }
> 
> ```
> 
> Which according to RegSkinner, would match anything after /django
> 
> But this did not work. Its still passing the django parth. What do I do?

Have you looked at your “uwsgi\_params” file which you include in the  
location?  
So I expect that it sets one more PATH\_INFO param to different value.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 9:41pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/10 "2012-02-15T21:41:53Z")

</div>

worker\_processes 1;  
user nginx nginx;

events {  
worker\_connections 1024;  
}

http {  
include mime.types;  
default\_type application/octet-stream;  
sendfile on;  
keepalive\_timeout 65;

```
    # I have to set min length to 0 and http version to 1.0 or it 

```

won’t  
compress  
# the XML-RPC (SCGI) responses. Those responses can be quite  
large  
if you’re  
# using many torrent files.  
gzip on;  
gzip\_min\_length 0;  
gzip\_http\_version 1.0;  
gzip\_types text/plain text/xml application/xml application/json  
text/css application/x-javascript text/javascript$

```
    server {
            listen 80;
            #error_log /var/log/nginx/error.log error;
            server_name localhost;

            location ~ /\.ht {
                    deny all;
            }

            location ~ /\.svn {
                    deny all;
            }

            location / {
                    root /home/sites/forum/;
                    index index.php index.html index.htm;
            }

```

location ~ .php$ {  
root “/home/sites/forum/”;  
fastcgi\_pass unix:/etc/phpcgi/php-cgi.socket;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
}  
location ~ ^/django(?P\<django\_path\>.\*?)$ {  
uwsgi\_param PATH\_INFO $django\_path;  
uwsgi\_pass 127.0.0.1:9001;  
include uwsgi\_params;  
}

# location ~ ^/RPC00001$ {

# include scgi\_params;

# scgi\_pass

unix:/home/rtorrent/rtorrent/session/rpc.socket;

# auth\_basic “idk”;

# auth\_basic\_user\_file

“/usr/local/nginx/rutorrent\_passwd\_rtorrent”;

# }

```
    }
    server {
            listen 443;
            server_name localhost;
            auth_basic "My ruTorrent web site";
            auth_basic_user_file 

```

“/usr/local/nginx/rutorrent\_passwd”;

```
            ssl on;
            ssl_certificate /usr/local/nginx/rutorrent.pem;
            ssl_certificate_key /usr/local/nginx/rutorrent.pem;

            location ~ ^/rutorrent/(?:share|conf) {
                    deny all;
            }

            location ~ /\.ht {
                    deny all;

```

}

```
            location / {
                    root /var/rutorrent;
                    index index.php index.html index.htm;
            }

            location ~ \.php$ {
                    root "/var/rutorrent";
                    fastcgi_pass unix:/etc/phpcgi/php-cgi.socket;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
}

# location ~ ^/RPC00001$ {

# include scgi\_params;

# scgi\_pass

unix:/home/rtorrent/rtorrent/session/rpc.socket;

# auth\_basic “My ruTorrent web site”;

# auth\_basic\_user\_file

“/usr/local/nginx/rutorrent\_passwd\_rtorrent”;

# }

```
    }

```

}

And I don’t think it was compiled with debugging support 😑

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 9:43pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/11 "2012-02-15T21:43:07Z")

</div>

If I visit 69.x.x.x/django

The error page has changed to

Page not found(404)

Request Method:GET  
Request URL:[http://69.x.x.x/](http://69.x.x.x/)

When it used to say the request url was 69.x.x.x/django

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 10:24pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/12 "2012-02-15T22:24:02Z")

</div>

Whenever I type /django/admin

The url changes to /admin and the ipb admin login is displayed  
(Because admin is in the root folder, not in the django folder.

if I type /django/admin/

The django admin page is displayed, but clicking login changes it to  
/admin and the ipb login page is displayed.

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 10:19pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/13 "2012-02-15T22:19:05Z")

</div>

On Thursday 16 February 2012 00:42:39 adam estes wrote:

> If I visit 69.x.x.x/django
> 
> The error page has changed to
> 
> Page not found (404)
> 
> Request Method:GET  
> Request URL:[http://69.x.x.x/](http://69.x.x.x/)

It’s what your django says. And “Request URL” seems to be correct.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 10:39pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/14 "2012-02-15T22:39:33Z")

</div>

On Thursday 16 February 2012 01:23:48 adam estes wrote:

> Whenever I type /django/admin
> 
> The url changes to /admin and the ipb admin login is displayed  
> (Because admin is in the root folder, not in the django folder.
> 
> if I type /django/admin/
> 
> The django admin page is displayed, but clicking login changes it to  
> /admin and the ipb login page is displayed.

Hm… I see. But what do you expect? Now, your django doesn’t know about  
“/django/” prefix in your url and naturally generates all links without  
it.

When you click to “/admin” - your browser goes to “/admin” and nginx  
receives a  
request to “/admin” (not /django/admin).

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 10:41pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/15 "2012-02-15T22:41:26Z")

</div>

Hmm. then that wouldn’t resolve my problem.

How can I host my django installation on another port?

I tried creating a new server object and just added in listen 8000 and  
the location thing, and it didn’t respond to anything I sent on that  
port, despite linux saying nginx was bound to that port.

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 11:57pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/16 "2012-02-15T23:57:44Z")

</div>

On Thursday 16 February 2012 02:13:26 Valentin V. Bartenev wrote:

> On Thursday 16 February 2012 01:40:56 adam estes wrote:
> 
> > Hmm. then that wouldn’t resolve my problem.
> 
> You can try to set django.root to “/django/” (it’s “/” by default) in your  
> django settings or uwsgi deploy .py script, so probably you don’t need any  
> magic with urls.

Or, probably:

uwsgi\_param PATH\_INFO $document\_uri;  
uwsgi\_param SCRIPT\_NAME /django;  
…

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![adam\_estes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@adam\_estes](https://www.ruby-forum.com/u/adam_estes)\
**Post date:** [February 15, 2012, 11:14pm UTC](https://www.ruby-forum.com/t/rewriting-base-url-when-passing/216851/17 "2012-02-15T23:14:20Z")

</div>

On Thursday 16 February 2012 01:40:56 adam estes wrote:

> Hmm. then that wouldn’t resolve my problem.

You can try to set django.root to “/django/” (it’s “/” by default) in  
your  
django settings or uwsgi deploy .py script, so probably you don’t need  
any  
magic with urls.

> How can I host my django installation on another port?
> 
> I tried creating a new server object and just added in listen 8000 and  
> the location thing, and it didn’t respond to anything I sent on that  
> port, despite linux saying nginx was bound to that port.

It’s strange. Firewall? Bound to wrong interface?

wbr, Valentin V. Bartenev
