Hi I have created several Aliasdomains, and hosting different domain name website with same CMS (multi-sites). the ngnix vhost file is like below. since each aliasdomain website needs to have their own robots.txt file, I need to redirect to the related robots.txt file based on the the domain. I tried to add the codes below to achieve this, but got error "nginx: [emerg] "alias" directive is not allowed here". I wonder how I can achieve this case correctly. thanks server { listen *:80; listen [::]:80; listen *:443 ssl http2; ssl_protocols TLSv1.2; listen [::]:443 ssl http2; server_name site1.com www.site1.com sub1.site2.com sub2.site2.com ; root /var/www/site1.com/web/; disable_symlinks if_not_owner from=$document_root; index index.html index.htm index.php index.cgi index.pl index.xhtml; location ~ /\. { deny all; } location = /robots.txt { allow all; log_not_found off; access_log off; if ($http_host = "sub1.site2.com") { alias /var/www/clients/client0/web2/web/robots.sub1.site2.com.txt; } if ($http_host = "sub2.site2.com") { alias /var/www/clients/client0/web2/web/robots.sub2.site2.com.txt; } } location @php { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/lib/php7.2-fpm/web2.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; } }
I am not sure if it can work correctly that way but my guess is it will be easier if each alias domain has its own vhost.
The proper way to do this with nginx is the "map" feature. More information can be found here: https://nginx.org/en/docs/http/ngx_http_map_module.html#map Like this example: Code: map $http_host $examplecom_robotstxt { hostnames; default robotstxt/development.txt; example.com robotstxt/production.txt; } server { ... location = /robots.txt { rewrite .* /$examplecom_robotstxt break; } ... } Please post your configs in "CODE" Tags. It's way easier to read. Regarding your config and the error, the alias definition within the IF statement seems off, this should be something like this is guess: Code: if ($http_host = "www.domain.com") { rewrite ^/robots.txt /robots.txt last; } BUT ... You shouldn't use IF statements in location defintions ever. The correct way to achieve what you are trying to be, would either be the map feature or define multiple "server {}" sections.