A
A
Alexey2020-03-19 12:33:58
Nginx
Alexey, 2020-03-19 12:33:58

Why does a direct link to the site work, but not when proxying through proxy_pass?

There is a site api.site.ru.
I want to set up proxying in nginx, from site.ru to api.site.ru. I write in the config:

server {
  server_name site.ru;
...
  location /api/ {
    proxy_pass http://api.site.ru;
  }
...
}


I make a direct request api.site.ru/api/request , I get code 200, everything is in order.
I give a request site.ru/api/request , I get 502. In the nginx logs:
upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: site.ru, request: "GET /api/request HTTP/1.1", upstream: "http://yyy.yyy.yyy.yyy:80/api/request", host: "site.ru"

What's happening?
What does upstream mean: " http://yyy.yyy.yyy.yyy:80/api/request ", host: "site.ru"? This means that nginx redirects the request to the site.ru host, i.e. to myself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2020-03-19
@alenov

Understood. You had to explicitly set the Host header:
proxy_set_header Host api.site.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question