Home nginx NGINX setting, proxy_pass to the subdomain

NGINX setting, proxy_pass to the subdomain

Author

Date

Category

There is a major web application located on a certain Domain Example.ru, and JSON API, located on the subdomain of API.example.ru. The request to the API.example.ru/sitemap dynamically generates an XML site map file. It is necessary that the request to example.ru/sitemap.xml is redirected to this API access point. Not very experienced in setting up NGINX, so difficulties have arisen. Current config (superfluous lowered):

server {
  LISTEN 80;
  Server_Name Example.ru;

  root /home/web/example.ru/www/dist;
  index index.html;

  location = /sitemap.xml {
    proxy_pass "http://api.example.ru/sitemap";
  }

  location ~ \. {
    try_files $ Uri = 404;
  }

  location / {
    Add_header X-Frame-Options "sameorigin";

    proxy_set_header X-FORWARDED-FOR $ proxy_add_x_forwarded_for;
    Proxy_Set_Header Host $ Host;

    proxy_http_version 1.1;
    proxy_pass http: // Localhost: 3000;
    proxy_redirect off;
    proxy_read_timeout 1m;
  }
}

Server {
  LISTEN 80;
  Server_name API.example.ru;

  root /home/web/example.ru/www/api/public;
  index index.php;

  location ~ \. {
    try_files $ Uri = 404;
  }

  location / {
    try_files /index.php = 404;
    include /etc/nginx/fastcgi.conf;
    FastCGI_PASS UNIX: /Run/php/php7.3-fpm.sock;
    fastcgi_index /index.php;
  }
}

With this config, the server redirects an examplephone.ru/sitemap.xml request to a web application, therefore the web application error page is displayed.
The server initially selects the correct location (checked the contents of the contents of the Location = /Sitemap.xml block on the RETURN 200 ‘test’;), but when you processes the request comes to the same SERVER unit, and not to the second corresponding to the subdomain API. *. It now comes with $ URI = / Sitemap, so location / is naturally selected and a web application is displayed.
DNS is configured correctly, and the web application and APIs are available on those domains, which and intelligently, the local CURL request to the API.example.ru/sitemap correctly returns the site map. In general, I obviously misunderstand something in the NGINX configuration. Would be grateful for the help.


Answer 1

If I understand the problem correctly, you need to specify a Host header in the lococreen:

location = / /sitemap.xml {
  proxy_set_header host api.example.ru;
  proxy_pass "http://api.example.ru/sitemap";
}

In your config option, you indicated the address of the processed server, it was cut, but the host left from the initial request, so you hit the server again.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions