Nginx – How to serve multiple domain static files with nginx in front of apache

apache-2.2djangonginx

I would like to run multiple domains from the same server. I would like nginx to sit in front of apache and serve static files. So requests for one.com/site_media/logo.png points to /var/www/one/site_media/logo.png and two.com/site_media/logo.png points to /var/www/two/site_media/logo.png. Where am I going wrong in the following configuration?

ports.conf

 NameVirtualHost *:80
 Listen 8080

nginx/sites-available/one.com

server {
   listen 80;
   server_name one.com;
   access_log /var/www/one/nginx_access.log;
   error_log /var/www/one/nginx_error.log;
   location / {
   proxy_pass  http://127.0.0.1:8080/;
   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;
   proxy_set_header   X-Magic-Header "secret";
   client_max_body_size 10m;
  }

  location ^~ /site_media/ {
     root /var/www/one;
     if ($query_string) {
         expires max;
     }
  }

nginx/sites-available/two.com

server {
   listen 80;
   server_name two.com;
   access_log /var/www/two/nginx_access.log;
   error_log /var/www/two/nginx_error.log;
   location / {
   proxy_pass  http://127.0.0.1:8080/;
   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;
   proxy_set_header   X-Magic-Header "secret";
   client_max_body_size 10m;
  }

  location ^~ /site_media/ {
     root /var/www/two;
     if ($query_string) {
         expires max;
     }
  }

apache2/sites-available/one.com

<VirtualHost *:8080>
    ServerName one.com
    ServerAlias www.one.com
    WSGIScriptAlias / /var/www/one/one.wsgi
    ErrorLog /var/www/one/apache_error.log
    LogLevel warn
    CustomLog /var/www/one/apache_access.log combined
</VirtualHost>

apache2/sites-available/two.com

<VirtualHost *:8080>
    ServerName two.com
    ServerAlias www.two.com
    WSGIScriptAlias / /var/www/two/two.wsgi
    ErrorLog /var/www/one/apache_error.log
    LogLevel warn
    CustomLog /var/www/one/apache_access.log combined
</VirtualHost>

A request on two.com for "/site_media/logo.png" fails because the server looks in /var/www/one/site_media. I would like to keep the "/site_media" hrefs for both sites. Is there a better way to rewrite the nginx location ^~ /site_media/? Or something else?

(Edit) My Workaround Is:

  location ^~ /site_media/ {
     if ($host = 'one.com') {
         root /var/www/one;
     }
     if ($host = 'two.com') {
         root /var/www/two;
     }
     if ($query_string) {
         expires max;
     }

This seems really "hacky" and not elegant.

Best Answer

Root directives should be at server level.

server {
   listen 80;
   server_name one.com;
   root /var/www/one;
   ...

server {
   listen 80;
   server_name two.com;
   root /var/www/two;
   ...

Also make sure the document root in Apache is properly defined for each.