Answer the question
In order to leave comments, you need to log in
Redirecting Ajax requests using nginx
Good afternoon! I'm trying to organize the creation of 3rd level domains for certain users (user1.site.ru should work like site.ru/user/1). Faced the problem of paths on subdomains (because the links in the project are all relative).
I'm trying to use the web server to determine Ajax requests and redirect them to the usual URL (i.e. user1.site.ru/ajax/123 should work the same as site.ru/ajax/123)
Here is a piece of the config for a specific subdomain (let's 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;
}
Answer the question
In order to leave comments, you need to log in
Try dropping the if - it doesn't always work in the obvious way. You need something like this:
resolver 127.0.0.1;
location / {
proxy_pass http://www.site.ru/user/1/;
}
location /ajax/ {
proxy_pass http://www.site.ru/ajax/
}
It worked for me like this:
server{
...
if ($http_x_requested_with = XMLHttpRequest) {
rewrite ^(.*)$ /ajax/$1 last;
}
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question