Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Having a server setup like below. I would like to restrict anyone from accessing http://myapp.com/blog/index.html which would route to /public/blog/index.html. Is this possible or should I really store the blog folder outside public? ( Which I don't want )

server {
  listen 80;
  server_name
  myapp.*

  root /srv/myapp/current/public;

}

server {
  listen 80;
  server_name
  blog.myapp.*

  root /srv/myapp/current/public/blog;

}
share|improve this question

closed as off-topic by Jenny D, Dave M, Ward, mdpc, kce Oct 4 '13 at 16:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must demonstrate a minimal understanding of the problem being solved. Try including attempted solutions, why they didn't work, and the expected results. See How can I ask better questions on Server Fault? for further guidance." – Jenny D, Dave M, Ward, mdpc, kce
If this question can be reworded to fit the rules in the help center, please edit the question.

It is indeed possible. Try one of the two options provided below...

server {
  listen 80;
  server_name
  myapp.*

  root /srv/myapp/current/public;

  location /blog {
    # option 1
    return 444;

    # option 2
    # rewrite /blog/(.*) $scheme://blog.myapp.tld/$1 permanent;
  }

}

server {
  listen 80;
  server_name
  blog.myapp.*

  root /srv/myapp/current/public/blog;

}

Some explanation on the two options...

return 444 is a Nginx specific status code, returning nothing to the browser. Ref: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes . If you prefer to send another status code, for example 403, then remove 444 and put whatever the code you prefer;

rewrite /blog/(.*) $scheme://blog.myapp.tld/$1 permanent; converts the sub-directory to sub-domain. This has been already answered. Ref: Nginx rewrite rule (subdirectory to subdomain)

You may know more about the return statement at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return and about the rewrite statement at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite .

I hope that helps.

share|improve this answer
    
Thank you for the writeup; however this redirects myapp.com/blog to blog.myapp.com/blog even without the $request_uri part. Is there a way to just redirect to blog.myapp.com instead? thank. I made it location /blog { return 301 $scheme://blog.myapp.com; } – Rubytastic Oct 3 '13 at 13:17
    
My apologies. I've fixed the code now. Thanks. – Pothi Oct 3 '13 at 14:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.