Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving two sites from one server with Nginx

Tags:

nginx

proxy

I have a Rails app up and running on my server and now I'd like to add another one.

I want Nginx to check what the request is for and split traffic based on domain name

Both sites have their own nginx.conf symlinked into sites-enabled, but I get an error starting nginx Starting nginx: nginx: [emerg] duplicate listen options for 0.0.0.0:80 in /etc/nginx/sites-enabled/bubbles:6

They are both listening on 80 but for different things.

Site #1

upstream blog_unicorn {   server unix:/tmp/unicorn.blog.sock fail_timeout=0; }  server {   listen 80 default deferred;   server_name walrus.com www.walrus.com;   root /home/deployer/apps/blog/current/public;    location ^~ /assets/ {     gzip_static on;     expires max;     add_header Cache-Control public;   }    try_files $uri/index.html $uri @blog_unicorn;   location @blog_unicorn {     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     proxy_set_header Host $http_host;     proxy_redirect off;     proxy_pass http://blog_unicorn;   }    error_page 500 502 503 504 /500.html;   client_max_body_size 4G;   keepalive_timeout 10; } 

Site two:

upstream bubbles_unicorn {   server unix:/tmp/unicorn.bubbles.sock fail_timeout=0; }  server {   listen 80 default deferred;   server_name bubbles.com www.bubbles.com;   root /home/deployer/apps/bubbles/current/public;    location ^~ /assets/ {     gzip_static on;     expires max;     add_header Cache-Control public;   }    try_files $uri/index.html $uri @bubbles_unicorn;   location @bubbles_unicorn {     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     proxy_set_header Host $http_host;     proxy_redirect off;     proxy_pass http://bubbles_unicorn;   }    error_page 500 502 503 504 /500.html;   client_max_body_size 4G;   keepalive_timeout 10; } 
like image 920
Chris Avatar asked Dec 03 '12 03:12

Chris


People also ask

Can one server have two websites?

If you have one big server with extensive resources like lots of HDD space, RAM, and CPU power, then you can host multiple websites using virtual hosting. You can host an unlimited number of websites on your Apache webserver. Make sure your server has enough resources to handle the traffic and disc space requirements.

Can NGINX have multiple server?

If you are using a dedicated server / VPS and want to host multiple applications on a separate domain and a single server then you will need to host multiple websites on a single server. You can achieve this with Apache / Nginx virtual hosting. Virtual hosting allows you to use a single VPS to host all your domains.

How do I host multiple websites with one domain?

You can host more than one domain name on your web hosting package and there are two main ways to do this: Addon Domains. Reseller Hosting. Virtual Private Server (VPS)


1 Answers

The documentation says:

The default_server parameter, if present, will cause the server to become the default server for the specified address:port pair.

It's also obvious, there can be only one default server.

And it is also says:

A listen directive can have several additional parameters specific to socket-related system calls. They can be specified in any listen directive, but only once for the given address:port pair.

So, you should remove default and deferred from one of the listen 80 directives. And same applies to ipv6only=on directive as well.

like image 53
VBart Avatar answered Oct 15 '22 17:10

VBart