2

I have bought a dedicated server from DigitalOcean, and configured nginx, mysql and php, its IP address is a.b.c.d. I have bought two domains from GoDaddy: siteone.com and sitetwo.com. I made a subdomain sub.siteone.com, it will be a WordPress site. I made sub.siteone.com forward to a.b.c.d in GoDaddy.

By following this post and this post, I put the WordPress files under /var/www/sub.siteone.com/html/. /etc/nginx/sites-available/sub.siteone.com contains follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/sub.siteone.com/html;
    index index.php index.html index.htm;

    server_name sub.siteone.com;

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

As a result, when I enter sub.siteone.com in a browser, it redirects to a.b.c.d and shows well the WordPress site.

The problem is that I want my server (a.b.c.d) to contain several sites (i.e., domains). So a mechanism I imagine would be a.b.c.d/one/ contains the first domain, and a.b.c.d/two/ contains the second domain. As a result,

  • when I enter sub.siteone.com in a browser, the url remains the same while the content is from a.b.c.d/one/;
  • when I enter e.g. sub.siteone.com/photos/ in a browser, the url remains the same while the content is from a.b.c.d/one/photos;
  • when I enter sitetwo.com in a browser, the url remains the same while the content is from a.b.c.d/two/;
  • when I enter e.g. sitetwo.com/downloads/ in a browser, the url remains the same while the content is from a.b.c.d/two/downloads

Could anyone tell me if this mechanism makes sense? And how could I modify the setting of nginx and GoDaddy to realise that?

SoftTimur
  • 307
  • 2
  • 5
  • 14

4 Answers4

6

What you want is called virtual hosting. And it works much better than what you described!

Just add multiple server blocks with different server_name and root directories. One server block per domain name. Note that only one of them can be marked as default_server but apart from that all blocks can look identical.

The best way to do this is by creating one file per site, and then symlink each file into /etc/nginx/sites-enabled/. After doing that, restart nginx with service nginx restart

Emil Vikström
  • 542
  • 3
  • 11
5

You can achieve this easily using server blocks in Nginx to create multiple 'virtual hosts', each with a different HTTP configuration. This works by Nginx processing your request differently depending on which URL you used to reach the server.

Put simply, to reach siteone.com you need to create a server block with a server_name value of siteone.com. Each server block can be in it's own file or in the same file, it doesn't really matter.

siteone.com:

server {
    listen 80;
    server_name sub.siteone.com;
    root /var/www/sub.siteone.com/html;
    # ADD ADDITIONAL SITEONE CONFIGURATION HERE
}

sub.siteone.com:

server {
    listen 80;
    server_name sub.siteone.com;
    root /var/www/sub.siteone.com/html;
    # ADD ADDITIONAL SUB.SITEONE CONFIGURATION HERE
}

sitetwo.com

server {
    listen 80;
    server_name sitetwo.com;
    root /var/www/sitetwo.com/html;
    # ADD ADDITIONAL SITETWO CONFIGURATION HERE
}

This should provide the behaviour you need. It's also worth noting that you can do a lot more than use different root directories for each server, it's also useful to have different log files for each virtual host, forwarding requests and many more features.

Here's a crude example of having specific log files for sitetwo.com

server {
    listen      80;
    server_name sitetwo.com;
    root        /var/www/sitetwo.com/html;
    error_log   /var/www/sitetwo.com/logs/error.log;
    access_log  /var/www/sitetwo.com/logs/access.log;
    # ADD ADDITIONAL SITETWO CONFIGURATION HERE
}
1

With Nginx, you should create as many site config files as required and simply modify the server_name and root sentences to map external domains to their content and code.

ma.tome
  • 1,169
  • 8
  • 15
0

Ben's Answer is good too , here is an alternative just for the sake of variety : Create a server and inside it add multiple location blocks pointing to the location of your site.

server{

   listen  443 ssl;
   server_name dev.domainofmysite.com;

   server_tokens off;


   location /param {
     proxy_pass http://127.0.0.1:8888;
   }
   location /tparam {

     proxy_pass http://127.0.0.1:7172;
   }
   location /dparam {
     proxy_pass http://127.0.0.1:7172;
   }

}