Nginx – have both apache and nginx serve the websites

apache-2.2nginx

I have an existing apache serving a couple of sites.

Now I have a new django site that's ajax intensive and so I'm planning to run it on apache's mod_wsgi but I'll use nginx as a reverse proxy.

Is it possible to have nginx be a reverse proxy to this new django site while make apache serve the other sites directly without using nginx?

Also if you can give me a rough setup on how I might do it if its possible.

Best Answer

The way I solved this problem is to have nginx reverse proxy all of the sites, but have them go to different apache virtual hosts listening on different ports.

nginx (in /etc/nginx)

proxy.conf

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;

/etc/nginx/sites-enabled/default

server {
listen 80;
server_name django-site.com;
location / {
proxy_pass http://127.0.0.1:8085;
include /etc/nginx/proxy.conf;
}
}
server {
listen 80;
server_name regular-site.com;
location / {
proxy_pass http://127.0.0.1:8086;
include /etc/nginx/proxy.conf;
}
}

Apache Settings /etc/apache2/sites-enabled

django-site

NameVirtualHost 127.0.0.1:8085
<VirtualHost 127.0.0.1:8085>
    ServerName django-site.com

    <Location "/">
       SetHandler python-program
       PythonHandler django.core.handlers.modpython
       SetEnv DJANGO_SETTINGS_MODULE settings
       PythonOption django.root
       PythonDebug On
       PythonPath "['/django/django-site'] + sys.path"
    </Location>
</VirtualHost>

regular-site

NameVirtualHost 127.0.0.1:8086
<VirtualHost 127.0.0.1:8086>
ServerName regular-site.com
ServerAlias regular-site.com
ServerAdmin admin@regularsite.com
DocumentRoot /var/www/regular-site/
</VirtualHost>

In /etc/apache2/apache2.conf

# Include ports listing
Include /etc/apache2/ports.conf

ports.conf must list every port /etc/apache2/ports.conf

#Listen 80
#disabled for nginx
Listen 8085
Listen 8086

<IfModule mod_ssl.c>
    Listen 443
</IfModule>