Nicholas Johnson
Nicholas Johnson

Reputation: 85

configuring django with nginx on AWS EC2 Instance

I'm currently configuring a django application to run in a prod environment using nginx. I've really been struggling with this and feel as if I've scoured all of stack overflow.

Here is my application's nginx config file

 # the upstream component nginx needs to connect to
upstream django {
    server unix:/tmp/uwsgi.sock;
   # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name 54.172.211.18 172.31.38.120 unclique.io; # substitute your machine's IP address or FQDN
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ec2-user/UnClique/UnClique/static/; # your Django project's static files - amend as required
    }


    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass unix://tmp/uwsgi.sock;
        include     /home/ec2-user/UnClique/config/uwsgi_params; # the uwsgi_params file you installed
    }

}

The landing page appears just fine (it seems). But when I click on links that should take me to other pages on the site, I get 400 Bad Request errors from nginx.

Here is a snippet of what my error.logs look like

108.51.36.126 - - [12/Aug/2018:02:24:16 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:24:16 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:24:18 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:24:20 +0000] "GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:25 +0000] "GET / HTTP/1.1" 200 2787 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
108.51.36.126 - - [12/Aug/2018:02:26:25 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:25 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:26 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:28 +0000] "GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:35 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:27:08 +0000] "GET / HTTP/1.1" 200 2787 "http://54.158.154.23:8000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
108.51.36.126 - - [12/Aug/2018:02:27:08 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:27:08 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:31:02 +0000] "GET / HTTP/1.1" 200 2787 "http://54.158.154.23:8000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
108.51.36.126 - - [12/Aug/2018:02:31:02 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:31:02 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:31:03 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"

It seems as though nginx is having trouble w/ the urls patterns in the namespaces of my application.

Any help would be greatly appreciated.

(Note: I've had the site running with standard python manage.py runserver 0.0.0.0:8000 for a month and it's worked so far.)

Upvotes: 2

Views: 440

Answers (1)

Nicholas Johnson
Nicholas Johnson

Reputation: 85

So I realized that it has to do with the encoding for the URL.

When I click the link, in the browser's address bar, I see

"GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"

If I strip that down to this:

/url'members:member_login'

it works. This is because I took out all of the special characters that got added up when you make a reqest to a URL.

So I don't have the complete answer to my question, because I'm unsure of how to make nginx/django parse these out automatically. But I'm on the right track.

Upvotes: 1

Related Questions