Using NGINX as a load balancer running on 10.1.2.15:9002, I have a need to rewrite http://10.1.2.15:9002/proxy.stream?opt=1 to http://10.1.2.15:9002/app/proxy.stream?opt=1.
Following are bits from my nginx.conf file:
http {
upstream app_cluster {
server 10.1.2.23:8080;
server 10.1.2.25:8080;
}
server {
listen 9002 default_server;
location /app/ {
proxy_pass http://app_cluster/;
}
location ~ ^/proxy.stream(.*)$ {
rewrite ^(.*)$ /app/$request_uri last;
}
}
}
By the way, I can replace the rewrite line with return 401 (for example), and I can see the 401 HTTP status returned using Chrome Developer Tools, so I know the regex is matching. I just can't get the URI rewritten properly. In fact, I only see the original request with a 406 status in Developer Tools, so I suspect something is wrong with my rewrite syntax.
Does anyone see what is wrong with this configuration?
Using $request_uri in the replacement string of a rewrite statement is problematic, as it has not been normalised and also contains the query string, which by default, rewrite will append again.
Also, your replacement string contains //, as you are appending a URI which already has a leading /.
The regular expression location is not necessary, as a prefix or exact match location will suffice and is more efficient for nginx to process. See this document for more.
For example:
location /proxy.stream {
rewrite ^ /app$uri last;
}
Make use of the matching part from the regex instead of $request_uri
rewrite ^(.*)$ /app/$1 last;
Related
i have question to Nginx specialist. There are such rules on Nginx.
If i request with header 'main', it adds /ismain to url:
if ($http_main) {
rewrite ^(.*)$ /ismain/$1;
}
Next it cut /ismain/ from url and goes to Host
location /ismain/ {
rewrite ^/ismain/(.*)$ $1 break;
proxy_pass http://Host:9999;
}
It works good and i can't change it because of company sequrity policy.
But now i need to do callback and headers are not allowed.
So i request in such a way(without header):
http://11.11.117.111:8077/ismain/someaddress
But Nginx cut off all slashes after port... and responses 400 Bad URI.
In logs i can see such url after cut:
http://11.11.117.111:8077someaddress
I tried request with double slashes:
http://11.11.117.111:8077//ismain/someaddress
http://11.11.117.111:8077/ismain//someaddress
But it's not work. I have the same response. I'm in frustration why i works with header, but doesn't want with my formed path. I suppose it's the same.
Maybe i need to screen slash with some symbol ? Can you advice me ?
All Nginx URIs contain a leading /.
Your first rewrite statement is adding a double //. You should use:
rewrite ^(.*)$ /ismain$1;
or:
rewrite ^/(.*)$ /ismain/$1;
Your second rewrite statement relies on the first bug. You should use:
rewrite ^/ismain(/.*)$ $1 break;
or:
rewrite ^/ismain/(.*)$ /$1 break;
I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.
How can I redirect "http://domain.com." to "http://domain.com" with Nginx?
What's the recommended way of doing this? Regex or is there any other options?
The following snippet does this in a general way, without having to hard code any hostnames (useful if your server config handles requests for multiple domains). Add this inside any server definition that you need to.
if ($http_host ~ "\.$" ){
rewrite ^(.*) $scheme://$host$1 permanent;
}
This takes advantage of the fact (pointed out by Igor Sysoev) that $host has the trailing dot removed, while $http_host doesn't; so we can match the dot in $http_host and automatically use $host for the redirect.
You will need to use Regex.
server {
listen 80;
server_name domain.com.WHATEVER, domain.com.WHATEVER-2, domain.com.WHATEVER-3;
rewrite ^ $scheme://domain.com$request_uri? permanent;
}
From: http://wiki.nginx.org/HttpRewriteModule
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301
I've seen a bunch of ngnix rewrites that have syntax like this:
server {
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
I don't understand the ^(.*) part. Does the ^ take everything after the TLD of the uri?
The ^ does indeed match at the beginning of the string. In the case of nginx's rewrite directive this means the beginning of the path component of the actual URI. Unfortunately nginx's documentation is slightly incorrect. Quoting from http://www.nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite :
If the specified regular expression matches a URI, the URI is changed as specified in the replacement string.
However, this is technically wrong. rewrite does not match the whole URI/URL but only its path component (which always starts with a / even if the user only enters e.g. http://www.example.com instead of http://www.example.com/). Therefore rewrite ^(.*) http://example.com$1 permanent; does not turn into http://example.comwww.example.com.
If I remember it correctly, the ^ just sets the Regex rule to match the start of the string.
The parentheses are used to extract that part with the $1-9 variables.
Another solution from the Nginx wiki. Link
server {
server_name www.example.com;
rewrite ^ http://example.com$request_uri? permanent;
}
It seems Nginx it always un-encodes urls when used with a regular expression. I have a rewrite rule:
location /api/ {
rewrite /api/(.*)$ $1 break;
proxy_pass http://127.0.0.1:8000/$1;
}
I would like to remove the api from the usl but keep the rest of the path. Part of the path is an email address someone#somewhere.com. I am passing someone%40somewhere.com but Nginx is turning it back with the # sign.
The correct answer seem to be
location /api/ {
rewrite ^ $request_uri;
rewrite ^/api/(.*) $1 break;
return 400;
proxy_pass http://127.0.0.1:8000/$uri;
}
See Nginx pass_proxy subdirectory without url decoding for full answer and original author.
(I realize this question is older than the one I referenced but I found this in google search and may not be the last one, so ...)
That is how Nginx handles urls. You can bypass it by changing your web application to escape the "%" character as "%25" and pass someone%2540somewhere.com.
This will be unescaped as someone%40somewhere.com.