NGINX: alias or proxy based on query string - nginx

I'm looking for a way to allow a proxy_pass when certain querystrings are in the url and send the URI to an alias the rest of the time.
Here's how I have the in one environment, the location block is configured like this:
location /my-route {
alias /assets/coming-soon/;
index index.html;
}
In another, it's configured like this:
location /my-route {
proxy_pass http://tool_servers/;
proxy_redirect off;
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-Host $server_name;
}
I want to combine the two and pass the bare uri to to the coming-soon page, but URIs with ?filter=test to tool_servers.
Proxy config cannot go inside an if ($arg_filter = "test") block (I tried). Is there another work-around?

Related

How to configure nginx to redirect with subpaths matching given pattern

I am using nginx as a reverse proxy. I want to redirect locations matching some patterns keeping exactly the same subpaths. For example, this configuration does not work for me:
location ~ ^/bpmn/?(.*)$ {
resolver 127.0.0.11 ipv6=off;
set $upstream activiti:8080;
proxy_pass http://$upstream/$1;
proxy_redirect off;
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-Host $server_name;
}
I would like to redirect the location matches the path "/bpmn/a/b/c" to "http://$upstream/a/b/c". My configuration works only for one level (e.g. "bpmn/a").
Thanks a lot for your help,
Regards.

More accurate nginx location matching

I have the following nginx rule
location /api {
proxy_pass http://flask:5000/api;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
It will match the following path, which is what I'm expecting
http://localhost/api
http://localhost/api/
http://localhost/api/a
http://localhost/api?
http://localhost/api?name=value
However, it will also match the following, which I'm not expecting
http://localhost/apii
http://localhost/apiX
May I know how can I avoid from matching unwanted http://localhost/apii and http://localhost/apiX and http://localhost/apiXX and ...
This requires two location blocks - one matching specifically /api and one for everything in the path /api/. In this way urls like /apio will not be captured.
Example:
location = /api {
proxy_pass http://flask:5000/api;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#Querystrings and /api, matches the first, any /api/* matches this one
location /api/ {
proxy_pass http://flask:5000/api;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Reference http://nginx.org/en/docs/http/ngx_http_core_module.html#location
One possibility is to use a regular expression. As the proxy_pass is not actually transforming the URI, the uri element can be safely removed.
location ~ ^/api($|/) {
proxy_pass http://flask:5000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Note that regular expression location blocks are evaluated in order. See this document for details.
It's actually really simple.
Just change location /api { to location /api/ {
Nginx will add a trailing slash to any request ending /api so there is no need to add a separate block for them. This block will behave as you expected your original one to.

Nginx Passing to Servers Based on URI

I have been setting up Nginx on my router, and creating subdomains (with CNAMES) to access various components within my network. It has mostly been fairly easy, until I have come to the cameras which are proving to be a problem.
They are basic IP cameras and to date I had opened each one on a different port. They have basic authentication, and once that has been entered I am presented with a live view.
Like all the other components I have set up so far (and they all work) I started by configuring one:
server {
listen 80;
server_name cam.example.co.uk;
location / {
proxy_pass http://192.168.1.101:2001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Hitting cam.example.co.uk from either LAN or WAN gives me a username and password prompt and then the live view loads.
Since there are 9 cameras, I thought it would be a good idea to use /1, /2, /3 etc. at the end to direct me to each one rather can creating subdomains.
server {
listen 80;
server_name cam.example.co.uk;
location /1/ {
proxy_pass http://192.168.1.101:2001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /2/ {
proxy_pass http://192.168.1.102:2002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
With that I got 404 not found errors, and messages in the logs such as:
"/usr/html/new/index.htm" failed (2: No such file or directory)
Some Googling later I found out that I may need to specify the URI as well in the proxy_pass line, so I changed them to look like:
proxy_pass http://192.168.1.102:2002/new/index.htm;
This then results in the username and password prompt, but when the credentials are entered, all I am left with is a blank screen. It worked fine when it was just location / so no idea why nothing is showing now.
I have a feeling that it is putting the URI in somewhere, but I have no idea where/why or what to do about it.
EDIT
Been Googling and trying various things:
location /1 {
resolver 127.0.0.1;
set $backend "http://192.168.1.101:2001/new/index.htm";
proxy_pass $backend;
proxy_buffering on;
proxy_redirect http://192.168.1.101:2001/new/index.htm http://cam.example.co.uk/1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Then going to this in the browser cams.example.co.uk/1 brings up the username and password prompt, but then displays a blank page. Looking at the Chrome developer tools I can see unexpected token errors, and it looks like it isn't loading the .js files properly.
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
Try this:
server {
listen 80;
server_name cam.example.co.uk;
location /1/ {
proxy_pass http://192.168.1.101:2001/;
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_redirect http://192.168.1.101:2001/ http://cam.example.co.uk/1/;
}
location /2/ {
proxy_pass http://192.168.1.102:2002/;
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_redirect http://192.168.1.101:2002/ http://cam.example.co.uk/2/;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

forward matched specific location and proxy redirect it in nginx

I am trying to forward specific uri if matched to backend in nginx. For example
forward https://www.hostname.com/*/a/b/c to https://int.hostname.com/*/a/b/c (Where * is a variable auto populated from regex)
Current configuration looks like below and have no idea how to proceed on above
location /a/b/c/ {
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 180m;
proxy_pass http://int.hostname.com/v2/e/t/a/b/c;
proxy_redirect default;
}
If you want your location to match any URI that ends with /a/b/c/, you will need to use regular expression syntax. See this document for details.
The URI suffix of the proxy_pass statement is optional. In the absence of a URI suffix, the original URI will be passed unmodified. See this document for details.
For example:
location ~ /a/b/c/$ {
...
proxy_pass http://int.hostname.com;
...
}

Proxy all subdomain to other domain path

How to proxy all subdomain to other domain path?
For example
SUBDOMAIN.abcxyz123.com
To be proxied to
myapp.otherdomain.com/SUBDOMAIN
Making sure that all header/path and query parameters in the request is kept.
Update:
I've tried and have a working config but still not the one I need:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.abcxyz123\.com$;
location / {
proxy_set_header Host "myapp.otherdomain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
# this worked:
proxy_pass http://myapp.otherdomain.com/somepath/;
# this does not work:
#proxy_pass http://myapp.otherdomain.com/$subdomain$request_uri;
}
}
Try this
server {
server_name ~^(?<subdomain>.*)\.abcxyz123\.com$;
resolver 8.8.8.8;
rewrite ^/(.*)$ /$subdomain/$1;
location / {
proxy_set_header Host "myapp.otherdomain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://myapp.otherdomain.com;
}
}
This should proxy all your traffic with original query parameters(query strings, request body, request method, etc), I changed the host header to the proxied "myapp.otherdomain.com" incase the server of 'myapp.otherdomain.com' has more than one virtual hosts. If you don't want the change, use $host instead.
This answer might need another edit since your question isn't very clear. If you have further question, comment and i will edit in my answer.

Resources