Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Sun Apr 24, 2011 3:44 am 
Offline
Senior Newbie

Joined: Sun Oct 17, 2010 6:20 pm
Posts: 7
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 ?


Top
   
PostPosted: Sun Apr 24, 2011 12:36 pm 
Offline
Senior Member
User avatar

Joined: Sat Aug 30, 2008 1:55 pm
Posts: 1739
Location: Rochester, New York
sri12 wrote:
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.

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

_________________
Code:
/* TODO: need to add signature to posts */


Top
   
 Post subject:
PostPosted: Sun Apr 24, 2011 2:12 pm 
Offline
Senior Newbie

Joined: Sun Oct 17, 2010 6:20 pm
Posts: 7
thanks hoopycat.

how do I edit the existing site config file to point
http://foo.example.com/ to http://example.com/foo/ ?


Top
   
 Post subject:
PostPosted: Sun Apr 24, 2011 3:52 pm 
Offline
Senior Member
User avatar

Joined: Sat Aug 30, 2008 1:55 pm
Posts: 1739
Location: Rochester, New York
If you're looking to do a redirect, so that it'll say http://example.com/foo/ in their address bar,

Code:
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:

Code:
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,

Code:
  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 server_names_hash_max_size)

_________________
Code:
/* TODO: need to add signature to posts */


Top
   
 Post subject:
PostPosted: Sun Apr 24, 2011 5:09 pm 
Offline
Senior Newbie

Joined: Sun Oct 17, 2010 6:20 pm
Posts: 7
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

Code:
 

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;
}
 


Top
   
 Post subject:
PostPosted: Mon Apr 25, 2011 2:40 pm 
Offline
Senior Member
User avatar

Joined: Sat Aug 30, 2008 1:55 pm
Posts: 1739
Location: Rochester, New York
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.

_________________
Code:
/* TODO: need to add signature to posts */


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group