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;
}
Related
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.
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;
...
}
I need to enter a bunch or rewrites in my conf file in Nginx. I am not very experienced so I copied what I found before, example.
location = /index.php/blog/blog/xxx/yyy/ {
return 301 /index.php/blog/xxx/yyy/;
}
However I was told that the best way is the following:
location ^~ /index.php/blog/blog/xxx/yyy/ {
rewrite ^/index.php/blog/xxx/yyy/;
}
Which one id the correct one?
The first one is more correct, both location as well as the return -wise, and it'll work faster.
FWIIW, your second snippet looks like it's missing a space in the rewrite after ^, and it's also less efficient, both location as well as rewrite-wise.
References:
http://nginx.org/r/location
http://nginx.org/r/return
http://nginx.org/r/rewrite
I have categories I'd like to rewrite.
for example:
example.com/videos?c=18
to:
example.com/category/name
I tried to do this using multiple examples, most had no effect and this example gave me only page not found to all /videos pages:
location /videos {
if ($args ~ "c=18") {
rewrite ^/videos(.*) http://$server_name/category/name$1 permanent;
}
}
Is this even doable purely via Nginx what I am trying to achieve?
As far as I got the question, currently your site has this url scheme, which you can't change:
http://example.com/videos?c=18
But you would like to present visitors with "pretty"-looking URLs like
http://example.com/category/name
That pretty URL does not really exist anywhere on the site, which is why you have to rewrite it, e.g. turn pretty virtual url into a real one that your scripts can process.
Once again, you rewrite from virtual to actual, not the other way round.
The following directive would turn /category/cars/ into /videos?c=cars
location /category {
rewrite ^/category/(.*)$ /videos?c=$1 last;
}
But your script won't understand /videos?c=cars url, it needs category ID to work. So in your case the pretty url should look like
http://example.com/category/18
which will be rewritten to
http://example.com/videos?c=18
"if" directive is not best solution, but in your case you can try "if" and $arg_name (argument name in the request line):
location /videos {
if ($arg_c = "18") {
rewrite ^/videos(.*) http://$server_name/category/cars? permanent;
}
if ($arg_c = "19") {
rewrite ^/videos(.*) http://$server_name/category/bikes? permanent;
}
# and so on
}
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;