2

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.

  • For a start, ServerName should be a hostname only and not include a port. As it is a name based VirtualHost, the proxy statement in nginx would then need to refer to it by its hostname and not 127.0.0.1. Only one it could work as 127.0.0.1 is if that was the default VirtualHost. – Graham Dumpleton Aug 6 '14 at 10:34
  • Thank you, Graham. I applied your suggested changes and edited my question with them, but I keep getting the same result. It looks as if nginx is not redirecting to apache. But if I try mysite.dev:8080 I get the django welcome page correctly, so it seems the focus of the problem should be in the nginx configuration. Thanks a lot for your time. – Rubén Cabrera Aug 7 '14 at 12:35
  • After a ps -ef I realized that the process running my nginx was run by the www-data user, which I had changed in my nginx.conf file. This means that the conf file being used is the default one, in /etc/nginx, instead of the one I created at /usr/local/nginx/conf following the tutorial instructions. I'll try to rearrange the configuration files and see wat happens. Including the virtual host file /usr/local/nginx/conf/sites-enabled/mysite from the default nginx.conf didn't work at all. – Rubén Cabrera Aug 7 '14 at 16:10
2

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.

0

Off the bat, you've two closing tags in the Virtual Host definition for Apache. I'll assume you've left the default server document root for Apache unchanged, even though its not listed.

For your NGINX configuration, I noticed you've not listed proxy_redirect. Try including this in your NGINX Server config and seeing if manually specifying the location header helps.

Also, here is a great tutorial I referenced when setting up my own dev environment. (using NGINX for static files and Apapche for dynamic). Perhaps it will give you additional insight to getting up and running correctly.

  • Thanks for your answer. The two closing tags were a typo. The document root for apache is /var/www/html I left proxy_redirect off; following the tutorial instructions and not knowing at first what it was doing. As I understood later, it rewrites the location, but doesn't change how the proxy behaves in any other way. I tried changing it (in my proxy.conf file) to proxy_redirect default; and aldo proxy_redirect / ; . The result remains the same. This makes me think that the proxy is not working at all, as the port is not showing when redirect is off. Thanks a lot anyway. – Rubén Cabrera Aug 7 '14 at 12:20
  • 1
    @RubénBCR Sorry to hear you are still left with the problem. At this point, if I were you, I'd try a fresh install of NGINX as you've confirmed Apache working. You can do a quick install with apt-get. It would likely be easier for you to simply to use the default config NGINX provides and tweak as necessary. Also, this time around try specifying the root directory and using a try_files in the location and see where that gets you. This tutorial should help you get started. Good luck! – chuk2bp Aug 7 '14 at 15:33

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.