Rewriting Base URL when passing

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 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?

On 15 Fev 2012 17h32 WET, [email protected] wrote:

Page not found (404) Request Method: GET Request URL:
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 :frowning:

— appa

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 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

Or regexp “location” with captures and correspond uwsgi_param setting.

Module ngx_http_core_module

i.e.:

location /django/ {

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

}

wbr, Valentin V. Bartenev

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?

I removed that from both the uwsgi_params and uwsgi_params.default.
Still
isn’t working.

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.

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?
http://nginx.org/en/docs/debugging_log.html

wbr, Valentin V. Bartenev

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

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

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 :expressionless:

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/

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

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.

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/

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

wbr, Valentin V. Bartenev

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

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.

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

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