2

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

3
  • I think this actually has nothing to do with nginx. Looking at your log, I think you should check your django code instead, /%7B$%20static%20'js/main.js'%20%%7D is url escaped str for { static 'js/main.js' }, this suggests that your django template is not working properly. Commented Aug 12, 2018 at 8:46
  • @hcheung as in a syntax error? I'm just confused as to how the static files are working locally and on the development server, but not on the production server. Commented Aug 12, 2018 at 15:32
  • As I said your nginx setting looks fine (except could have some fine-tuning, but generally okay). Nginx only parse what has been passed to it, nothing more and nothing less. If it is work on the dev server and not on production, check the url composition under browser developer console to see the differences and track back to the django source code. Commented Aug 13, 2018 at 1:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.