在AWS EC2实例上使用nginx配置django

时间:2018-08-12 03:30:56

标签: python django amazon-web-services nginx amazon-ec2

我当前正在配置django应用程序,以使用nginx在产品环境中运行。我真的一直在为此苦苦挣扎,感觉好像我已经在搜索所有堆栈溢出了。

这是我的应用程序的nginx配置文件

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

}

着陆页似乎很好。但是,当我单击应该带我到网站其他页面的链接时,我从nginx收到400错误的请求错误。

这是我的错误的摘要。日志

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

nginx似乎在我的应用程序的命名空间中遇到了url模式问题。

任何帮助将不胜感激。

(注意:我已经使用标准python manage.py runserver 0.0.0.0:8000运行了一个月的网站,并且到目前为止已经可以使用。)

1 个答案:

答案 0 :(得分:1)

所以我意识到这与URL的编码有关。

当我单击链接时,在浏览器的地址栏中,看到

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

如果我将其简化为:

/url'members:member_login'

有效。这是因为我删除了您对URL进行请求时添加的所有特殊字符。

所以我没有我的问题的完整答案,因为我不确定如何使nginx / django自动解析这些内容。但是我走在正确的轨道上。