Rewrite URL /blog/content/images/**/* to /content/images/**/* - nginx

My front-end is hitting /blog/content/foo/bar.jpg when looking for static assets.How can I make NGINX redirect those requests to /content/foo/bar.jpg instead?
At first I tried this:
location ~ ^/blog/content/ {
root /var/www/ghost/content/;
}
Apparently it didn't work – (btw, I'm testing each change in the .conf file with a sudo nginx -s reload + F5 in the browser.. is there a better way to test/debug NGINX behavior (and actually understand what's going on instead of this "worked / didn't work" feedback I get with each F5?)
Then, I tried this one I found in a cheatsheet – at the server level:
rewrite ^/blog/content/(.*)$ /$1 last;
Again, without luck. What bothers me is that I can't even see what the line above is doing and why it isn't working.
Someone, please, get me out of this "google for a solution -> try something that looks promising --> hit F5 hoping it works (it doesn't) -> google for a solution" loop.

The solution I found was to rewrite with:
rewrite ^/blog/(.*)$ /$1 last;
But it isn't ideal. I'd prefer to rewrite only when the URI contains "/blog/content/"..

Related

How to use multiple nginx rewrite flags

I have a config like this in my nginx site configuration that is throwing a syntax error:
rewrite ^/favicon\.ico$ /static/ico/favicon.ico last permanent;
The idea was to make this the last rewrite rule that's processed, and to also configure it to return a permanent redirect.
Is there a trick for this?
(Answering my own question...)
Looking at the docs, it appears the syntax takes the following form:
rewrite regex replacement [flag];
Alas, just one flag is allowed at a time. I've done some minor testing though, and it seems that when you use the permanent flag, you don't have to use the last flag.
So the result becomes:
rewrite ^/favicon\.ico$ /static/ico/favicon.ico permanent;

Clean URI/URL Nginx Rewrite for multiple pages & multiple variables

I am pretty much a moron with regex and I'm just getting started with a cloud-based Nginx server, which is a big change from administering my in-my-closet Apache server.
I'm trying to do rewrites to get clean URLs like this:
www.domain.com/folder/red/abc ---> www.domain.com/folder/red.cfm?query=abc
www.domain.com/folder/blue/ab/cd/ef ---> www.domain.com/folder/blue.cfm?name=ab&city=cd&state=ef
www.domain.com/folder/blue/ab ---> www.domain.com/folder/blue.cfm?name=ab
I'm basically trying to get rewrites of items after the "folder" subfolder to rewrite to static .cfm pages. Some of those .cfm pages have zero, one, two or three URL variables; the number of variables is not fixed or consistent.
I have been reading A LOT about rewrites and try_files, and I have tried, oh, a couple hundred different variations of rewrites, and I just can't seem to find the solution.
For example, I've tried:
location /folder/blue {
rewrite ^/folder/(.*)?$ /folder/blue.cfm?name=$1 last;
}
And this just gets me absolutely nowhere. I would post my entire conf file, but it is long due to other stuff that was added in by the default server setup.
I would love to make this string as simple as is humanly possible, but I really need help with this. I appreciate it!
For future reference, it would help having a spec.
It seems like this is the only spec you've provided:
www.domain.com/folder/red/abc ---> www.domain.com/folder/red.cfm?query=abc
www.domain.com/folder/blue/ab/cd/ef ---> www.domain.com/folder/blue.cfm?name=ab&city=cd&state=ef
www.domain.com/folder/blue/ab ---> www.domain.com/folder/blue.cfm?name=ab
The following solution should 100% satisfy the above "spec":
location /folder/red/ {
rewrite ^(/folder/\w+)/(\w*)$ $1.cfm?query=$2 last;
return 410;
}
location /folder/blue/ {
rewrite ^(/folder/\w+)/(\w*)$ $1.cfm?query=$2 last;
rewrite ^(/folder/blue)/(\w*)/(\w*)/(\w*)$ $1.cfm?query=$2&city=$3&state=$4 last;
return 410;
}

Nginx rewrite '*/filename/playlist.m3u8' to '*/filename.mp4/index.m3u8' as redirect

I'm trying to migrate over from Wowza and set-up VOD on Nginx on Debian through the help of Kaltura's nginx-vod-module, everything is working fine, apart from the existing VOD archive still req. to be accessible through the old URLs... so a rewrite rule is in order, but since it's a bad practice to do this, it should be a nginx redirect 'return 301 '.
The previously used Wowza URL format for files in the VOD archive was:
http://<server-address>:<rtmp-port>/vod/<file-name>/playlist.m3u8
The new URL format has to be:
http://<server-address>/vod/<file-name><file-extension>/index.m3u8
While I don't know much about rewrite rules, for now I've managed to put together a temp solution:
location ~ playlist.m3u8$ {
rewrite ^(.*)/playlist.m3u8$ $1.mp4/index.m3u8;
}
But I don't know how would that translate into a working Redirect rule ('Return 301 ') or if it's possible to have one that would work universally without specifying each and every filename in the VOD folder.
While I'm at it, it kinda would be nice to have a simpler/shorter URL to access the newly added files.
http://<server-address>/vod/<file-name>.m3u8
The rewrite rule that should do that I think would be a combination of two rewrite rules, something like:
location ~ playlist.m3u8$ {
rewrite ^(.*)/playlist.m3u8$ $1.mp4/index.m3u8;
}
location ~ index.m3u8$ {
rewrite ^(.*).m3u8$ $1.mp4/index.m3u8;
}
But I'm worried it would target the URLs that have been rewritten and result in a HTTP error 404... suggestions to avoid that?
Try this:
rewrite ^http://(.*?):\d+/vod/(.*?)/.*?\.(.*)$ http://$1/vod/$2.mp4/index.$3
Regex101

NGINX rewrite issue

I have NGINX + PHP5-fpm and slim for routing, my website wont work with out this rewrite : try_files $uri /index.php; , now everything is fine and all site is ok except some API, for example http://example.com/webservice/android.php/get_video_info
It doesn't work and say it :No input file specified.
Why ? i should mention that http://example.com/webservice/android.php is exist on server but "http://example.com/webservice/android.php/get_video_info" is not exist and its just a post action on this android.php
PS : if you need more information i will provide for you
It was about my app.config, after I checked the NGINX log i found that the issue it's make path duplicated like /var/www//var/www/ after i change my config and remove second one, issue is solved

NginX: rewrite backslashes to Forward-slashes

I want to write NginX rewrite rule to convert all Backslashes to forward-slashes.
Something exactly like: Using .htaccess to replace backslash in URL with forward-slash
however, I am working in NginX while above link refers to Apache.
I migrated my application from Windows IIS to Linux Tomcat and hence I need to get this done.
My URL has multiple Backslashes which gets resolved fine in IE and Chrome but Firefox is resolving them to its Unicode %5C and hence I need to rewrite.
My sample URL in WIndows/IIS: https://doman.com/company/.\images\company\companylogo.png
When I moved stuff to Linux/Tomcat, above URL works in Chrome and IE, but Firefox is converting above backslashes to %5C. So Firefox is trying to resolve: https://doman.com/company/.%5Cimages%5Ccompany%5Ccompanylogo.png
And needless to say, Firefox fails to load the image.
Here is what I tried so far in my nginX configuration (Once statement at a time)):
rewrite \ / permanent;
rewrite \\ \/ permanent; # with escaping thinking it might help
rewrite (.*)\(.*)\(.*)\(.*) $1/$2/$3/$4 permanent;
rewrite (.*)\(.*)\(.*)\(.*) $1\/$2\/$3\/$4 permanent;
Howcer , none seem to work and last 2 statements are throwing NginX configuration Error.
Any pointers would be of great help. There are thousands such URLs and I cannot imagine of converting all of them into Forward-Slash'd style.
Finally, I figured it out myself.
First off, in NginX, a backslash needs to be escaped twice. Nginx Uses Lua launguage module extensively for parsing request. So, NginX config parser as well as Lua module will strip off the escaping backslashes. Hence need to escape it twice.
This is wrong: \\
This is Correct: \\\
This part bugged me for 2 days as I was following typical PCRE standards for escaping characters and NginX would throw regex error at me.
More about this behaviour is explained here: http://wiki.nginx.org/HttpLuaModule#Special_PCRE_Sequences
Now, over to my question:
Source URL: https://domain.com/company/.\images\company\companylogo.png
Redirected URL: https://domain.com/company/./images/company/companylogo.png
I wanted to convert all \ to / in above URL. So, for this purpose, below rewrite rule is needed:
rewrite ^/(.+)\\\(.+)\\\(.+)\\\(.+)$ /$1/$2/$3/$4 redirect;
Above rule will do a single redirect (HTTP Code 302) and will get all the three \ converted to /.
However, If you have varying number of \ in the URL, then above rule may(not) work. Hence in that case, use below rule to convert ALL \ to /
rewrite ^/(.+)\\\(.+)$ /$1/$2 redirect;
Please note that, with above rule in place multiple redirects (HTTP Code 302) will happen causing some latency. The number of redirects will be equal to number of \ in the source URL. This would work on my URL too. But then it would have done the redirect for 3 times (as I have 3 backslashes in my URL). So, I am good with the 1st rule I mentioned above.
Nonetheless, I had a great learning all along and I understand NginX better now.
Here are few (out of hundreds of) links which helped solving this issue:
http://wiki.nginx.org/HttpLuaModule
https://blog.engineyard.com/2011/useful-rewrites-for-nginx
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
http://blog.rackcorp.com/2010/05/nginx-location-and-rewrite-configuration-made-easy/
http://www.cyberciti.biz/faq/unix-linux-bsd-nginx-redirect-url-http-301-status-code/

Resources