how to create a sub domain in linodemanager and host it

i need to create a subdomain and configure my nginx to support it. how do I do it, looking up some articles are very confusing.

  • Do I need to do it both on the linode manager and as well as in the nginx?

  • In nginx do I have to treat it as a complete new site and create a new file under "sites-available" and "site-enabled" directories ?

  • or do i modify the nginx file for the same domain and add a mapping to point it to a different directory.

Can someone provide some instructions on how to complish this ?

5 Replies

@sri12:

i need to create a subdomain and configure my nginx to support it. how do I do it, looking up some articles are very confusing.

  • Do I need to do it both on the linode manager and as well as in the nginx?

If your domain's DNS is configured via the Linode Manager, yes. You'll need to have an "A" record in the DNS for example.com for subdomain.example.com to work. This will allow the rest of the world to find the IP address of the web server.

> - In nginx do I have to treat it as a complete new site and create a new file under "sites-available" and "site-enabled" directories ?

  • or do i modify the nginx file for the same domain and add a mapping to point it to a different directory.

It's up to you… nginx doesn't care either way. I would tend to put it in its own file, since it makes it easier to find the configuration the next time you need to change it. If it is just a redirect, e.g. http://foo.example.com/ redirects to http://example.com/foo/, I would keep that in the same file as example.com's config.

The sites-* directories are set up for your benefit, to make it easier for you (or some other sysadmin) to find a site's configuration quickly. So, don't be afraid to use it.

thanks hoopycat.

how do I edit the existing site config file to point

http://foo.example.com/ to http://example.com/foo/ ?

If you're looking to do a redirect, so that it'll say http://example.com/foo/ in their address bar,

server {
  listen 80;
  server_name  foo.example.com;

  location / {
    rewrite ^/(.*)$ http://example.com/foo/$1 permanent;
  }
}

If you want it to exist "on its own", you'd set it up as a normal virtual host, just like example.com's configuration, but with the root as the subdirectory. The snippet here is ripped almost verbatim from the library article linked above:

server {
  listen 80;
  server_name foo.example.com;
  access_log /srv/www/example.com/logs/foo-access.log;
  error_log /srv/www/example.com/logs/foo-error.log;

  location / {
    root   /srv/www/example.com/public_html/foo;
    index  index.html index.htm;
  }
}

Be mindful of any configuration that might need to be carried over from example.com's config to foo.example.com, like fastcgi_pass or logging stuff.

If you're doing it this way, you'll probably also want to add a redirect from http://example.com/foo/ to http://foo.example.com/, so that there is one and only one canonical URL for the content. Into example.com's config,

  location /foo {
    rewrite ^/foo(.*)$ http://foo.example.com$1 permanent;
  }

(A quick note about the usage of rewrite here: The first argument to rewrite is a regular expression that matches on the pathname part of the URL, so http://example.com/foo/bar is /foo/bar, http://example.com/ is /, etc. The "(.*)" says "grab everything between here and the next match and shove it into $1", and the "$" at the end represents the end of the line.)

I haven't tested these configuration snippets, but I did look at a working configuration template when writing them, so they're written with love. -rt (you haven't lived until you've had to increase servernameshashmaxsize)

thanks again, how do I do this if I am using an existing config file of domain.com (instead of creating a new one)

Config I have looks like this

server {
listen   80;
server_name www.domain.com domain.com  *.domain.com;
access_log /srv/www/domain.com/logs/access.log;
error_log /srv/www/domain.com/logs/error.log;

location / {
    root   /srv/www/domain.com/public_html;
    index  index.html index.htm;
}

location /phpmyadmin { root /usr/share; index index.php; }

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  /srv/www/domain.com/public_html$fastcgi_script_name;
}

You'd add another server{} block, outside of the one already present in the file.

Note that your existing configuration has *.domain.com as a server_name… I'm not sure how this will interact with foo.domain.com, but I think I've seen something about this case in the nginx documentation.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct