nginx if specific subdomain endpoint - nginx

I'd like to put robots.txt for the subdomain dev. to point to a specific robots.txt static file.
What routing rule do I need?
This is what i'm thinking:
if ($host = "dev.example.com") {
location ^~ /robots.txt {
allow all;
root /static/disallow/robots.txt;
}
}
Based on Nginx subdomain configuration I believe I may need to just make separate server blocks and use include's. If not a simple routing rule, is the includes method how this is typically done?

If you are wanting a specific robots.txt for each subdomain, you can do so with separate server blocks like this, which you allude (and link) to in your question:
server {
server_name subdomainA.domain.com;
include /common/config/path;
location = /robots.txt {
root /subdomainA/path;
}
}
server {
server_name subdomainB.domain.com;
include /common/config/path;
location = /robots.txt {
root /subdomainB/path;
}
}
Regarding your other approach, have you read If is Evil?

Related

How to serve a set of files from nginx only if a domain name has a keyword

I have a usecase where a files are grouped and each group can be served for a specific domain name in nginx server. I am not sure how can I acheive that. valid_referer is something which is similar but that works on referer header not on original url. Is there anything similar to valid_referer like valid_origin ?
with valid refererer my config looks like this. But would prefer to handle same via origin condition instead.
location /landing/jack/ {
valid_referers ~\.codejack\. ~\.jacktest\.;
if ($invalid_referer) {
return 403;
}
}
Edit: files are grouped via location. So each location can say to be linked to one or more origins.
location /landing/google/ {
valid_referers ~\.google\.;
if ($invalid_referer) {
return 403;
}
}
location /landing/stackoverflow/ {
valid_referers ~\.stackoverflow\.;
if ($invalid_referer) {
return 403;
}
}
All I need is conditional way where I use origin url instead of referer url.
I think you are looking for nginx server blocks
Example in your config file you can have like this.
server {
server_name sub1.yourdomain.com;
root /var/www/sub1;
index index.html;
}
server {
server_name sub2.yourdomain.com;
root /var/www/sub2;
index index.html;
}
server {
server_name sub3.yourdomain.com;
root /var/www/sub3;
index index.html;
}
For more information, https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

Same root folder for multiple website different robots.txt

I have a first website with this configuration:
server {
server_name mywebsite.com;
root /var/www/website/prod;
...
}
and a second one like this with the same root folder:
server {
server_name pro.mywebsite.com;
root /var/www/website/prod;
...
}
And the only problem is they have the same robots.txt file.
Is it possible to tell the second server to link the file robots.txt to another file like robots-pro.txt?
And by the way the url http://pro.mywebsite.com/robots.txt would open the file robots-pro.txt.
For the main domain I did to block robots-denyall.txt:
location /robots-denyall.txt {
return 404;
}
And for the subdomain which I don't want to be referenced:
location = /robots.txt {
alias /var/www/mywebsite/prod/robots-denyall.txt;
}

Nginx multiple domains shared resources

I have the following folders:
/web/domain1/
/web/domain2/
/web/shared/
I want domain1 and domain2 to share static files from /web/shared/ but I am having trouble creating the mapping in nginx.
domain1: /assets/ mapped to /web/shared/
domain2: /admin/assets/ mapped to /web/shared/
server{
server_name domain1;
root /web/domain1/;
location / {
rewrite /assets/(.*) /web/shared/$1;
}
}
This gives me 404 error.
Define a location for URIs that begin with /assets/ (see this document for details). Use the alias directive, as the root directive cannot be used in this case (see this document for details).
For example:
location /assets/ {
alias /web/shared/;
}
This works
location /assets/(.*) {
alias /web/shared/$1;
}

nginx deny not working with return

I am trying to deny all access on a certain server block.
server {
listen 8080;
server_name myserver;
root /srv/nkosl;
deny all;
location /mydistro {
return 302 https://www.ubuntu.com/;
}
location /files {
autoindex on;
}
}
This denies access on files location (and successfully returns 403 forbidden message).
However I can still access myserver:8080/mydistro and it redirects me to ubuntu page.
How can I deny the /mydistro location as well?
this answer describes why allow/deny does not work with return/rewrite.
one way to work around it is indeed using ifs, however ifs can be tricky in nginx in certain cases and allow/deny can also take a network mask...
Try to completely remove the /mydistro location. Or, at least, comment the line return 302 https://www.ubuntu.com/;.
To redirect selectively, try this:
location /mydistro {
if ($remote_addr != 127.0.0.1) {
rewrite ^ https://www.ubuntu.com;
}
}

Nginx redirect from one domain to another?

I'm on the process of migrating the same app but to a different domain.
For the old domain, I've the routes as:
http://app.example.com/app/users/sign_in?query=hello
I want it to be redirected to another domain omitting the app part as:
http://app.newexample.com/users/sign_in?query=hello
I tried with:
server {
...
location /app {
rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
}
...
}
I doesn't work. How to achieve this?
I had this issue about a year ago and spent a long time looking for solutions. I found and use this:
server {
listen 80;
server_name example.com app.example.com;
rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}
server {
listen 80;
server_name app.newexample.com;
# config for primary domain here
}
From this link. Credit to them.
Don't put scheme in the rewrite pattern:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
Brg.
I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine.
location ~ /app/(.*) {
return 301 $scheme://app.sparkon.com/$1;
}

Resources