Search code examples
pythondjangonginxfastcgi

Django website urls going to / on ec2 instance with nginx


I have a django website which works perfectly fine on my local computer on running with runserver. THe website is hosted in an Amazon EC2 instance of Ubuntu 12.04 LTS with nginx as webserver. I have used fcgi with nginx for deployment with django and S3 is used to serve the static files. The home page is displayed properly but for every other urls (even the ones not on urls.py), the home page(root page /) is displayed. I don't know what's going on.

# urls.py (don't think anything is wrong here)

from django.contrib import admin
admin.autodiscover()

from tastypie.api import Api
from rating.myapi import ReturnNext, Rating

v1_api = Api(api_name='v1')
v1_api.register(ReturnNext())
v1_api.register(Rating())

urlpatterns = patterns('',
    url(r'^$', 'fbData.newviews.home', name="home"),
    url(r'^level/$', 'fbData.newviews.level_complete', name="level"),
    url(r'^top/$', 'fbData.newviews.top_style', name="top"),     
    url(r'^facebook/', include('django_facebook.urls')),
    url(r'^accounts/', include('django_facebook.auth_urls')),
    (r'^api/', include(v1_api.urls)),    
    url(r'api/doc/', include('tastypie_swagger.urls', namespace='tastypie_swagger')),
    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

nginx.conf

    user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile off;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 128;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    server {
#       fastcgi_pass    121.0.0.1:3033
#       listen      *:80;
#       listen      127.0.0.1:3033;
#       listen      127.0.0.1:8080;
#       listen      127.0.0.1:80;


        server_name ec2-54-245-140-15.us-west-2.compute.amazonaws.com;

        access_log  /srv/www/test/logs/facebook-app-rating.access.log;
        location / {
        root    /srv/www/test/facebook-app-rating;
        index index.html index.htm;
        fastcgi_pass 127.0.0.1:3033;
        }
    }
}

Solution

  • I don't see any fastcgi_param you are passing from nginx to your django application. And you need them. To solve your problem you need to pass PATH_INFO like this

    server {
        ...
        fastcgi_param PATH_INFO $fastcgi_script_name;
    }
    

    But you'll need other parameters too. You can see examples here or google it.