Welcome! Log In Create A New Profile

Advanced

Change response 302 to 200

Posted by merlock 
Change response 302 to 200
April 23, 2024 10:12AM
Hello, I have a problem. I have 2 backend groups that are in cluster mode, and when I query them directly, the same backend returns a 302 when it doesn't find the resource in any of them and redirects it to the server that does have the resource.

What I need is that when I receive a 302 response, it continues to query the next server until receiving a 200. Additionally, if I receive a 200, I need to cache the IP of the server that returned the 200. Every time I request the resource, for example "channel01", it should always redirect me to that server and not start querying the entire cluster again.


upstream origin_city01_servers {
server origin01.domain.com:443;
server origin02.domain.com:443;
server origin03.domain.com:443;
}

upstream origin_city02_servers {
server origin04.domain.com:443;
server origin05.domain.com:443;
}

proxy_cache_path /data/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/cert.key;

listen 443 ssl;
server_name live.domain.com;

location / {
if ($cookie_backend_server) {
proxy_pass https://$cookie_backend_server;
break;
}
proxy_pass https://origin_city01_servers ;
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;

set $backend_server $upstream_addr;
if ($upstream_status = 200) {
add_header Set-Cookie $backend_server;
}

proxy_cache_valid 200 1d;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_bypass $http_upgrade;
add_header Set-Cookie $upstream_addr;

}

location /city02 {
proxy_pass https://origin_city02_servers;
proxy_redirect off;

proxy_cache_valid 200 1d;
proxy_cache my_cache;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}


Here's the response I get when I query the first cluster called "origin_city01_servers". If you notice, I ask 3 times, and when it reaches the IP address "XXX.XXX.XXX.XXX:443", it finds the resource. But then, if I ask the weighted cluster again, it sends me to the next server and returns a 302 with the location of the URL of the server with the IP "XXX.XXX.XXX.XXX:443".


root@varnish-cache-01:/etc/nginx/sites-available# curl -I https://live.domain.com/channel01/index.m3u8
HTTP/1.1 200 OK
Server: nginx/1.22.0 (Ubuntu)
Date: Tue, 23 Apr 2024 13:53:43 GMT
Content-Type: application/vnd.apple.mpegurl
Content-Length: 740
Connection: keep-alive
X-Route-Time: 1520
X-Run-Time: 263
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, DELETE, OPTIONS
Access-Control-Expose-Headers: Server, range, X-Run-Time, X-Sid, Content-Length, Location
Access-Control-Allow-Headers: x-vsaas-session, x-no-redirect, origin, authorization, accept, range, content-type, x-add-effective, session, x-originator, x-sid
X-Sid: 6627bd67-2e34-4704-9536-e7682d617b54
Cache-Control: no-cache
Pragma: no-cache
X-Media-Info-Time: 78
X-Prepare-Time: 178
Set-Cookie: XXX.XXX.XXX.XXX:443

root@varnish-cache-01:/etc/nginx/sites-available# curl -I https://live.domain.com/channel01/index.m3u8
HTTP/1.1 302 Found
Server: nginx/1.22.0 (Ubuntu)
Date: Tue, 23 Apr 2024 13:53:44 GMT
Content-Length: 60
Connection: keep-alive
X-Route-Time: 145
X-Run-Time: 39
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, DELETE, OPTIONS
Access-Control-Expose-Headers: Server, range, X-Run-Time, X-Sid, Content-Length, Location
Access-Control-Allow-Headers: x-vsaas-session, x-no-redirect, origin, authorization, accept, range, content-type, x-add-effective, session, x-originator, x-sid
Location: https://origin02.domain.com/channel01/index.m3u8
Set-Cookie: ZZZ.ZZZ.ZZZ.ZZZ:443

root@varnish-cache-01:/etc/nginx/sites-available# curl -I https://live.domain.com/channel01/index.m3u8
HTTP/1.1 302 Found
Server: nginx/1.22.0 (Ubuntu)
Date: Tue, 23 Apr 2024 13:53:44 GMT
Content-Length: 60
Connection: keep-alive
X-Route-Time: 145
X-Run-Time: 39
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, DELETE, OPTIONS
Access-Control-Expose-Headers: Server, range, X-Run-Time, X-Sid, Content-Length, Location
Access-Control-Allow-Headers: x-vsaas-session, x-no-redirect, origin, authorization, accept, range, content-type, x-add-effective, session, x-originator, x-sid
Location: https://origin02.domain.com/channel01/index.m3u8
Set-Cookie: CCC.CCC.CCC.CCC:443
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 180
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready