0

I have to create a server_name as a listener for origin pulls by my CDN.

The CDN wants to pull from origin.mydomain.com

I already have 100s of lines of code under www.mydomain.com that showcases all the rewrites, rules and such, and I need to use all this code again.

My easy solution would be to have

server_name www.mydomain.com origin.mydomain.com

To easily have NGINX listen for the requests to the "origin" subdomain.

My fear is that google discovers the subdomain and starts crawling it. I'd like to block google from the "origin" subdomain somehow. Since declaring multiple server_name, I am not sure I can just place robots.txt file somewhere, since using same root folder as live site.

Is there an easy way to do this?

All feedback appreciated.

Cheers Ryan

user3273784
  • 151
  • 1
  • 13

1 Answers1

0

Use two server blocks and use the include directive to pull in the common code. For example:

server {
    server_name www.mydomain.com;
    include /path/to/common/config;

    location = /robots.txt {
        root /path/to/friendly/dir;
    }
}
server {
    server_name origin.mydomain.com;
    include /path/to/common/config;

    location = /robots.txt {
        root /path/to/unfriendly/dir;
    }
}

So you have two robot.txt files in different directories - or use rewrite ... last to map the URI to different local files.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Okay, so simplify by including all the rewrites and such in an external code that can be included. does the include have to have any format? Or just cut/paste into blank config file? – user3273784 Oct 13 '16 at 18:22
  • [The documentation](http://nginx.org/en/docs/ngx_core_module.html#include) states that "Included files should consist of syntactically correct directives and blocks". Basically a cut & paste. – Richard Smith Oct 13 '16 at 18:25
  • Hmmm. I'm getting an error. nginx: [emerg] "location" directive is not allowed here in /usr/local/etc/nginx/conf.d/rewrite_rules.conf:7 – user3273784 Oct 13 '16 at 19:50
  • I am now having in the main site conf `server { server_name www.mydomain.com; include/usr/local/etc/nginx/conf.d/traileraddict_rules.conf location = /robots.txt { root /path/to/friendly/dir; } }` – user3273784 Oct 13 '16 at 20:03
  • It's not a good idea to put the files in an existing include directory, otherwise some other `nginx` file is going to suck them in too. You will probably find that `conf.d` is already taken. – Richard Smith Oct 13 '16 at 20:08
  • ok, I instead made the include point to `include /usr/include/rewrite_rules;` Does that work? – user3273784 Oct 13 '16 at 20:12
  • I changed the directory again, include /home/ to make sure no chance of OS making changes. All in all, it worked! I tried to upvote your answer but my level isn't high enough yet. – user3273784 Oct 13 '16 at 22:29