0

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;

}
Rubytastic
  • 223
  • 1
  • 2
  • 14

1 Answers1

1

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.

Pothi Kalimuthu
  • 6,117
  • 2
  • 26
  • 38
  • Thank you for the writeup; however this redirects http://myapp.com/blog to http://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 03 '13 at 13:17