Nginx rewtire question mark - nginx

I use Windows 7 for developing web sites. Now I have a problem on rewrite an url. Try to change a question mark to an underscore, but nothings seems to work.
location /site/ {
rewrite "^skript.php([?]{1})(.*)$" skript.php_$2;
}
Url should be "skript.php_$args.
Solution needed.

The ? is the delimiter for the query string. The rewrite and location directives use a normalised URI which already has the query string removed and placed into $args.
There are a number of ways to append _$args to the URI, depending on what you are trying to achieve. For example:
location = /foo.php {
rewrite ^ $uri_$args?;
}
Or:
location = /foo.php {
return 301 $uri_$args;
}
Or:
rewrite ^/foo.php$ $uri_$args?;
See this and this for details.

Related

URL rewriting QUERY_STRING NGINX?

I want to download any file from the host whose last address had the x-oss-process parameter.
i.php?path=$1&x-oss-process=%1
Refer that $1 is the address of the referenced file and %1 is the value of the x-oss-process parameter
Like the link below
https://dl.example.com/img/all/235868.jpg?x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
After referring this address to the following address
i.php?path=img/all/235868.jpg&x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
Now how do I write the nginx code to do this? Thank you for your help.
If the query argument name didn't contain any dash characters, like x_oss_process instead of x-oss-process, the solution would be much more simple. In that case we could check $arg_x_oss_process variable (see $arg_name variable description):
if ($arg_x_oss_process) {
rewrite ^ /i.php?path=$uri;
}
The first parameter ^ is a regex that matches any string, $uri is a normalized request URI (without query arguments). All the query arguments would be added to the rewrited URI (see the rewrite directive description):
If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended, for example:
rewrite ^/users/(.*)$ /show?user=$1? last;
However since your query argument name contain dashes, the solution will be more complex (see this SO question for additional details). We would need to get the x-oss-process query argument value from $args variable using the map directive which shoud be placed outside the server block:
map $args $x_oss_process {
~(?:^|&)x-oss-process=([^&]*) $1;
}
server {
...
if ($x_oss_process) {
rewrite ^ /i.php?path=$uri;
}
...
}
This rewrite rule will rewrite
/img/all/235868.jpg?x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
request to the
/i.php?path=/img/all/235868.jpg&x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
This is slighly different from what you've been asked for (note the slash before img/all/235868.jpg URI part). I'd rather change i.php script to correctly process this, but if you want to strip this slash with nginx itself, you can use second map block:
map $args $x_oss_process {
~(?:^|&)x-oss-process=([^&]*) $1;
}
map $uri $truncated_uri {
~/(.*) $1;
}
server {
...
if ($x_oss_process) {
rewrite ^ /i.php?path=$truncated_uri;
}
...
}

how to make nginx location match path both with slash and without slash?

currently i have this location in my nginx file, it does not work with http://mydomain/ab/cd. How can i make the browser to go to the same page when user type both http://mydomain/ab/cd and http://mydomain/ab/cd/?
location /ab/cd/ {
}
The fastest, in terms of performance, is simply two exact locations:
location = /ab/cd {
...
}
location = /ab/cd/ {
...
}
You can try
location ~* ^/ab/cd(|\/) {...}
It is a prefix matching regex that checks if it has trailing slash or not.

Read query parameter from url to do a nginx redirect

I have two source URL format and want to redirect both url based on
color variant to the destination link with the parameter appended.
Scenario1: www.example.com/pages?abc=123&color=white which should redirect to www.example.com/variant1?abc=123&color=white
Scenario2: www.example.com/pages?abc=456&color=red which should redirect to www.example.com/variant2?abc=456&color=red
I have tried with below , it works for one but not for both as its specific. Not able find the solution for both cases, as else doesnt work
location = /pages {
if ($args ~* "&color=white”) {
rewrite ^.*$ /variant1 redirect;
} }
While it is possible to do this in Nginx, it'll defeat some of the advantages that Nginx provides. If you can, it's better to do it within your application code or Lua.
If you choose to go forward with if statements, you'll want to match against individual $arg_* variables rather than $args. In particular, you'll want to use $arg_color, which will contains the color querystring value.
location = /pages {
if ($arg_color = "white") { return 301 /variant1$is_args$args; }
if ($arg_color = "red") { return 301 /variant2$is_args$args; }
# If we get to this point, there was no match. You have to keep
# this very simple and can only use directives documented as being
# compatible with `if`, or you'll get all sorts of crazy bugs and
# crashes.
return 404.
}

Nginx rewrite: how to change URL from dynamic to static

I'm configuring nginx as reverse proxy.
I need to change (rewrite?) the URLs, example: when the request (to nginx Reverse Proxy) is "http://example.com/test/?username=test1;password=passwdtest1" it will must "modified" to the main server as "http://example.com/test/?username=production;password=passwdproduction1".
Consider that in the original request the fields "username=test1;password=passwdtest1" are not always the same (they changes), instead the "modified" to the main server are always the same.
Others example to be more clear:
"/test/?username=test1;password=passwdtest1" -> "/test/?username=production;password=passwdproduction1"
"/test/?username=test1876;password=somepasswd" -> "/test/?username=production;password=passwdproduction1"
"/test/?username=somevalues;password=somepasswdvalue" -> "/test/?username=production;password=passwdproduction1"
So, independently to what are the values of "?username=somevalues;password=somepasswdvalue" it should always become "?username=production;password=passwdproduction1".
Thanks for your help!
A little late on the answer but this should work for you:
location ~* /test/? {
if ($arg_username ~ "^$|\s+") { return 404; }
if ($arg_password ~ "^$|\s+") { return 404; }
rewrite ^ /test?username=production&password=passwdproduction1? permanent;
}
The code above checks if it is within the example.com/test path. If it is it will check if the user name or the password variable are present and not empty in the query string. In case if any isn't present or is empty it will return a 404 else it will redirect you to the preferred url.
By the way, instead of the semicolon in your example urls I would use an ampersand (&).

Rewrite Rule in Nginx to Block a Specific URL

I would like to block a specific URL from being access and return a 444 Error.
Example:
if ( $request_uri ~ https://subdomain.domain.com/abc/xyzdirector/login.do ) {
return 444;
}
Now this works fine, the issue is if I type the following URL in my browser and change ANY of the capitalization in the sub-directories, it does not work:
Example:
https://subdomain.domain.com/ABC/xyzdirector/login.d
https://subdomain.domain.com/abc/XYZdirector/login.d
https://subdomain.domain.com/abc/xyzdirecTOR/login.d
https://subdomain.domain.com/Abc/XyzDirector/login.d
When I do this, the url gets forwarded and servered.
How do I block this?
You want a case-insensitive regex location:
location ~* ^/abc/xyzdirector/login\.do$ {
return 444;
}
If you have any other regex locations in your config, make sure you put this one above any others that may match this url.

Resources