Can Nginx substring the $request_uri? - nginx

I want to substring $request_uri
For example, The URL address is:https://example/surveyssl/survey
If I using $request_uri, it will get "surveyssl/survey"
Can I substring "surveyssl" so that I can get "survey" only?
location /surveyssl/survey{
proxy_pass http://ssldev/surveyssl/index.php/$request_uri;
#It will fail because the url output is
#http://ssldev/surveyssl/index.php/surveyssl/survey
#but I want to get this url:
#http://ssldev/surveyssl/index.php/survey
}

With your shown sample, could you please try following. Please make sure to clear your browser cache before testing your URLs. First ruleset is very specific one to catch only surveyssl; 2nd rules set it dynamic one where it will catch anything coming after surveyssl.
location ~ /surveyssl/(survey/?) {
proxy_pass http://ssldev/surveyssl/index.php/$1;
}
EDIT: To make it dynamic try following rules:
location ~ /surveyssl/([\w-]+/?) {
proxy_pass http://ssldev/surveyssl/index.php/$1;
}

Related

nginx location with wildcard to proxy_pass

I understand a few basic concepts of nginx, but this one is beyond me.
This directive below works perfectly fine
location /a/web/8b5d3df315/assets {
proxy_pass https://thisdomain.com/a/assets;
}
However, the 8b5d3df315 needs to be a wildcard (or 'any') since I have many different entities needing to point to the same uri https://thisdomain.com/a/assets
I have tried this which returns a 404.
location /a/web/([A-Za-z0-9]+)/assets {
proxy_pass https://thisdomain.com/a/assets;
}
As well as the one below, which returns a: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except"
location ~ ^/a/web/([A-Za-z0-9]+)/assets {
proxy_pass https://thisdomain.com/a/assets;
}
This answer felt close to my problem https://stackoverflow.com/a/53354944/1864622 but I don't think is quite a match.
Does anyone know how to go about this?

nginx proxy_pass doesn't work right when concatenating to variable

I have an NGINX server running as a reverse proxy for a service I host. I tried configuring it with this:
set $upstream_nzbget http://nzbget:6789;
location /nzbget/api/ {
proxy_pass $upstream_nzbget/;
}
But the proxy_pass doesn't work properly when I try to add the slash / to the end of $upstream_nzbget. If I change it to this, it works properly:
location /nzbget/api/ {
proxy_pass http://nzbget:6789/;
}
So I think it has something to do with variables in proxy_pass. In the first case (the one that isn't working), I'm not really sure what the URL ends up being. I haven't found a way to log the final URI that gets used by proxy_pass.
Can someone explain why the first case isn't working? What is the proper solution? Note that I need to keep the variable in proxy_pass so it uses the resolver.
Side note: I use a trailing slash in my location block because otherwise I get a 404 when I use this URL:
domain.com/nzbget/api/api:password/xmlrpc
This is why I use /nzbget/api/ instead of /nzbget/api.
EDIT 1
I played around with this some more, and I found that this also doesn't work:
location /nzbget/api/ {
set $upstream_nzbget http://nzbget:6789/;
proxy_pass $upstream_nzbget;
}
This one is really strange. It's the same string, the only difference is using a variable vs a string literal. I'm not doing any string concatenation here. I'm not sure why there's a behavioral difference.
EDIT 2
This SO question might be the same issue, but it has no helpful answers.
You need to pass $request_uri when using it with variable. It's there in the docs somewhere.
It'll be something like:
location /nzbget/api/ {
set $upstream_nzbget http://nzbget:6789$request_uri;
proxy_pass $upstream_nzbget;
}

How to block a specific URL in NGINX webserver

I want to block a specific URL but I am not able to do this.
The URL that should be blocked is example.com/clientarea/?dxx_g=dddd.
But the following url should still work - example.com/clientarea.
I tried the following:
location ^~ /clientarea/ {
return 444;
}
But if I do this it will block all connections to /clientarea.
I hope you can help me or advise me how to make this possible.
The location and rewrite statements test a normalized URI which does not include the ? and anything following it.
The $request_uri variable contains the entire URI. Test this variable using an if or map directive.
For example:
if ($request_uri = /clientarea/?dxx_g=dddd) {
return 444;
}
You can also use regular expressions. See this document for more. See this caution on the use of if.
If you have a number of URIs to block, you should consider using a map instead.

Is it possible to replace random url with nginx substring

i have this kind of random string url using nginx substring
http://example.com/-UJ7GGXX5bo3w/ZnpPtIWiABs/TTTXIANZk/dHNEIuFjucvd7RiwwCLcBchJiIwIi8jGAmOrvyxs
i have successfully use proxy_pass to the domain name and i need to change the random url parts to become something like this
http://example.com/directory1/directory2/directory/directory4
thanks
Well you can use regex in the location like
location ~ ^/[0-9a-zA-Z]+/[0-9a-zA-Z]+/[0-9a-zA-Z]+/[0-9a-zA-Z]+$ {
proxy_pass http://example.com/directory1/directory2/directory/directory4;
}

Nginx Rewrite Issue to External link

I'm trying to set up a permanent redirect from
http://domain.com/member/blog_post_view.php?postId=1
to
http://blog.domain.com/friendly-url-here
The source URL contains both a ? and an = which I think might be the cause but am unsure.
I've tried all sorts of nginx suggestiosn including the one below but can't seem to get the redirection to work and hoped someone can point me in the right direction.
location /blog_post_view.php?postId=1 {
rewrite "/blog_post_view.php\?postId\=1" http://blog.domain.com/friendly-url-here permanent;
}
That part of request line starting from the question mark is called query string, while the location directive matches only path part of URI.
You should use $arg_* variable instead:
location =/blog_post_view.php {
if ($arg_postId = 1) {
return 301 http://blog.domain.com/friendly-url-here;
}
}
Reference:
http://nginx.org/r/location
http://nginx.org/r/if
Embedded Variables

Resources