Nginx + Apache mod_wsgi, troubles understanding how to configure servers

apache-2.2configurationdjangonginx

I've been trying for too long to set up a development environment on my Ubuntu 12.04. My intention is to set up everything as I'd have to do later in a production environment, so I followed this tutorial on how to set up Nginx as a reverse proxy with Apache 2.

I've been searching and my question is quite similar to this one, but no answer was given there and I cannot comment on it to add some of my research.

Let's get into the files:

Apache and wsgi

First, I installed mod_wsgi from apt-get and then created a mysite file in /etc/apache2/sites-available:

<VirtualHost *:8080>
    ServerAdmin mymail@mymail.com

    ServerName mysite.dev

    ErrorLog /path/to/developmentfolder/mysite/logs/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
    LogLevel debug

    CustomLog /path/to/developmentfolder/mysite/logs/access.log combined

    WSGIDaemonProcess mysite

    WSGIProcessGroup mysite 

    WSGIScriptAlias / /path/to/developmentfolder/mysite/apache/django.wsgi
    <Directory /path/to/developmentfolder/mysite/apache>
    Order allow,deny
    Allow from all
    </Directory>



</VirtualHost>

Note that I didn't change httpd.conf at all for this set up (I don't know if this is a mistake).

Then I modified ports.conf:

#NameVirtualHost *:80
#Listen 80

#Modificaciones para usar nginx
Listen 8080
NameVirtualHost *:8080

Next, I created

import os, sys

apache_configuration = os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append('/path/to/developmentfolder/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Then run:

# a2ensite mysite

#/etc/init.d/apache2 restart

And I was done with the Apache configuration.

At this point, after adding an entry to my hosts file, I can go to mysite.dev:8080 and see the "It works" message from Django.

Nginx

I insalled Nginx 1.6.

Created an user to run the nginx server:

    # adduser –system –no-create-home –disabled-login –disabled-password –group nginx

Then created a script for nginx to start automatically, I actually downladed it from http://library.linode.com/assets/658-init-deb.sh, placed it at /etc/init.d/, renamed it to nginx and then made some changes:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO


PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin

# Changed the path of the DAEMON as the one in the original script didn't match mine
DAEMON=/usr/sbin/nginx
#Everything from here is exactly as it was
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
            --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /opt/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile     /opt/nginx/logs/$NAME.pid \
              --exec $DAEMON
          echo "$NAME."
          ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
            exit 1
            ;;
    esac

    exit 0

Next, created the folders /usr/local/nginx/conf/sites-aviable/ and /usr/local/nginx/conf/sites-enabled/ and edited /usr/local/nginx/conf/nginx.conf to this (after some modifications of the original from the tutorial):

user nginx;
worker_processes 1;
error_log logs/error.log;
pid       logs/nginx.pid;
events {
    worker_connections 1024;
}
http {
    #modificado el include con una ruta donde realmente se encuentra el archivo
    include         /etc/nginx/mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    tcp_nopush      on;
    keepalive_timeout 65;
    gzip            on;
    include         /usr/local/nginx/conf/sites-enabled/*; 
}

Then, created /usr/local/nginx/conf/sites-aviable/mysite . As my Apache seems to be working fine, and after reading some Nginx recomendations, I have the feeling that the problem is in this file, but I'm new with servers and can't figure out why:

server {
    listen 80;
    server_name mysite.dev;
    access_log /path/to/developmentfolder/mysite/logs/nginx_access.log;
    error_log /path/to/developmentfolder/mysite/logs/nginx_error.log;

    location / {
        proxy_pass  http://mysite.dev:8080;
        include     /usr/local/nginx/conf/proxy.conf;
    }
    location ~ /(js/|css/|imagenes/|media).* {

        root /path/to/developmentfolder/mysite/mysite/static;
    }
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
        root    html;
    }
}

This is the proxy.conf file included above:

proxy_redirect      off;
 proxy_set_header        Host             $host;
 proxy_set_header        X-Real-IP        $remote_addr;
 proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
 client_max_body_size    10m;
 client_body_buffer_size 128k;
 proxy_connect_timeout   90;
 proxy_send_timeout      90;
 proxy_read_timeout      90;
 proxy_buffer_size       4k;
 proxy_buffers           4   32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;

Then created a symbolic link in sites-enabled to the configuration file in sites-available:
# ln -s /usr/local/nginx/conf/sites-aviable/ceia /usr/local/nginx/conf/sites-enabled/

Restarted nginx:

# /etc/init.d/nginx restart

And when I access mysite.dev I only get the "Welcome to Nginx" page, but, if I understood it right, it should be the "It worked" (welcome to django) page again.

I also noticed that I cannot find the nginx error logs, not in my development folder (located in my home folder) or in /usr/local/nginx/conf

How can I make nginx to redirect properly to apache?

Is this a permissions issue related to the nginx user I created?

Thanks a lot, sorry for the huge post.

Best Answer

Eureka, I made it work.

The main issue was with the tutorial info, it was significantly out of date.

I installed nginx 1.6, which creates a folder structure similar to Apache, with sites-available and sites-enabled. There is no need (and no use if you don't know how to configure it) on the steps I made creating those folders in /usr/local/nginx...

The default conf file is also located in the installation directory, /etc/nginx/nginx.conf . I didn't have to add any lines to this file.

This is the conf file for mysite, located at /etc/nginx/sites-available (and also linked from sites-enabled)

server {
listen 80;
server_name mysite.dev;


location / {
    proxy_pass  http://mysite.dev:8080/;
    #The following lines were at proxy.conf, but I copied them here trying to avoid any
    # problems managing permissions or files. The include directive for proxy.conf is commented.
    proxy_redirect off;
     proxy_set_header   Host $host;
     proxy_set_header   X-Real-IP $remote_addr;
     proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

     client_max_body_size 10m;
     client_body_buffer_size 128k;

     proxy_connect_timeout  90;
     proxy_send_timeout     90;
     proxy_read_timeout     90;
     proxy_buffer_size      4k;
     proxy_buffers          4 32k;
     proxy_busy_buffers_size 64k;
     proxy_temp_file_write_size 64k;
    # This might work, but bypassed it to test a simpler solution
    # include     /usr/local/nginx/conf/proxy.conf;
}
location ^~ /static/{

    alias /var/www/mysite/static/;
    access_log off;

}

# I'm not completely sure about the following, but I don't have any troubles with it yet and
# as far as I know, it might be right.
error_page  500 502 503 504 /50x.html;
location = /50x.html {

    root    html;
}
}

The rest of the files are as I listed in the question.

Then, DON'T FORGET TO DELETE YOUR BROWSER CACHE after modifications or you can go crazy.

Thank you all for your help. I hope this answer helps people facing the same problem in the future.