0

I got many server aliases in nginx configuration:

server {
    listen      80;
    server_name example.com site1.example.com site2.example.com site3.example.com
    ...

How i can control specific folder for alias in location of robots.txt file, for example i mean:

location ^~ $http_host/robots.txt {
    alias /home/web/public_html/static/$http_host/robots.txt;
}

Construction above not working.

3 Answers 3

1

You can use $host nginx variable:

location /robots.txt {
    alias /home/web/public_html/static/$host/robots.txt;
}

Or:

location /robots.txt {
    root /home/web/public_html/static/$host;
}

By default $host stands for: in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request.

1
  • Thank you. That`s exactly what i need! Work very well.
    – otakuf
    Jul 7, 2017 at 7:41
0

I don't know if that's possible. If it is someone will provide another answer.

Another approach is to create a server block for each site with any custom locations, then include any common directives in a common cile. Something like this

example1.conf

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

  include common1.conf;

  location ^~ robots.txt {
    alias /home/web/public_html/static/example1/robots.txt;
  }
}

example2.conf

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

  include common1.conf;

  location /abcd {
    //whatever
  }

  location ^~ robots.txt {
    alias /home/web/public_html/static/example2/robots.txt;
  }
}

common1.conf

location /common/location {
  //whatever
}
0

You have an issue with your location statement. It only matches the normalized URI sent from the user, that is, the part after domain name and possibly port in the URL and before query arguments.

Furthermore, you don't specify the actual filename in an alias directive, just the path where to find the file.

Therefore you need to use:

location /robots.txt {
    alias /home/web/public_html/static/example1;
}

in your configuration.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .