Redirect Ajax requests funds nginx

Good day! Trying to organize the creation of the domains of the 3rd level for certain users (user1.site.ru must work out how site.ru/user/1). Faced with the problem of ways on subdomains (as links in the project all relative).
Trying by web servers to identify Ajax requests and redirect them via the normal url (i.e. user1.site.ru/ajax/123 must work well as site.ru/ajax/123)

Here is a piece of config for a specific subdomain (say user1)

location / {
 resolver 127.0.0.1;
 if ($http_x_requested_with = XMLHttpRequest) {
 set $pass1 http://www.site.ru/;
}
 if ($http_x_requested_with != XMLHttpRequest) {
 set $pass1 http://www.site.ru/user/1/;
}
 proxy_pass $pass1;
}


Ajax requests work correctly.
Please help in writing validation for Ajax request.
October 3rd 19 at 02:42
3 answers
October 3rd 19 at 02:44
Try to give up if it doesn't always work in an obvious way. Need something like:

resolver 127.0.0.1;

location / {
 proxy_pass http://www.site.ru/user/1/;
}

location /ajax/ {
 proxy_pass http://www.site.ru/ajax/
}


Although, to do proxy_pass to your own server...
This variant I already have.
Unfortunately, it is not suitable, because of the 3 only 1 of the programmers were not lazy and all Ajax-requests via post /ajax, the rest of the titles are hit and miss, something in the spirit site.ru/blogs/create/blogName.
Because using location I can wrap approximately only a third of the queries. That and trying to write a check for everything Ajax. - Ericka_Hegmann55 commented on October 3rd 19 at 02:47
October 3rd 19 at 02:46
I have just worked:

server{
...
if ($http_x_requested_with = XMLHttpRequest) {
 rewrite ^(.*)$ /ajax/$1 last;
}


if the inside location, then replace last with break

nginx.org/ru/docs/http/ngx_http_rewrite_module.html
I think this will only work for queries in the url which is "/ajax".
I have requests may be by any means.
Ajax request
user1.site.ru/blogs/create/asdasdasd
should work as well as
site.EN/blogs/create/asdasdasd - Ericka_Hegmann55 commented on October 3rd 19 at 02:49
This will work for all Ajax requests - Ericka_Hegmann55 commented on October 3rd 19 at 02:52
Imagine request
user1.site.ru/ajaxQuery/param1/param2

Rewrite wrap on it
user1.site.ru/ajax/ajaxQuery/param1/param2

Right?

I need to get turned on
site.ru/ajaxQuery/param1/param2

Lookup your example gave me 404

location / {
 if ($http_x_requested_with = XMLHttpRequest) {
 rewrite ^(.*)$ /ajax/$1 break;
}
}
- Milo61 commented on October 3rd 19 at 02:55
I.e. on the main domain - Ericka_Hegmann55 commented on October 3rd 19 at 02:58
October 3rd 19 at 02:48
nginx.org/ru/docs/http/ngx_http_map_module.html

map $http_x_requested_with $passvar {
 default http://www.site.ru/user/1/;
 XMLHttpRequest http://www.site.ru/;
}

resolver 127.0.0.1;

server {
 location / {
 proxy_pass $passvar;
}
}
room user also can sumapit from the host and proxies to pass 2 variables - Ericka_Hegmann55 commented on October 3rd 19 at 02:51

Find more questions by tags Nginx