Nginx Config redirecting to lowercase string instead of original one - redirect

I am trying to do a redirect in my nginx config from SubDomain.domain.com to domain.com/SubDomain. The problem I am facing is that when i try SubDomain.domain.com nginx redirects it to domain.com/subdomain and misses the uppercases. How can i achieve that?
Here is my nginx config block:
if ($http_host ~* "^(.+)\.domain\.com$") {
set $subdomain $1;
rewrite (.*) http://domain.com/$subdomain$1;
}

The variable is always lowercase.
$host
This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.
This variable may have a different value from $http_host in such cases: 1) when the Host input header is absent or has an empty value, $host equals to the value of server_name directive; 2)when the value of Host contains port number, $host doesn't include that port number. $host's value is always lowercase since 0.8.17.
$http_HEADER
The value of the HTTP request header HEADER when converted to lowercase and with 'dashes' converted to 'underscores', e.g. $http_user_agent, $http_referer...;
Source: http://wiki.nginx.org/HttpCoreModule#.24host

Related

Haproxy not replacing the variable

I'm using the following rule to redirect one subdomina (anything.registro.myserver.com.br) to another host appending a variable.
The problem the variable %[capture.req.hdr(0)] it's not replacing on the redirected link.
The final result is https://app2.otherserver.com.br/register/%[capture.req.hdr(0)]
acl fqdn_register hdr_dom(host) -i .registro.myserver.com.br
capture request header Host len 128
redirect prefix https://app2.otherserver.com.br/register/%[capture.req.hdr(0)] code 302 if fqdn_register
What is wrong?
Best regards

How do I secure cookies in HAProxy 2.2+ using an `http-response` line?

I'm upgrading from HAProxy 1.8 to 2.2 and the command reqirep has been deprecated and removed. I used this previously to automatically add Secure to cookies that weren't previously secure. I want to use the new http-response syntax.
My old code looks like this:
rspirep ^(set-cookie:\ (?:(?!(\ Secure|ASPXAUTH=)).)*)$ \1;\ Secure
This adds ; Secure to any cookie header that doesn't contain Secure or ASPXAUTH=.
I'd like to do the same thing with one of the modern http-response commands.
Here's my initial translation:
http-request replace-header Set-Cookie (.*) %[src];\ Secure if { hdr_reg(Set-Cookie) -i (?!(\ Secure|ASPXAUTH=)) }
# Replace the "Set-Cookie" header
# That contains any value
# With the initial value with "; Secure" appended to the end
# If the cookie doesn't contain " Secure" or "ASPXAUTH=", ignoring case
Is this the right approach? Have you done this successfully?
We ended up with this as a solution. It's not perfect because it will only look for Secure modifier on the end of the Set-Cookie line but it works for what we need.
http-response replace-header Set-Cookie ^((?:.(?!\ [Ss]ecure))*)$ \1;\ Secure

Redirect/rewrite nginx location to .sock file without prefix

I have one server that has several APIs running on it. One of them is users-DB The following gets down to gunicorn just fine:
location /usersDB/ {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock;
}
Except when I try to access the usersDB API's /helloWorld route, and look in the logs at gunicorn.err I see:
GET /usersDB/helloWorld
I was hoping to see:
GET /helloWorld
Of course, gunicorn returns 404s and that is what I see in my browser. I've tried rewrite rules:
location /usersDB/ {
rewrite /usersDB/(.*) /$1 last;
include proxy_params;
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock;
}
But the above results in the requests making their way to /var/www/htmlhelloWorld instead of app.sock.
I know that if you use a url for the proxy_pass you just add a trailing /, but I'm not sure what to do in the case of a sock file.
How do I get rid of the /usersDB/ suffix that is now included on all routes in nginx?
Use a separating :. For example:
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock:/;
See this document for details.

Nginx redirect without scheme and hostname

I have used return or rewrite in Nginx to redirect, but both will redirect with the http header Location which has value of the full url, but I want it just value the path.
for example:
return 301 /test;
or
rewrite ^/$ /test permanent;
will has the Location value of
http://www.example.com/test
but I want it be just
/test
I wonder if it is possible.
Solved.
as of 1.11.8, just set the directive(Thanks to #RichardSmith):
absolute_redirect off;
before 1.11.8, I find a wonderful answer(Thanks to #xiaochen):
https://stackoverflow.com/a/39462409

NGINX redirect from old to new domain

I need to redirect requests like http://domain.com/?part=word to http://another_domain/?part=word, 'word' may be different
My nginx configuration:
server {
listen 80;
server_name domain.com www.domain.com;
rewrite ^/?part=(.*)$ http://another_domain/?part=$1 permanent;
}
redirect does not work, what am I doing wrong?
instead of specifying what words you want to handle, you can just tell nginx to append all the args
server {
listen 80;
server_name old.example.com;
return 301 http://new.example.com$request_uri;
}
Update:
I've recently found out that $request_uri already contains the query string, and thus the extra part that I've had ($is_args$query_string) would not be necessary. I've updated the above part and removed the extra query string.