Nginx location expressions - nginx

I have problem when im using ~* for case insensitive expression
this will download my index.php instead of showing this file.
maybe i do not have a correct understanding of this expression. can you help me ?

I found my problem
When I want to use an insensitive expression, I should create
another location block to route the query to proper location in webserver!
Thank you for the response !

Related

Nginx image not found fallback

My site have many images /image/test/manyfile.jpg and now day I have /image/test/manyfile_large.jpg, but not all .jpg have _large.jpg.
So my idea is use Nginx to check manyfile_large.jpg file, if not exist then rewrite or redirect to manyfile.jpg
Can anyone give an example config?
Thanks a lot! :)
You can use try_files to test for the existence of one or more files. See this document for details.
First you need to capture the components of the URI so that you can construct the two filename variants to try.
I am not sure if the examples in your question and comment are URIs or pathnames. In the following example, the URI is /image/foo/bar.jpg and the corresponding files are located at /path/to/image/foo/bar.jpg and (optional) /path/to/image/foo/bar_large.jpg.
For example:
location ~ ^(/image/.+)\.(jpg|png|svg)$ {
root /path/to;
try_files $1_large.$2 $1.$2 =404;
}
The block will return the large variant if it exists, then the regular variant if it exists, then a 404 status if neither exist.
The order of regular expression location blocks is significant, as the first block with a matching regular expression is used to process the request. See this document for details.

Having issues with regex matching wrong URLs in NGINX rewrite

Hi I have urls that look like this
http://dansawesomesite.com/123/articlename
I have the following rewrite rule in nginx
location ~* /(\d+)/([\+\w-\ ]+)/?$ {
try_files $uri /wpcontentredir.php?slug=$1;
}
This matches the above URL however the issue comes about when I have the following URL's
http://dansawesomesite.com/posts/630325/like
(as well as a number of similar)
These also end up getting matched which is correct based on my regex, but will mess things up as I dont want these urls parsed through that try_files, I just want them to pass as is.
Just wondering if anyone can help me with only matching the top first URL?
CHeers
Dan
Try to add "^" to the beginning of regexp
location ~* ^/(\d+)/([+\w-\ ]+)/?$
So it will match only if first part of URI contains digits and not "posts" or something

Rewrite rule with nginx replace get variable with slach

I've been reading everything i could find for two days now but nothing seems to work
I have an url like this
http://www.mysite.com/auction.php?category=23140
and want make a rewrite rule so it will appears like that
http://www.mysite.com/auction/category/23140.php
I do that only in order to build seo friendly url but so far nothing have worked.
rewrite ^/auction.php?category=(.*)$ /auction/category/$1 last;
It look so simple i realy don't know why it doesn't work.
Should i put this line in the server block or the location block ? Is the regex wrong ?
Any help will be greatly appreciated
At first, pls remember rewrite in nginx does not match query string, only uri.
For example, in this url: "/auction.php?category=23140", uri is /auction.php and query string is category=23140
So in your case, the correct rule should be:
if ( $request_uri ~ "^/auction\.php\?category=[0-9]+(.*)?$" ) {
rewrite "^.*$" /auction/category/$arg_category break;
}
Above is just a example, you can change it according to your case. And also you can find the usage of $request_uri and $arg_PARAMETER from the nginx manual.
You're doing it backwards, try this.
rewrite ^/auction/([^/]*)/([0-9]*) /auction.php?$1=$2
piece of advice, if this is a custom application you're writing without a framework, you could find a routing module that you could use and it would help you do every thing easily.

How do I escape question mark with nginx rewrite?

Im bussy migrating my websites to nginx and I have a lot of rewrite rules that need to be converted, the only problem I'm experiencing is when I try to do something like this:
rewrite ^/media?(.*)$ /in.php?id=$1 last;
This causes php to read $1 ad the key of $_GET instead of it's value. Is there any way to change this behaviour without resorting to difficult location based methods? The reason I'm asking is because I have multiple query string based rewrites that need to be addressed.
Everything what comes after ? in nginx is known as variable $query_string, so if you want to translate whole query string from location rule ^/media?(.*)$, because it looks like it's what you want, try to use this:
rewrite ^/media?(.*)$ /in.php?id=$query_string last;
Is it what are you looking for?

Rewrite Rule in Nginx

I conisder moving to Nginx but I want you to ask if is possible to rewrite urls into that schema, and if you could help me a bit:
A url like http://example.com/username into profile.php?u=username. But then, will I have problems in accessing other pages like e.g. home.php
A url like http://example.php/questions/102039/that-question into questions.php?quid=102039
Thank you very much!
Yes, it is possible to rewrite URLs with Nginx.
Your first example can be handled easily by wrapping the rewrite with a block which checks if a file (home.php in your example) exists; if it doesn't, then it tries the redirect into the profile.php request. (See the try_files syntax for this.)
Your second example is just as simple:
Rewrite ^/questions/(\d+) /questions.php?quid=$1
(Because the matching expression is not anchored at the end, it should accept any string after the digits, but I haven't checked that so I'd recommend you test it.)

Resources