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 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.

share|improve this question

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

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

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer

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.