Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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>
share|improve this answer
    
awesome! thanks. –  Marconi Feb 7 '11 at 8:46
    
I have a follow-up question though, If I only add the server{ ... } in nginx for only django-site.com it'll only proxy django-site.com and other sites will continue to run directly through apache right? –  Marconi Feb 7 '11 at 9:29
1  
@Marconi No. That will only happen if you specify your URL like domain.com:8085. The default port for a web browser to use is port 80, which is what you have Nginx listen on so all traffic has to pass through this. (Unless you use multiple IPs, but that's just making things complicated) –  Martin Fjordvald Feb 7 '11 at 9:41
    
@Martin: Does that mean I need to create an equivalent server{...} in nginx and matching virtualhost in apache for each existing sites that apache used to serve directly? –  Marconi Feb 8 '11 at 6:01
    
@Marconi: Ideally yeah, though if you can use the default flag of the Listen directive to create a server block which will catch all requests without a better matching server block. –  Martin Fjordvald Feb 8 '11 at 7:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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