A little Django troubleshooting 0 comments

We hit an annoying apache problem this month. When attempting to serve a Django view on the root of the site, Django was attempting to match '/index.html' rather than /.

Looking at the uwsgi logs sent us down the wrong path, as the log files looked like:

[pid: 10093|app: 0|req: 1/1] 172.16.0.1 () {42 vars in 797 bytes} [Wed Jul 19 17:25:12 2017] GET / => generated 86199 bytes in 258 msecs (HTTP/1.1 200) 1 headers in 63 bytes (1 switches on core 0)

Leading us to believe that Django was recieving a request for /, and so the problem must be on the Django layer.

Raising an exception in the view with debug mode turned on set us back on the right track. Looking at the Meta information for the request showed us that REQUEST_URI was set to "/", but PATH_INFO was set to "/index.html".

At the time, this left us confused, as we where pretty sure that apache and uwsgi where not responsible. We decided to verify using tshark.

tshark -V -i lo -f "dst port 3031"

-V has tshark print packet details
-i lo says to watch the local loopback device (might be different for you)
-f "dst port 301" says only way traffic going to port 3031 (which we where running uwsgi on).

tshark does a good job of showing you the content of the request. This part:

0000  00 1d 03 00 0e 00 52 45 51 55 45 53 54 5f 4d 45   ......REQUEST_ME
0010  54 48 4f 44 03 00 47 45 54 0c 00 51 55 45 52 59   THOD..GET..QUERY
0020  5f 53 54 52 49 4e 47 00 00 0b 00 53 45 52 56 45   _STRING....SERVE
0030  52 5f 4e 41 4d 45 1b 00 70 6f 72 74 61 6c 2e 76   R_NAME..portal.v
0040  61 67 72 61 6e 74 2d 72 72 69 2d 6c 61 62 73 2e   agrant-rri-labs.
0050  63 6f 6d 0b 00 53 45 52 56 45 52 5f 50 4f 52 54   com..SERVER_PORT
0060  02 00 38 30 0f 00 53 45 52 56 45 52 5f 50 52 4f   ..80..SERVER_PRO
0070  54 4f 43 4f 4c 08 00 48 54 54 50 2f 31 2e 31 0b   TOCOL..HTTP/1.1.
0080  00 52 45 51 55 45 53 54 5f 55 52 49 01 00 2f 0b   .REQUEST_URI../.
0090  00 52 45 4d 4f 54 45 5f 41 44 44 52 0a 00 31 37   .REMOTE_ADDR..17
00a0  32 2e 31 36 2e 30 2e 31 0b 00 52 45 4d 4f 54 45   2.16.0.1..REMOTE
00b0  5f 55 53 45 52 00 00 0d 00 44 4f 43 55 4d 45 4e   _USER....DOCUMEN
00c0  54 5f 52 4f 4f 54 0d 00 2f 76 61 72 2f 77 77 77   T_ROOT../var/www
00d0  2f 68 74 6d 6c 0b 00 53 43 52 49 50 54 5f 4e 41   /html..SCRIPT_NA
00e0  4d 45 00 00 09 00 50 41 54 48 5f 49 4e 46 4f 0b   ME....PATH_INFO.
00f0  00 2f 69 6e 64 65 78 2e 68 74 6d 6c 09 00 48 54   ./index.html..HT
0100  54 50 5f 48 4f 53 54 1b 00 70 6f 72 74 61 6c 2e   TP_HOST..portal.
0110  76 61 67 72 61 6e 74 2d 72 72 69 2d 6c 61 62 73   vagrant-rri-labs
0120  2e 63 6f 6d 0f 00 48 54 54 50 5f 43 4f 4e 4e 45   .com..HTTP_CONNE
0130  43 54 49 4f 4e 0a 00 6b 65 65 70 2d 61 6c 69 76   CTION..keep-aliv
0140  65 12 00 48 54 54 50 5f 43 41 43 48 45 5f 43 4f   e..HTTP_CACHE_CO
0150  4e 54 52 4f 4c 09 00 6d 61 78 2d 61 67 65 3d 30   NTROL..max-age=0
0160  1e 00 48 54 54 50 5f 55 50 47 52 41 44 45 5f 49   ..HTTP_UPGRADE_I
0170  4e 53 45 43 55 52 45 5f 52 45 51 55 45 53 54 53   NSECURE_REQUESTS
0180  01 00 31 0f 00 48 54 54 50 5f 55 53 45 52 5f 41   ..1..HTTP_USER_A
0190  47 45 4e 54 69 00 4d 6f 7a 69 6c 6c 61 2f 35 2e   GENTi.Mozilla/5.
01a0  30 20 28 58 31 31 3b 20 4c 69 6e 75 78 20 78 38   0 (X11; Linux x8
01b0  36 5f 36 34 29 20 41 70 70 6c 65 57 65 62 4b 69   6_64) AppleWebKi
01c0  74 2f 35 33 37 2e 33 36 20 28 4b 48 54 4d 4c 2c   t/537.36 (KHTML,
01d0  20 6c 69 6b 65 20 47 65 63 6b 6f 29 20 43 68 72    like Gecko) Chr
01e0  6f 6d 65 2f 35 39 2e 30 2e 33 30 37 31 2e 31 31   ome/59.0.3071.11
01f0  35 20 53 61 66 61 72 69 2f 35 33 37 2e 33 36 0b   5 Safari/537.36.
0200  00 48 54 54 50 5f 41 43 43 45 50 54 55 00 74 65   .HTTP_ACCEPTU.te
0210  78 74 2f 68 74 6d 6c 2c 61 70 70 6c 69 63 61 74   xt/html,applicat
0220  69 6f 6e 2f 78 68 74 6d 6c 2b 78 6d 6c 2c 61 70   ion/xhtml+xml,ap
0230  70 6c 69 63 61 74 69 6f 6e 2f 78 6d 6c 3b 71 3d   plication/xml;q=
0240  30 2e 39 2c 69 6d 61 67 65 2f 77 65 62 70 2c 69   0.9,image/webp,i
0250  6d 61 67 65 2f 61 70 6e 67 2c 2a 2f 2a 3b 71 3d   mage/apng,*/*;q=
0260  30 2e 38 08 00 48 54 54 50 5f 44 4e 54 01 00 31   0.8..HTTP_DNT..1
0270  14 00 48 54 54 50 5f 41 43 43 45 50 54 5f 45 4e   ..HTTP_ACCEPT_EN
0280  43 4f 44 49 4e 47 0d 00 67 7a 69 70 2c 20 64 65   CODING..gzip, de
0290  66 6c 61 74 65 14 00 48 54 54 50 5f 41 43 43 45   flate..HTTP_ACCE
02a0  50 54 5f 4c 41 4e 47 55 41 47 45 0e 00 65 6e 2d   PT_LANGUAGE..en-
02b0  55 53 2c 65 6e 2b 71 3d 30 2e 38 0b 00 48 54 54   US,en;q=0.8..HTT
02c0  50 5f 43 4f 4f 4b 49 45 57 00 52 52 53 50 53 45   P_COOKIEW.RRSPSE
02d0  53 53 49 44 2d 76 33 69 65 62 62 35 70 6b 6c 71   SSID=v3iecc5pklq
02e0  43 75 6a 32 66 35 64 39 36 77 38 71 59 6a 76 68   suj3f5c97w9p9jvi
02f0  33 33 31 35 6e 3b 20 63 73 72 66 74 6f 6b 65 6e   3305n; csrftoken
0300  3d 75 41 62 78 75 35 74 55 4d 52 6a 38 32 56 6d   =uAbxu5tTMRj82Vm
0310  47 76 65 71 35 6e 6f 50 30 52 35 4a 54 61 4c 42   Gveq5noP0R5JTaLB

contained the important detail:

00e0  4d 45 00 00 09 00 50 41 54 48 5f 49 4e 46 4f 0b   ME....PATH_INFO.
00f0  00 2f 69 6e 64 65 78 2e 68 74 6d 6c 09 00 48 54   ./index.html..HT

Showing that Apache was setting PATH_INFO to ./index.html

The culprit turned out to be Apache's dir module.

We dont need it, so we disabled it, you could also do the following in your apache config (for 2.4+)

<Location />
 DirectoryIndex disabled
 SetHandler uwsgi-handler
 uWSGISocket 127.0.0.1:3031
</Location>

Comments

There are currently no comments

New Comment

required

required (not published)

optional