-3

I am using Django with Nginx and Gunicorn.

When I type in https://DomainName[dot]com in the browser below is the message that I get.

    Page not found (404)
    Request Method: GET
    Request URL:    http://127.0.0.1:8001/

Can some one please suggest why is the request URL different to the url typed in browser (https://DomainName[dot]com). I would really appreciate your help.

Below is my 'Nginx' server configuration for my domain.

server {
        listen 80;
        server_name DomainName[dot]com;

        access_log off;

       # root /opt/myenv/projects/myproject;

        location /static/ {
            alias /opt/myenv/static/;
        }

        location / {
                proxy_pass http://127.0.0.1:8001;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
                add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        }
}

Below is my Gunicorn Configuration

command = '/opt/myenv/bin/gunicorn'
pythonpath = '/opt/myenv/projects/myproject'
bind = '127.0.0.1:8001'
workers = 3
user = 'nobody'

EDIT:

Below are a few of the configurations that I've tried:

  1. Setting the proxy_pass (within my server block) and bind setting in my Gunicorn configuration to 'http://mydomain[dot]com' and When I visit the url I get

    500 Internal Server Error

    When I check the Error logs for Nginx, below are what I get:

    [alert] 16730#0: *766 768 worker_connections are not enough while connecting to upstream, client: 192.241.xxx.xxx, server: mydomain.com, request: "GET / HTTP/1.0", upstream: "http://192.241.xxx.xxx:80/", host: "mydomain[dot]com"

    [error] 16870#0: *7 open() "/opt/myenv/static/assets/jqm/js_css/images/ajax-loader.gif" failed (2: No such file or directory), client: 183.82.xxx.xx, server: mydomain.com, request: "GET /static/assets/jqm/js_css/images/ajax-loader.gif HTTP/1.1", host: "www.mydomain[dot]com", referrer: "http://www.mydomain[dot]com/home/"

  2. Setting the proxy_pass (within my server block) and bind setting in my Gunicorn configuration to 'http://mydomain[dot]com:8001' and When I visit the url I get

    Page not found (404)
    Request Method: GET
    Request URL:    http://mydomain[dot]com:8001/
    

What I am trying to achieve is a way to set my request url to 'http://mydomain.com' so that, When I enable OAuth2 for Facebook authentication, the request url is inline with the app's domain setting in Facebook.

Note: For indicating'.' I've used [dot], as Server fault doesn't allow users with less than 10 reputation to post more than 2 links.

  • Dear Down voter, please suggest what is wrong with the question, at least I can improve it further. Down voting without any comment doesn't help anyone. – user44776 Dec 10 '13 at 7:11
  • You most likely need to fix your application/change a setting. Your nginx config looks ok, but your application shouldn't use your app server's url, but your web server's. – fuero Dec 10 '13 at 7:25
  • @fuero Thanks you your suggestion, I'll search for a way to do that. – user44776 Dec 10 '13 at 8:27
  • It doesn't look like you've done anything beyond show your error and config. You need to do your own research and troubleshooting and include that in your question. – Drew Khoury Dec 10 '13 at 9:01
  • @DrewKhoury I've spent nearly a day and a half researching on web, reading through Django book and trying various possible combinations of proxy_pass (server config) and bind (gunicorn config), setting them to mydomain.com:8001, mydomain.com and vice versa, but none of them really helped and my last bet was to post it here. I didn't see it worthwhile for the readers time, to read my research as I didn't find it tangible and felt, I was missing something very obvious. Would appreciate if you have any suggestions. – user44776 Dec 10 '13 at 9:35
0

Django thinks that it's running on 127.0.0.1:8001 because it is running on 127.0.0.1:8001. The fact that you have an instance of nginx forwarding requests to it doesn't change that.

Have you considered having Gunicorn listen on a UNIX socket and have nginx forward requests to that instead? This should eliminate the problem as Gunicorn won't be listening on a TCP socket at all.

This can be achieved by modifying the Nginx and Gunicorn configuration as shown below

Nginx Configuration:

 upstream app_server {
    server unix:/path/gunicorn.sock fail_timeout=0;
 }

server {
        listen 80;
        server_name DomainName[dot]com;

        access_log off;

        location /static/ {
            alias /opt/myenv/static/;
        }

        location / {

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

                if (!-f $request_filename) {
                    proxy_pass http://app_server;
                    break;
                    }
        }
 }

Replace bind in Gunicorn Configuration to the one shown below:

bind = 'unix:/path/gunicorn.sock'
  • your answer sounds promising, could you please suggest me some online documentation that could guide me to do what you have advised. I've tried searching online, but wasn't successful. – user44776 Dec 10 '13 at 13:52
  • The Gunicorn documentation covers it. You bind to a unix socket with Gunicorn, then you list that socket as a server in your upstream block in the nginx configuration. – Jim Dec 10 '13 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.