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.

link|improve this question
feedback

migrated from stackoverflow.com Oct 28 '11 at 10:43

This question came from our site for professional and enthusiast programmers.

2 Answers

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.

link|improve this answer
feedback

Seems incredibly complicated. Why don't you use nginx on all levels? In any event, the best reference for NGINX configurations I've come across still is: https://calomel.org/nginx.html

  • Option 1: Nginx webserver to serve static files

    This is a basic webserver running on port 80 (http) serving out web pages. Though we have added quite a few security checks, this is as basic a server as you can get. On an AMD64 2GHz machine this config will easily serve out thousands of pages a minute.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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