Score:0

Why does nginx proxy redirect the request?

pa flag

I'm trying to pass the API requests to the backend with NGINX reverse proxy.

When I send a get request to /api/testdata, it sends a request to {frontendURL}/api/testData which returns with 301(redirect) status code. After that It redirects the request to the correct url {backendURL}/api/testData.

Network tab network tab 1 network tab 2 network tab 3

nginx config:

server {
    listen 80 default_server;
    listen [::]:80 default_server;



    root /usr/share/nginx/html;

    server_name _;

    location /health {
            access_log off;
            add_header 'Content-Type' 'application/json';
            return 200 '{"status":"Healthy"}';
    }

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

    location /api {
        proxy_pass http://localhost:3001; 
        proxy_redirect off;
    }

Can I somehow prevent the redirect request so that only one request goes out? Or is this the normal behaviour?

djdomi avatar
za flag
either tell the real url or share the output of curl
szgezu avatar
pa flag
@djdomi I can send some picture about the network tab. [Redirected request](https://i.stack.imgur.com/8Rq8b.png) [Request to the redirected url](https://i.stack.imgur.com/5OUpN.png) [Response](https://i.stack.imgur.com/60S8J.png)
szgezu avatar
pa flag
If I use curl without -L on {frontendUrl}/api/testdata, it doesn't send back anything because the request is just redirected. With -L, it sends the data back correctly.
br flag
You first request goes to `gwk-frontend`. I guess it's some other server.
djdomi avatar
za flag
remind to share the output by edit the question instead of using the commantary section ;)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.