convert url segment to get variable keeping others get variables - nginx

I can't fix that I have a url
domain.com/api/class/access/1/index.php?username=usuariodemo&name=sala
where /1/ is a ID so, I need convert /1/ to get variables like
domain.com/api/class/access/index.php?id=1&username=usuariodemo&name=sala
That is pure PHP not framework my index.php is located on folder api/class/access/index.php
I have this I try others but this explain me better waht I want do
location /api/class/access/(.*)/* {
try_files $uri $uri/ /api/class/access/index.php?id=$1&$query_string;
}
That show me:
No input file specified.
Thanks!
Regards!

The location statement in your question is invalid and probably unnecessary.
If you want to rewrite a URI ending with .php it may be easiest to place a rewrite statement inside the block that processes all .php URIs, usually: location ~ \.php$ { ... }.
For example:
location ~ \.php$ {
rewrite ^(/api/class/access/)(\d+)/(index.php)$ $1$3?id=$2 last;
...
}

Related

Nginx Configuration for Dynamic URL Segments as Arguments for Parent Segment Index

I am trying to set a location in the configuration that allows me to do something like https://example.com/car/<vin> which would not go to a <vin> application or directory but to /car/index.html. From there, I would read the URL or pass <vin> to /car/index.html.
I have tried various regex location blocks, like the one below, but they all result in a 404 when accessing /car/<vin>.
location ~ ^/car/(.*)$ {
root $document_root/car/
index index.html;
}
What would be an appropriate location block?
Do you want to use a regular expression? The prefix location would also work as it matches any URI that begins with /car/. See this document for details.
For example:
location ^~ /car/ {
try_files /car/index.html =404;
}
Using $document_root in the root statement may not work, and the index directive only works with URIs that end with a /. The try_files statement is probably the simplest solution. See this document for details.

How to rewrite url for url with multiple query parameter?

Server: Nginx
Application: PHP
URL in question is
/p/all_articles/user/ABC
/p/all_articles/user/ABC/page/123
which is
/index.php?p=all_articles&user=ABC
/index.php?p=all_articles&user=ABC&page=123
ABC = this could be a-zA-Z0-9 as it's username
123 = this could be page numbers 0-9
Tried many combinations and options, the following got me much closer to expected result, but it is not perfect.
location /p/all_articles {
rewrite ^/p/all_articles/user/(.*)?$ /index.php?p=all_articles&user=$1 last;
rewrite ^/p/all_articles/user/(.*)/page(?:/([0-9]+))?$ /index.php?p=all_articles&user=$1&page=$2 last;
try_files $uri $uri/ /index.php;
}
the above rewrite location block results only when one of them is quoted other one would work, but not together.
Similarly
/search/QUERY
/search/QUERY/page/123
which is
/index.php?search=QUERY
/index.php?search=QUERY&page=123
QUERY = anything a-zA-Z0-9 and space
123 = this could be page numbers 0-9
like the one for all_articles, the closest I was able to get is
location /search {
rewrite ^/search /index.php?search last;
rewrite ^/search/(.*)/page(?:/([0-9]+))?$ /index.php?search=$1&page=$2 last;
try_files $uri $uri/ /index.php;
}
But, this works to get either one of the clean URL when the other rewrite is quoted, but not together.
I appreciate if anyone give any answers to solve this issue.
The rewrite statements are evaluated in order so the more specific regular expression needs to be placed before a less specific regular expression.
With the two regular expressions:
^/search
^/search/(.*)/page(?:/([0-9]+))?$
The first one will match anything that also matches the second, so the second regular expression will never be evaluated.
The simple solution is to reverse the order of the rewrite statements in both of your location blocks.

How can I check if file exists in nginx? otherwise run a rewrite rule

good day everyone,
In order to resize images on the fly, i have decided to use a service like Kraken or imgix.
My images will be displayed as so: site.com/img/path-to-img-s250x250.jpg
and what i would like to achieve is: if the image path-to-img-s250x250.jpg exists, then i'll display it, and if not then run an nginx rewrite rule to resize the original image and save it.
Is this something possible or should i do the checks with PHP?
Also do you have any better ideas on how to better deal with resizing images on the fly?
Thanks a lot
location /img/ {
try_files $uri #getImg #resize;
}
location #getImg{
rewrite "img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+).([0-9]{5}).(jpg|jpeg|png|gif|ico)$" /img/$1/$2-$3x$4.$6 break;
}
location #resize{
rewrite "img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+).([0-9]{5}).(jpg|jpeg|png|gif|ico)$" $scheme://$host/image?path=$1&file=$2&ext=$6&w=$3&h=$4 break;
}
and here is the image link: site.com/img/prods/1002/filename-200x200.12345.jpg
12345 is for the versioning
You can use try_files to test for the existence of one or more files. See this document for details.
Use a regular expression location to capture the elements of the URI. Regular expression locations are evaluated in order, so there position within the configuration file may be significant. See this document for details.
If the final parameter of a try_files statement is a URI. it will generate an internal redirect.
For example:
location ~ "^/img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+)\.([0-9]{5})\.(jpg|jpeg|png|gif|ico)$" {
try_files $uri /img/$1/$2-$3x$4.$6 /image?path=$1&file=$2&ext=$6&w=$3&h=$4;
}
The above will not work on its own, as many PHP scripts use the originally requested URI (REQUEST_URI) to determine the route.
You can create a custom location to explicitly set the REQUEST_URI and SCRIPT_FILENAME parameters.
For example:
location = /image {
include fastcgi_param;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param REQUEST_URI $uri$is_args$args;
}
Base this block on your existing code (the above is just a guess). We include the global configuration files first, then override SCRIPT_FILENAME and REQUEST_URI. I have assumed that index.php is handling the /image route.

Display default image when missing file in nginx

We have various pictures associated with items on our site but not for all of them. To this end we would like to display the contents of 'blank.jpg' if a given image is not found.
To this end we have the nginx config like this
location /static/images/ {
root /blah_blah_blah/pictures/;
error_page 404 blank.jpg;
break;
}
The problems with this is that this takes /static/images/fred.jpg and redirects to /static/images/blank.jpg when what we want is to simply display the contents of blank.jpg whilst keeping the /static/images/fred.jpg url
It also seems to go into a redirect loop occasionally.
Not sure if you still need it 10 month later but this looks like what you were looking for: https://serverfault.com/questions/562269/redirect-missing-images-in-a-specific-directory
I'd like to share a little bit more sophisticated example. It's a way how you can match only selected extensions or name patterns:
location ~ ^/media/(.*)/preview(.*).(png|mp4)$ {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/media/(.*)/preview(.*).(png|mp4)$ $scheme://$host/media/no_preview/preview$2.$3 redirect;
}

Nginx same config for multiple paths

I'd like to configure several paths within a site (such as /foo/ and /bar/) in the same way. To avoid copy-pasting, I figured I should use a single location block, but the only way I found for doing that is to use a regex, such as:
location ~ ^/(foo|bar)/ {
...
}
Is this the best way or is there a better alternative?
This would work, but I believe it works slower, because it involves a regex engine, another alternative you could create the config you want in a separate file and include them in each location so that it would be written once and could be edited all together, like this
location /foo {
include foo.conf;
}
location /bar {
include foo.conf;
}
Inside foo.conf you could write any config that's in a location scope
heres a random sample snippet:
root /foo/bar;
try_files $uri /index.html;

Resources