nginx rewrite from apache 1 rule - nginx

Hi need help to reqrite this rule from apache to nginx, trying all day but nothing:
apache:
^pimage/small-([^/.]+)-([^/.]+).jpg$ /img_on_fly.php?iname=$1&iuid=$2&isize=small
tried with all online converters, tried everything I found online and nothing. I have no experience with nginx but other 15 rules rewrited one by one, but all were simple than this, so if someone can. Also, I am not sure what is "location" for this rule in nginx conf?

try the following in the server element of your conf file
location ~ ^\/pimage\/small\-(?<iname>[^\/\.]+)\-(?<iuid>[^\/\.]+)\.jpg$ {
try_files $uri /img_on_fly.php?iname=$iname&iuid=$iuid&isize=small
}
your conf file should look like:
server {
...
...
...
}
you can just change it to
server {
...
...
...
location ~ ^\/pimage\/small\-(?<iname>[^\/\.]+)\-(?<iuid>[^\/\.]+)\.jpg$ {
try_files $uri /img_on_fly.php?iname=$iname&iuid=$iuid&isize=small
}
}

What about escaping the dots, like so:
rewrite ^pimage/small-([^/\.]+)-([^/\.]+).jpg$ /img_on_fly.php?iname=$1&iuid=$2&isize=small;
try it and comment back.
UPDATE:
Try escaping the dashes too, like so:
rewrite ^pimage/small\-([^/\.]+)\-([^/\.]+).jpg$ /img_on_fly.php?iname=$1&iuid=$2&isize=small;
After try that, try also the variation of "dots not escaped and dashes escaped".

Related

About Nginx $arg_name syntax

Please tell me the syntax to start with with $arg_name in Nginx
For example, I want to write if $arg_name starts with Test.
I want to write a syntax that matches the following
http://localhost/sss?name=Test1
http://localhost/sss?name=Test2
location /
{
 if($arg_name=="Test") →I want to write the syntax starting with Test here
{
return 500;
}
}
Seems you can use regexes
This is completely untested. But maybe it will get you closer.
Using the suggestion here to use location instead of an if.
https://www.digitalocean.com/community/questions/nginx-rule-for-wildcard-url
location ~ /.*name=Test.*/ {
...
}
Here's my favorite regex playground if you don't already have one.
https://regex101.com/r/Q89zXg/2/

Nginx : how to regrex on Nginx url wildcard?

I want to configure regular expression on location
example: /order-service/js/app.js or /order-service/js/xxxx/xxxxxx/app.js like this,
So order-service will be set proxy_pass://order-service/js/xxxx/xxxxxx/app.js
that means I will get 2 variables, one is order-service, and another is /js/xxxx/xxxx/app.js, so how can I write the URL wildcard?
location ~ ^/(order-service)(/js/.*) {}
or more specific:
location ~ ^/(order-service)(/js.*/app.js) {}

How to use try_files with 2 or more roots

I have looked hi and low and found no such implementation and am wondering if what I am trying is even possible. I have 3 relative paths that serve up static content:
Path1: /usr/local/www/style1/static/...
Path2: /usr/local/www/style2/static/...
Path3: /usr/local/www/style3/static/...
The 3 different roots are static unto themselves but the content from /static on down is only semi-static (might be a bit different depending on the exact file being served up and may exist in one path and not in another). For example
/static/css/custom.css
/static/css/form/button.css
/static/css/form/images/buttondisabled.png
/static/images/ui/buttons/add_item.png
/static/images/ui/menu/help.png
The following is what I would like to do. Which is basically, if "/static" content is requested I want to check the relative path associated with path1 and if not found there then check the relative path associated with path2 and if not found check the relative path associated with path3. This seems fairly simple but I have not found any examples that outline how this might be done. Could I set the 3 different roots up as variables perhaps:
path1root /usr/local/www/style1;
path2root /usr/local/www/style2;
path3root /usr/local/www/style3;
location /static
{
try_files path1root/$uri path2root/$uri path3root/$uri (=404);
}
Or might that be done as follows since it is only needed for /static content:
location /static
{
path1root /usr/local/www/style1;
path2root /usr/local/www/style2;
path3root /usr/local/www/style3;
try_files path1root/$uri path2root/$uri path3root/$uri (=404);
}
Or can what I am attempting to do even be done at all ?? If I cannot do it with 3 roots could it be done with just 2 roots without defining one of them as an overall arching base root. If it is possible to do this and stay away from regular expressions that would be better -- but if that is needed then that is needed.
You could use a common root and try the three directories in the try_files statement:
location /static {
root /usr/local/www;
try_files /style1$uri /style2$uri /style3$uri =404;
}

nginx rewrite rules http://example.org/img?src=A to A?

My url format :
http://example.org/img?src=http://a.com/logo.png
I want nginx to rewrite this request to http://example.org/logo.png
(not url redriect)
how to do this?
The src argument is available as the $arg_src variable, however, you will need to use an if block to extract the part that you need. See this caution on using if.
For example (and you will need to adapt the regex to your specific needs):
location = /img {
if ($arg_src ~ \w(?<src>/\w.*)$) {
rewrite ^ $src last;
}
}
See nginx documentation here and a useful resource for regular expressions here.

nginx url rewrite with proxy pass example?

I need to know how do i do a proxy pass in nginx for certain url pattern only
i have written following but i am not sure whether its working as i wanted. What i wanted is
1. if the url matches '/member-chat' it needs to be redirected the proxy pass
2. anything else needs to be re-written as below
is what have written is correct ?
location ^/member-chat {
proxy_pass http://lxx.com:5280/http-bind;
}
location !/member-chat {
rewrite ^/files/([^/]+)/([^/]+)$ /_files/$1/$2;
rewrite ^/plugins/([^.]+) http://www.lxx.com:9090/plugins/$1;
}
if i do this as below
location / {
rewrite ^/files/([^/]+)/([^/]+)$ /_files/$1/$2;
rewrite ^/plugins/([^.]+) http://www.lxx.com:9090/plugins/$1;
}
i get a error
nginx: [emerg] duplicate location "/" in /var/www/vhosts/system/lxx.com
/conf/vhost_nginx.conf:4
nginx: configuration file /etc/nginx/nginx.conf test failed
Several issues:
Your location #1 location ^/member-chat is wrong
because ^ to match the beginning of the path only works with regular expression matching (location ~ or location ~* for case-sensitive/-insensitive expression matching).
Either do location /member-chat which will also match locations like /member-chatABCDE or /member-chat/xyz
or use location = /member-chat to only match /member-chat.
You can also use regular expressions like location ~ ^/member-chat (prefix-match) or location ~* ^/member-chat$ (exact match), but avoiding regular expressions in favor of prefix or even better exact matches is recommended
(regular expressions have much worse performance and are compared at the very last in the matching process).
Location #2 is just plain wrong because there is nothing like a not operator for location matching.
nginx will process locations in a certain order, e.g. it will start with exact matches (=), then check for prefix-matches (no modifier) and afterwards check for regular expressions (~ or ~*).
However, if a regular expression match is found, it will be favored over the prefix-match.
Conclusion
location = /member-chat {
# exact match
# proxy stuff for chat goes here
}
location /files {
# match files
}
location /plugin {
# match plugin
}
I really recommend you to read the nginx docs to prevent you from asking one question after another.
E.g. location matching is a complex topic but so far well covered by the docs already.

Resources