Nginx - if $request_uri exists proxy_pass one url, otherwise another url - nginx

I want to handle 2 cases: test.example.com and test.example.com/ABC.
If the entered url is the base domain (test.example.com), I want to proxy_pass a given endpoint (Let's say example.com/home).
If test.example.com/ABC is given, I want to proxy_pass to example.com/confirm/ABC
test.example.com = https://example.com/home
test.example.com/ABC = https://example.com/confirm/ABC
I made the (1) work like so:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name test.example.com;
location / {
proxy_pass https://example.com/home;
}
}
But I couldn't figure out how to say "If $request_uri exists, proxy_pass to different endpoint". I tried:
location / {
if ($request_uri) {
proxy_pass https://example.com/confirm/$request_uri;
}
proxy_pass https://example.com/home;
}
How can I achieve this?

$request_uri always exists and is never empty. The URI for the root of your domain is a single / (even if it's not displayed in the browser's address bar).
The location block which matches that URI is location = /. See this document for details.
For example:
location = / {
proxy_pass https://example.com/home;
}
location / {
proxy_pass https://example.com/confirm/;
}
The first location block only matches the root URI /, and the second location block matches any other URI. The remainder of the URI is automatically appended to /confirm/. See this document for details.

Related

Nginx same location 2 blocks with diffrent proxy pass

Trying to configure nginx to have 2 proxy passes for the same location.
Example:
domain.com to go to the homepage proxy
domain.com/user to go to user profile proxy
What i need to do:
What I tryed:
location = / { proxy_pass http://home_page; }
location ~ ^/(.+) { proxy_pass http://user_profile_page; }
If I go to domain.com/ and domain.com/username it goes to the same proxy.
I have to mention that the config has also the /auth location block with exact match "="
that has domain.com/auth, domain.com/auth/login. This location block works as expected
location ^~ /auth { proxy_pass http://auth_page; }

NGINX: Convert URI path to query string

My goal is to convert all non / requests of my Nginx server to /?data={uri-path}.
I was trying with the following configuration:
server {
server_name example.com;
listen 80;
location = / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
alias /home/....;
expires $expires;
}
location / {
return 301 http://example.com/?data=$request_uri;
}
}
The problem is I end with an URI like this: http://example.com/?data=?data=?data=?data=?data=?data=
and the browser complains about lots of redirections.
Based on documentation, location = should have the priority but seems I'm doing something wrong.
And feedback is appreciated.
It depends on what location = / does.
If it points to a directory and that directory contains an index.html file, Nginx will internally rewrite the URI, then search for a location to process that revised request.
This will result in a redirection loop.
You could break the loop by adding another location block to handle the final URI, for example:
location = /index.html { root ...; }
If the location = / block is intended to proxy the request, then you are missing a proxy_pass statement.

nginx proxy_pass removing base path

So I currently have a site at http://www.example.com and I would like that all requests from
http://www.example.com/api/v2/<anything>
to be proxied to
http://api.v2.com/<anything>
Note that http://api.v2.com does not have a base path.
My config:
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
This way, however, the requests are being proxies to http://api.v2.com/api/v2/<anything> keeping the original path, and not http://api.v2.com/<anything>.
I've already noticed that:
location /api/v2 {
proxy_pass http://api.v2.com/api;
works as desired - http://api.v2.com/api/v2/<anything> proxies to http://api.v2.com/api/<anything>.
Is there a way to achieve the first behavior (that is , http://api.v2.com/api/v2/<anything> to http://api.v2.com/<anything>)? Do I have to use a rewrite? I know it it related to normalized URIs and that is expected, just would like to know if there's a way to do it.
If you want to remove the proxy path from resulting url to the server, either you should have the uri part in the proxy url, or you should specify the rewrite part.
In this specific case, it should be like as follows,
location /api/v2/ {
rewrite /api/v2/(.*) /$1 break;
proxy_pass http://api.v2.com/;
}
for more information visit nginx documentation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
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.
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com/;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Modify $request_uri in Nginx

I have multiple apps running on the Nginx server:
http://example.com/app1/ctrl/view
http://example.com/app2/ctrl/view
...
I would like to assign these apps DNS like so:
http://app1.example.com
http://app2.example.com
...
For that I've tried the following server block:
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1/$request_uri;
}
}
If a user is not logged in, my app would redirect to URI:
app1/ctrl/user/login?_next=/app/ctrl/view
Essentially $request_uri becomes (Note doubled app1 instance):
app1/app1/ctrl/user/login?_next=/app/ctrl/view
Is there a convenient way to modify $request_uri or a better method to get around this problem?
EDIT1
It seems I've solved my problem with the following server block:
server {
listen 80;
server_name app1.example.com;
location / {
set $new_request_uri $request_uri;
if ($request_uri ~ ^/app1/(.+)$) {
set $new_request_uri $1;
}
proxy_pass http://example.com/app1/$new_request_uri;
}
}
If someone knows a better (or proper "Nginx") way to do this please don't hesitate to post an answer.
EDIT2
Based on the comments I've also tried the following:
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1/;
proxy_redirect /app1/ /;
}
location ~ ^/app1/(.+)$ {
return 301 http://$server_name/$1;
}
}
This one looks better on screen, as it eliminates app1 instance in the $request_uri part completely, but you must have two location blocks.
EDIT3
The most efficient way to solve my problem apparently is as shown in this config:
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1/;
proxy_redirect /app1/ /;
}
location /app1/ {
rewrite ^/app1(.+) $1 permanent;
}
}
This is due to the fact, that Nginx always tries to match the longest prefix first and then (if ^~ modifier is not present) starts sequentially processing regexes until the first regex match is found. Essentially this means that all regexes are processed on every request, regardless if any of these find a match, therefore it's better to have regexes inside location directives.
You don't need to go complex way. Solution is much simpler
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://app1.example.com/app1/;
}
location /app1/ {
proxy_pass http://app1.example.com/app1/;
# or
# rewrite ^/app1(.+) $1 permanent;
}
}
Nginx will take care of adding /app1/ to request and strip it from Location header.
See proxy_redirect directive.

nginx proxy_pass configuration

I need to proxy a couple of urls to different hosts. Actually, I'm using the same host with different port to test my nginx configuration. This is my virtual host definition:
server {
listen 8081;
server_name domain.com;
location /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
location / {
# rewrite cq_user_authenticated===(.*)/(.*)/iuuid=(.*)/commenti.html$ /Plasmid/comments/legacy/$3/$1 ;
# rewrite querystring===(.*)$ /Plasmid/comments/legacy/$1 ;
# rewrite cq_user_authenticated===([^&]*)&/.*uuid=([^/]*) /comments_api/legacy/$2 ;
# rewrite userdetails(.*) /Plasmid/comments/user_details ;
root html;
index index.html index.htm;
}
}
Of course my hosts file has mapping for the domain.com
When I call the url: http://domain.com:8081/Plasmid/default/page/12 I get an http 404
If I remove the second location from my configuration:
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
I get the resource I want, but some part are missed since the are hosted on a different platform:
[error] 1033#0: *1 open() "/usr/local/Cellar/nginx/1.2.6/html/_community/content
How can I resolve this issue?
Do a little change:
location ^~ /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location ^~ /_comunity/ {
proxy_pass http://localhost:8082/comments_api/;
Why is that? Because ^~ means starts with and when you request for page:
http://domain.com:8081/Plasmid/default/page/12
it fit to that rule. In your configuration you are using no mark and something like this:
location /anylocation
and it looks like your nginx prefer rule
location / {
than
location /Plasmid
and
location /_comunity
because it's using root directive and searching for _community/content in html folder (as you get in error message).
In other words ^~ has greater priority than no mark. One thing that could also help is to add break directive after each proxy_pass directive;

Resources