I've two locations on my Nginx config:
location /ok {
return 200;
}
location /check {
# pseudo code
if (call(http://localhost:9090/ok) == 200) do something
}
I just can use Nginx and think one solution it can be proxy_pass /check to /ok and if everything were OK proxy_pass request again to a new location, Any idea?
Thanks
Related
How to write properly configuration for Nginx in order to map incoming POST /some_endpoint/product/component/key to the appropriate node?
Product, component, key are different from request to request.
I have tried some variants, no lack.
location /some_endpoint/([0-9A-Za-z]+)$/([0-9A-Za-z]+)$/([0-9A-Za-z]+)$ {
proxy_pass http://first-node:8080/some_endpoint/$1/$2/$3;
}
location /some_endpoint/(~*)$/(~*)$/(~*)$ {
proxy_pass http://first-node:8080/some_endpoint/$1/$2/$3;
}
I have achieved needed behaviour using the following configurations:
location ~/rest-endpoint1/(.*)$ {
proxy_pass http://first-node:8080/rest-endpoint1/$1$is_args$args;
}
location ~/rest-endpoint2/(.*)$ {
proxy_pass http://first-node:8080/rest-endpoint2/$1$is_args$args;
}
location ~/rest-endpoint3/(.*)$ {
proxy_pass http://second-node:8081/rest-endpoint3/$1$is_args$args;
}
location ~/rest-endpoint4/(.*)$ {
proxy_pass http://second-node:8081/rest-endpoint4/$1$is_args$args;
}
So, I want to redirect all my images requests to another server, but I would like to check if image/webp is present on the accept header of the request before doing so, I was planning on doing that verification on the other server, but I think that it would be better to do it in nginx, is it possible to do that?
If the header was not present, I would just like to leave it as it is (serving the static image)
This is what I have rn:
location ~ \.(png|jpg|jpeg)$ {
proxy_pass ...;
}
You can use the following:
map $http_accept $local {
~image/webp '';
default $uri;
}
server {
...
location ~ \.(png|jpe?g)$ {
try_files $local #remote;
}
location #remote {
proxy_pass ...
}
}
However if the requested file is not found on the local server, request will be proxied to your remote server. If it isn't suitable for you, one of the possible ways is to use an evil if:
location ~ \.(png|jpg|jpeg)$ {
if ($http_accept ~ image/webp) {
proxy_pass ...
}
}
This should work with the modern nginx versions, and if isn't evil in any situation, but this is exactly the one where I'd better avoid it. As an alternative you can use this one:
map $http_accept $locname {
~image/webp remote;
default local;
}
server {
...
location ~ \.(png|jpe?g)$ {
try_files "" #$locname;
}
location #local {
# serve the file locally
}
location #remote {
proxy_pass ...
}
}
I have two applications, one nodejs and another react app. I user is not logged in, I return the node app, if user is logged in, I want to return the react app index.html file.
location / {
if ($cookie_isLoggedIn = "true") {
// how can I return the index.html file here ?
}
proxy_pass http://localhost:3000;
}
What I tried so far:
rewrite ^/$ /platform-build/index.html; - doesn't do anything.
alias and root are not working inside if statement.
You need to use two different content handlers (proxy_pass and try_files), so you need two different locations for that. You can return some static HTML content via the directive like
return 200 "<body>Hello, world!</body>";
but I don't think it will satisfy your needs. However you can use the following trick (taken from this answer):
map $cookie_isLoggedIn $loc {
true react;
default node;
}
server {
...
location / {
try_files /dev/null #$loc;
}
location #react {
root /your/react/app/root;
index index.html;
try_files $uri $uri/ /index.html;
}
location #node {
proxy_pass http://localhost:3000;
}
}
The author of original trick says it doesn't have any performance impact.
I'm trying to chain two requests together using nginx's njs module and subrequests. I can see that special_value comes back in the response headers but I don't know how to read the value using proxy_set_header. The code below results in a blank value in X-Special-Value
nginx.conf:
location ~* /step_one {
js_content njs_func;
}
location ~* /step_two {
proxy_pass http://some_other/api;
proxy_set_header X-Special-Value $http_special_val;
}
http.js
function njs_func(r) {
r.headersOut['special_val'] = '42';
r.subrequest('/step_two')
.then(reply => r.return(reply.status, reply.responseBody));
}
you could do something like this:
nginx.conf
set $specialValue "";
location ~* /step_one {
js_content njs_func;
}
location ~* /step_two {
proxy_pass http://some_other/api;
proxy_set_header X-Special-Value $specialValue;
}
https.js
function njs_func(r) {
r.variables.specialValue = '42';
r.subrequest('/step_two')
.then(reply => r.return(reply.status, reply.responseBody));
}
I need to make a reverse proxy using nginx that proxy clients according to a specific id that they provides inside they post/get:
clientid=<value from the clientid inside the post/get>
if clientid=XXX {
ProxyPass server1
}
if clientid=YYY {
ProxyPass server2
}
How can I achieve that with nginx ?
with #workaround comments, i get something like this:
location ~ ^/(api|newapi)/(v2/)?(xxxx|yyyy|zzzz) {
echo_read_request_body;
echo $request_body;
if ($request_body ~* (.*)?clientid=2621(.*)?) {
proxy_pass https://apiold;
}
proxy_pass https://apinew;
include proxy.conf;
}
According to official nginx documentation try using:
if (($request_body ~* (.*)clientID#1(.*)) )
{
proxy_pass server1;
}
if (($request_body ~* (.*)clientID#2(.*)) ) {
proxy_pass server2;
}
}