nginx substitude file sitemap.xml on subdomain - nginx

I have file sitemap.xml in site public directory. When I use subdomain 'm.', it uses same public directory as general site. I need to return file '/mobile-sitemap.xml' from url '/sitemap.xml', when I'm on subdomain 'm.'.
What nginx modules and rules could help me to do this?
My first step was like this:
if ($host ~* m\.(.*)) {
#if url = /sitemap.xml, give back file /mobile-sitemap.xml as /sitemap.xml
}
Or maybe it's ok to google, if i just send 301 redirect to another sitemap file?

You could create a separate server block for your mobile subdomain, in which case the if statement would not be necessary. See this caution on the use of if.
However, to implement this in a single server block, use a location and a rewrite:
location = /sitemap.xml {
if ($host ~* m\.) {
rewrite ^ /mobile-sitemap.xml last;
}
}
See this and this for more.

Related

Nginx: Rewrite URL to hash

I have an old site which has been upgraded to an SPA frontend, so now the URLs to access certain records use the hash, so the URLs changed from like example.com/aq/12345 => example.com/#/aq/12345
This is already working fine, but I also wanted to set up a redirect so that anyone who had the old URLs would be redirected to the new URLs. I tried setting up the rewrite like this:
location / {
root /var/www/frontend;
autoindex on;
rewrite ^/aq/(.*)/$ /#/aq/$1/ last;
}
... but when I did this, nginx simply started looking for a file named /var/www/frontend/#/aq/[whatever]/index.html, instead of simply redirecting to /var/www/frontend/index.html with the hash in the URL. What is the proper way to configure this, or is not possible to do this at all?
The proper way is to use the HTTP 301 status code:
location /aq {
root /var/www/frontend;
autoindex on;
return 301 https://example.com/#$request_uri;
}

Nginx rewrite rule for changing domain name in image urls

I have Wordpress website which is live. In this website are a lot of posts with uploads over 200GB. I want to create in localhost development environment which I have done, but I have problems with posts images, because I don't want to download all of them and put in my localhost. So I am wondering if there is any Nginx rewrite rule which will rewrite only domain name in my image urls.
I need to rewrite this url:
https://example.local/wp-content/uploads/2020/10/10/very-nice-picture.png
To this one:
https://example.com/wp-content/uploads/2020/10/10/very-nice-picture.png
This is possible with Nginx or there is other better solution?
Here are your options:
Use a redirection:
location /wp-content/uploads/ {
return 301 http://example.com$request_uri; # or use 302 to prevent redirection caching
}
Use transparent proxying:
location /wp-content/uploads/ {
proxy_pass http://example.com;
}
You can also use remote files only in case they are missing on the local development server:
location /wp-content/uploads/ {
try_files $uri #remote;
}
location #remote {
# redirect or proxy the request as shown above
}

Nginx rewrite requests to subdomain.tld/files/* to external domain

I'd like to rewrite all requests to Nginx matching http://*.examle.tld/files/* to http://$1.otherdomain.tld/files/?file=$2. I'd also like to rewrite the same request without the subdomain i.e. http://example.tld/files/* to http://otherdomain.tld/files/?file=$1
The reason for this is to use production files from local development without having to sync folders.
This is what I've got so far, however without success:
location / {
...
rewrite ^http://(\w+)\.(.*)/files/(.*) http://$1.otherdomain.tld/inc/reader.php?file=$3;
rewrite ^.*/files/(.*) http://$1.otherdomain.tld/inc/reader.php?file=$1;
...
}
Thank you for any assistance.
You cannot use the server name as part of the rewrite directive's regex. If you have a server block with a wild card server_name as described here, you can use a named capture for use later within the block.
For example:
server {
server_name ~^(?<sub>\w+\.)?example\.tld$;
location /files/ {
rewrite ^/files(.*)$ http://${sub}otherdomain.tld/files/?file=$1 permanent;
}
}
See this document for details.

nginx url rewriting between 2 domain names

Got an issue with URL rewrite under nginx. Actually I have a main domain name dns1.com which is pointing a webserver with a WordPress installation (permalink = /%category%/%postname%/). I've created this page dns1.com/forum/forumname1.
I also have a domain name forumname1.com and I want this one to be pointed to the content of dns1.com/forum/forumname1. The issue is that I don't know how to display the result of dns1.com/forum/forumname1 with only forumname1.com in the bar address :( Actually it works when I go to forumname1.com/forum/forumname1 but this URL is pretty redundant :/ I should make this parameter "/forum/forumname1" hidden but how?
Here is my nginx serverblocks :
server{
server_name dns1.com;
root /var/www/dns1.com;
location = /forum/forumname1 {
rewrite ^/(.*)$ http://forumname1.com permanent;
}
}
server{
server_name forumname1.com;
root /var/www/dns1.com;
location = / {
### Rewriting rule HERE but which one?
}
}
Thank you :)

nginx rewrite rule to prepend something to a url

I want to rewrite all my JPG file URLs using mobify CDN. For that, all I have to do is prepend the URL
https://ir0.mobify.com/jpg50/ to my existing URL. So for example, if I have the URL
http://xxx.yyy.com/wp-content/uploads/2290/07/abc.png then the user has to be redirected to
https://ir0.mobify.com/jpg50/http://xxx.yyy.com/wp-content/uploads/2290/07/abc.jpg
I wrote the following code in my nginx config. I tested the regexs at regexlib and they seem to be fine.Still do not understand what is wrong with my config. Please help.
location ~ \.jpg$
{
rewrite ^http://(.*).jpg$ https://ir0.mobify.com/jpg50/$uri last;
}
Try this ...
location ~ \.jpg$ {
return 301 https://ir0.mobify.com/jpg50$request_uri;
}

Resources