Nginx virtualhosts

hi guys, i am having some issues with virtualhosting in nginx:

the config below:

i want to make both host1.com and host2.com work, and serve static
content directly.

http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
log_format main '$remote_addr - $remote_user [$time_local]
$request ’
'“$status” $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 20;
#gzip on;

server {
    listen       xx.xx.xx.xx:80;
    server_name  host1.com;
    #charset koi8-r;
    access_log  logs/host1.com.access.log main;
    location /data {
        root /home/fs01/storage;
        internal;
        }
    location / {
        root   /home/fs01/www;
        index  index.html index.htm index.php;
    }

    location / {
        proxy_pass        http://localhost:8000/;
        proxy_redirect    off;
        proxy_set_header  X-Forwarded-For

$proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;

    }
server {
    listen       xx.xx.xx.xx:80;
    server_name  host2.com;
    #charset koi8-r;
    access_log  logs/host2.com.access.log main;
    location / {
                                                                           access_log

logs/host2.com.access.log main;
location / {
root /home/m1emuz/public_html;
internal;
}
location / {
root /home/fs01/www;
index index.html index.htm index.php;
}

    location / {
        proxy_pass        http://localhost:8000/;
        proxy_redirect    off;
        proxy_set_header  X-Forwarded-For

$proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;

    }


    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip)$ {
        root              /home/fs01/storage;
        access_log        off;
        expires           30d;
    }

    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip)$ {
        root              /home/m1emuz/public_html;
        access_log        off;
        expires           30d;
    }


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/www/nginx-dist;
    }



    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

the error i am getting when restarting the nginx.

Starting nginx.
2008/01/08 16:15:23 [emerg] 88032#0: “server” directive is not allowed
here in /usr/local/etc/nginx/nginx.conf:47

nginx version: nginx/0.5.34

thanks. much appreciated.

On Tue, Jan 08, 2008 at 05:12:16PM +0100, Stefanita rares Dumitrescu
wrote:

server_names_hash_bucket_size  128;
server {
    listen       xx.xx.xx.xx:80;
    server_name  host1.com;
    #charset koi8-r;
    access_log  logs/host1.com.access.log main;
    location /data {
        root /home/fs01/storage;
        internal;
        }

Here you have two different “location /”:

        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  Host $http_host;

    }

You forgot here

  }
        internal;

$proxy_add_x_forwarded_for;
expires 30d;
# redirect server error pages to the static page /50x.html
#
#location ~ /.ht {
# deny all;
#}

 }

Igor S. wrote:

On Tue, Jan 08, 2008 at 05:12:16PM +0100, Stefanita rares Dumitrescu
wrote:

server_names_hash_bucket_size  128;
server {
    listen       xx.xx.xx.xx:80;
    server_name  host1.com;
    #charset koi8-r;
    access_log  logs/host1.com.access.log main;
    location /data {
        root /home/fs01/storage;
        internal;
        }

Here you have two different “location /”:

you mean :

    location /data {
        root /home/fs01/storage;

this one?

btw. i have another question.

the static config. the syntax is correct? i mean having 2 locations
defined with static content. i defined 1 for each virtualhost. is it
possible to make it global?

i tried defining
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip)$ {
root /;

instead of

    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip)$ {
        root              /home/fs01/storage;
        access_log        off;
        expires           30d;
    }

    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip)$ {
        root              /home/m1emuz/public_html;
        access_log        off;
        expires           30d;
    }

but seems it’s not working.

On Tue, Jan 08, 2008 at 05:55:50PM +0100, Stefanita rares Dumitrescu
wrote:

    location /data {
        root /home/fs01/storage;

this one?

No I meant

    location / {
        root   /home/fs01/www;
        index  index.html index.htm index.php;
    }

    location / {

Please read http://wiki.codemongers.com/NginxHttpCoreModule#location

instead of
root /home/m1emuz/public_html;
access_log off;
expires 30d;
}

but seems it’s not working.

No, please read
http://wiki.codemongers.com/NginxHttpCoreModule#location
http://wiki.codemongers.com/NginxHttpCoreModule#root

On Tue, 2008-01-08 at 17:12 +0100, Stefanita rares Dumitrescu wrote:

        root   /home/fs01/www;

    }
 }
server {
    listen       xx.xx.xx.xx:80;
    server_name  host2.com;
    #charset koi8-r;
    access_log  logs/host2.com.access.log main;
    location / {                

Cliff

thanks for all the help guys. i am working this out today, and then test
it :slight_smile:

you have nested two “servers” directive. check your your closing
parenthesis for your first server directive. :slight_smile:

On Jan 9, 2008 12:12 AM, Stefanita rares Dumitrescu

user nobody;
worker_processes 10;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
log_format main '$remote_addr - $remote_user [$time_local]
$request ’
'“$status” $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 20;
#gzip on;

server {
    listen       11.11.11.11:80;
    server_name  host1.com;
    #charset koi8-r;
    access_log  logs/host1.com.access.log main;

    location / {
        root   /home/fs01/www;
        index  index.html index.htm index.php;
    }

    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip|mp3)$ {
        root              /home/fs01/storage;
        access_log        off;
        expires           30d;
    }


    location / {
        proxy_pass        http://localhost:8000/;
        proxy_redirect    off;
        proxy_set_header  X-Forwarded-For 

$proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/www/nginx-dist;
    }

}

server {
listen 11.11.11.11:80;
server_name host2.net;
#charset koi8-r;
access_log logs/host2.net.access.log main;

    location / {
        root   /home/m1emuz/public_html;
        index  index.html index.htm index.php;
    }

    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|flv|zip|mp3)$ {
        root              /home/m1emuz/public_html;
        access_log        off;
        expires           30d;
    }

    location / {
        proxy_pass        http://localhost:8000/;
        proxy_redirect    off;
        proxy_set_header  X-Forwarded-For 

$proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;

    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/www/nginx-dist;
    }

}

}

this is the full new config. i understand the ‘root’ and ‘location’
directives now, but idon’t understand, why accessing host2.net, brings
me to host1.com folder. i know that once the first match is done, the
query stops, but it’s virtual host. and i don’t understand why the files
are not served from
/home/m1emuz/public_html

erm . so i need to create another instance of apache running on another
port. yikes.

well, a man’s gotta do what a man’s gotta do.

Well I’m assuming since this is a ruby forum that you have two (maybe
rails) apps which you are trying to separate out. But you need to pass
(proxy_pass) the requests to two different ports running your two
different apps. For instance localhost:8000 and localhost:7000. Maybe
I’m reading your config/intent wrong.

Your proxy_pass is set to the same port for both servers. Are these two
sites running from the same code base? If not you need to pass the
requests off to two different upstream servers.

worked out. it seems i forgot to add the virtualhost config to the
apache server :slight_smile: thanks guys. now i have a working virtualhost config :stuck_out_tongue:

unsubscribe

the system works out with nginx - serving the static content, and
passing the dynamic requests to an apache & php server listening to the
internal interface on port 8000.

On Wed, 2008-01-09 at 17:31 +0100, Asa Wilson wrote:

Well I’m assuming since this is a ruby forum

This isn’t a Ruby forum. You are viewing the Nginx mailing list via a
Ruby forum.

Cliff

On Wed, 2008-01-09 at 17:33 +0100, Stefanita rares Dumitrescu wrote:

the system works out with nginx - serving the static content, and
passing the dynamic requests to an apache & php server listening to the
internal interface on port 8000.

Do you need Apache for a particular purpose? If it’s just PHP you might
consider just using PHP+FastCGI instead.

Cliff

nop there is no particular purpose i have apache for, i am just getting
the hang of nginx for now, and i have a lot of stuff to do, so i decided
that running this setup would be the minimum administrative efforts for
now, and i get the benefits of nginx for static content as well.

once my setups are complete, i will be going into further more
optimization/ changes :slight_smile: