Multiple nginx locations - nginx-config

I have multiple nginx locations via the same route
location ~^/(v1) {
proxy_pass http://localhost:8000/;
}
location = /v1/smth {
}
Does the proxy_pass would be applied for /v1/smth?

The nginx http-core-module documentation describes the order of precedence when evaluating a match. Specifically:
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.
In your example, /v1/smth therefore matches exactly with the second (empty) location block, and the proxy_pass is therefore not applied.

Related

How can the $request_method parameter be changed in nginx for a proxy_pass directive?

I need to alter the nginx $request_method variable under certain conditions when I hand off the request through a proxy_pass directive.
I was thinking of using something like the map directive:
map $request_method $request_method {
default $request_method;
DELETE POST;
PUT POST;
}
But, there are 2 problems:
map directives are only allowed in the top level http block and can't be changed inside of a location directive.
this also gives me a duplicate "request_method" variable error from nginx.
How can I alter the $request_method for a proxy_pass?
There is a directive proxy_method.
You will need another variable declared via map (yes, at server level):
map $request_method $my_proxy_method {
default $request_method;
DELETE POST;
PUT POST;
}
And then in your location:
proxy_method $my_proxy_method;
Note that Nginx variables are evaluated lazily, so if you have lots of other locations, my_proxy_method will be evaluted only for this one.

nginx redirect all images to new domain

I had links to my images something like it:
mydomen.com/upload/iblock/f88/f887c7bc3229b93a0a0f7c248d3aefb5.jpg
regex is something like it:
mydomen.com/upload/(catalog|iblock|medialibrary|partners|resize_cache|rk|sale|uf)/*/.(jpg|png|jpeg)
and now i thansfered all my images to storage and now my images have these urls:
images.mydomen.com/iblock/f88/f887c7bc3229b93a0a0f7c248d3aefb5.jpg
so, from mydomen.com i move to images.mydomen.com and removed /upload/
can anyone helps me to redirect all requests images to new domain via nginx location??
You will need to remove the /upload/ prefix from the original URI using a regular expression and capturing the remainder. You can use either the rewrite or location directives.
For example:
rewrite ^/upload((catalog|iblock|medialibrary|partners|resize_cache|rk|sale|uf)/.*\.(jpg|png|jpeg))$ https://images.example.com$1 permanent;
Or using a location block:
location ~* ^/upload((catalog|iblock|medialibrary|partners|resize_cache|rk|sale|uf)/.*\.(jpg|png|jpeg))$ {
return 301 https://images.example.com$1;
}
The regular expression location blocks are evaluated in order, so its position within the configuration file may be significant. See this document for details.

Can not use 301 redirect in NGINX

I have a web site which is translated to 18 languages. Some languages are the same (Brazilian is Portuguese). So I want to redirect from br to pt to avoid odd content, from /some_domain/br/... -> /some_domain/pt/...
I can write single redirect from one domain to another. Something like this:
location = /user/unique {
return 301 http://www.usgreencardoffice.com/blog/the-american-dream;
}
I want to achieve the following:
domain.com/br/something -> domain.com/pt/something
But for the languages redirection, I have no idea. How can I achieve this?
If the language code is at the beginning of the URI, a prefix location will be an efficient solution:
location ^~ /br/ {
rewrite ^/br(.*)$ /pt$1 permanent;
}
The ^~ modifier makes this prefix location take precedence over regex locations at the same level. If you change permanent to last, the rewrite becomes internal and thus invisible to the user.
See this and this for details.

Nginx redirect URL with specific query parameter

Nginx, I am trying to permanently redirect the URLs with a device GET parameter (http://www.example.org/page?device=desktop) to the relative URL without this parameter (http://www.example.org/page).
I did this, but it doesn't work.
location {
rewrite ^(.*)\?device=desktop $1 permanent;
}
Each query parameter is exposed as a variable prefixed with $arg_ in the configuration file. For example, device would become $arg_device. Using this you can make the comparison check within your location block, for example:
location / {
if ($arg_device = desktop) {
return 301 $uri;
}
}

how to do nginx rewrites from a url with get parameters

This is my first ever StackOverflow question so please bear with me.
Nginx serves many different sites for us and we have a lot of redirects from migrating clients from different vendors and such. We have set up an /includes directory that houses redirect files for each domain that we migrate over. Occasionally, we will need to write redirects from a url that contains get parameters:
http://example.com/content/default.aspx?NewsId=28
To do this, we have been doing this in an nginx /includes file called example.com-redirects
location ^~ /content/default.aspx {
if ($args ~ "NewsId=28") { rewrite ^ http://example.com/news? permanent; break; }
# add more statements like the one above
}
That has worked just fine for us thus far. Unfortunately, we need to do the same thing but for a different domain that could have the same get parameters. And of course nginx doesn't allow for duplicate locations.
location ^~ /content/default.aspx {
if ($args ~ "NewsId=28") { rewrite ^ http://differentexample.org/news? permanent; break; }
}
I've tried a couple of different solutions all giving me syntax errors. No one at my company is an nginx expert anymore so I could really use some help solving this. I have added an if ($host ~ "example.com") within the location block and that gave me an error. And I've tried adding the location block within the if ($host ~ "example.com") block. Both times nginx told me that I can't put that there.
I usually find my answer in the vast knowledge base that is the internet but seem to be striking out on a solution for this and we're running out of time before we launch this client.
Instead of using if, use a variable. In the virtual host config you set this variable. In the include with the location you use it:
server {
...
set $redirect_host "example.org";
include /includes/news28.conf;
...
}
# include part
location /foo {
if ($args ~ "NewsId=28") { rewrite $scheme://$redirect_host/news? permanent; }
}
Of course you need to set that variable also in the server config of the host already using this include. Hope this helps.