Nginx: location regex not working - regex

i'm getting trouble with my regex don't know what is wrong with it. it's returning a URi with orls/f instead of orls/f?p=4550. When i pass https://secure.toto02.com/orls/myservice/f?p=4550
my conf file is below
location ~ "^/([a-zA-Z]+)/myservice/(.+)$" {
error_log /var/log/nginx/error-server.log notice;
rewrite_log on;
#proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://192.168.3.45:8080/orls/$2;
proxy_redirect http://192.168.3.45:8080/orls/ https://secure.toto02.com/$1/myservice/ ;
}
Can anyone help?

Capturing the location will not encompass the query string. You have to manually include it via $is_args and $args variables like so:
proxy_pass http://192.168.3.45:8080/orls/$2$is_args$args;

Related

NGINX - Regex into locations.conf doesn't work

I have an issue into the configuration of my nginx proxy.
I just try to use regex has it is defined here: https://underthehood.meltwater.com/blog/2017/12/12/lightweight-tests-for-your-nginx-api-gateway/
But nothing works. My server won't start if it is any regex into the path.
I tried:
location ~ ^/tesla/(?<id>.*)$ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
}
or
location ~ ^/tesla/test/ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
}
or
location ~* ^/tesla/test/ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
}
Nothing work. My final aim is to extract a value from the url and do the following thing:
location ~* ^/(<version>.*)/test/ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Prefix $version;
}
UPDATE (SOLVED)
I finally succeed thanks to Richard.
Here is the complete code:
location ~* ^/api/(?<version>.*)/(?<service>.*)(/.*/.*)$ {
proxy_pass http://$service.localnetwork:8080$3;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Prefix /api/$version/$service;
proxy_set_header msvc_name $service;
proxy_set_header msvc_version $version;
}
You can't use a proxy_pass with a static URI within a regular expression location block, it will throw an error - see your Nginx error log. See this document for details.
However, you can construct the URI to send upstream by appending variables to the proxy_pass statement. In your case, you could capture the remainder of the URI in the same regular expression.
For example:
location ~* ^/([^/]+)/test(/.*)$ {
proxy_pass http://localhost:8081$2;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Prefix $1;
}

Nginx forward proxy with # in the url

So I am trying to forward a url that contains a # and am having some difficulty. I have the following in the location sections:
location ~* /.%23/evl {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://foo.com;
}
location ~* /(?!.%23/evl) {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bar.com;
}
I would like everything with
http://example.com/#/evl/* to goto foo.com
everything else to bar.com
Any help would be appreciated.

Nginx proxy - limit request with regex

I've an Nginx 1.6.2, working as proxy. The backend HTTP server is an Apache.
I'ld like to control the number of connections certain URL, in some vhosts. The exact "location" form is works, but if I pass a regex to "location", it doesn't.
server {
listen 80;
server_name www.myhost.com;
location ~* ^/.*ABCD_promo.*$ {
limit_req zone=one burst=5;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://vm-apache4;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://vm-apache4;
}
}
In nginx.conf, I've this directive:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
before the config part(s) above.
When I change the "location ~* ^/.*ABCD..." to "location /ABCD_promo", then it works. What em I misses?

Changing the Host passed to the backend

I have my frontend server running nginx. The backend is on another machine on the same VPN. This is its config:
server {
listen 80;
server_name *.vpn.domain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect http://10.8.25.102:8100/ http://$server_name/;
}
}
I would like to pass a different host to the backend... I'd like the backend to receive, for requests done tosubdomain.vpn.domain.com the host subdomain.local.domain.com
Is there any way to do this? I'm looking for a regexp substitution (or even a substring substitution) but I'm having surprisingly little success... I thought it would be a piece of cake. I think the solution would be in the lines of
server {
listen 80;
server_name *.vpn.domain.com;
set $my_host $http_host;
replace $my_host .vpn. .local.
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $my_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect http://10.8.25.102:8100/ http://$server_name/;
}
}
It's just that I haven't found yet the proper syntax for replace $my_host .vpn. .local. I don't really care about multiple substitutions... I won't have a.vpn.a.vpn.domain.com
I finally figured it out, I can do
if ($http_host ~ ^(.*)\.vpn\.(.*)$) {
set $my_host $1.local.$2;
}
And then, as there're CSRF validations in place, I also need to rewrite the Referer... so this is how it ended up looking
server {
listen 80;
server_name *.vpn.domain.com;
set $my_host $http_host;
if ($http_host ~ ^(.*)\.vpn\.(.*)$) {
set $my_host $1.local.$2;
}
set $referer $http_referer;
set $referer_host no;
if ($http_referer ~ ^(https?://)([^/]+)(/.*)$) {
set $referer_host $2;
set $rewritten_referer $1$my_host$3;
}
if ($referer_host = $http_host) {
set $referer $rewritten_referer;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $my_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Referer $referer;
proxy_set_header IS_SECURE no;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect https://$my_host/ https://$http_host/;
proxy_redirect http://$my_host/ http://$http_host/;
}
}

nginx location regex /yyyy/mm/dd/

I'm trying to pass requests from address helpme,com/donor/2014/12/07/Name on the other server.
URL like the: /donor/2014/12/07/Mike
need to convert to a query type of: /donor.php?yyyy=2014&mm=12&dd=07&donor=Name.
Now i have:
location ~* ^/donor/+$ {
rewrite ^/(.*) /donor.php?yyyy=$1&mm=$2&dd=$3&donor=$4 break;
proxy_pass http://164,151,234,168;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Help me please.
The regex for that should be (untested) something like /donor/(\d{4})/(\d{2})/(\d{2})/(.+).
The use of commas (,) instead of periods (.) for the hostname/IP seems wrong, though. As far as I know the same character is used everywhere….