Nginx reverse proxy only xhr/ajax requests
up vote
1
down vote
favorite
i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^/api(.*)$ {
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?
node.js nginx
|
show 1 more comment
up vote
1
down vote
favorite
i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^/api(.*)$ {
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?
node.js nginx
Unless you add some data (e.g. a special header) to the XHR request in your client code, the server (e.g. NGINX) cannot distinguish between a request made by browser navigation, client code, or other utilities (e.g. cURL). This isn't bulletproof though, utilities like cURL can be configured to use special headers, unless you're really clever it's not really possible to distinguish between the various possible clients making a request to your server.
– Jake Holzinger
Nov 14 at 22:51
So for example if add header['X-Requested-With'] = 'XMLHttpRequest' for example to the XHR request i can pick it up by the server?
– Abouhassane Abdelhamid
Nov 14 at 22:54
Yes, that's the basic idea, NGINX should have the ability to inspect the headers and conditionally forward the request, or respond with a 404.
– Jake Holzinger
Nov 14 at 23:00
great, i'll look it up. thanks @JakeHolzinger
– Abouhassane Abdelhamid
Nov 14 at 23:03
Are the XHR/AJAX requests using the POST method? You can arrange for the GET method to return a 404 response by addingif ($request_method != POST) { return 404; }
– Richard Smith
Nov 15 at 9:14
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^/api(.*)$ {
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?
node.js nginx
i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^/api(.*)$ {
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?
node.js nginx
node.js nginx
edited Nov 14 at 22:43
asked Nov 14 at 22:36
Abouhassane Abdelhamid
137
137
Unless you add some data (e.g. a special header) to the XHR request in your client code, the server (e.g. NGINX) cannot distinguish between a request made by browser navigation, client code, or other utilities (e.g. cURL). This isn't bulletproof though, utilities like cURL can be configured to use special headers, unless you're really clever it's not really possible to distinguish between the various possible clients making a request to your server.
– Jake Holzinger
Nov 14 at 22:51
So for example if add header['X-Requested-With'] = 'XMLHttpRequest' for example to the XHR request i can pick it up by the server?
– Abouhassane Abdelhamid
Nov 14 at 22:54
Yes, that's the basic idea, NGINX should have the ability to inspect the headers and conditionally forward the request, or respond with a 404.
– Jake Holzinger
Nov 14 at 23:00
great, i'll look it up. thanks @JakeHolzinger
– Abouhassane Abdelhamid
Nov 14 at 23:03
Are the XHR/AJAX requests using the POST method? You can arrange for the GET method to return a 404 response by addingif ($request_method != POST) { return 404; }
– Richard Smith
Nov 15 at 9:14
|
show 1 more comment
Unless you add some data (e.g. a special header) to the XHR request in your client code, the server (e.g. NGINX) cannot distinguish between a request made by browser navigation, client code, or other utilities (e.g. cURL). This isn't bulletproof though, utilities like cURL can be configured to use special headers, unless you're really clever it's not really possible to distinguish between the various possible clients making a request to your server.
– Jake Holzinger
Nov 14 at 22:51
So for example if add header['X-Requested-With'] = 'XMLHttpRequest' for example to the XHR request i can pick it up by the server?
– Abouhassane Abdelhamid
Nov 14 at 22:54
Yes, that's the basic idea, NGINX should have the ability to inspect the headers and conditionally forward the request, or respond with a 404.
– Jake Holzinger
Nov 14 at 23:00
great, i'll look it up. thanks @JakeHolzinger
– Abouhassane Abdelhamid
Nov 14 at 23:03
Are the XHR/AJAX requests using the POST method? You can arrange for the GET method to return a 404 response by addingif ($request_method != POST) { return 404; }
– Richard Smith
Nov 15 at 9:14
Unless you add some data (e.g. a special header) to the XHR request in your client code, the server (e.g. NGINX) cannot distinguish between a request made by browser navigation, client code, or other utilities (e.g. cURL). This isn't bulletproof though, utilities like cURL can be configured to use special headers, unless you're really clever it's not really possible to distinguish between the various possible clients making a request to your server.
– Jake Holzinger
Nov 14 at 22:51
Unless you add some data (e.g. a special header) to the XHR request in your client code, the server (e.g. NGINX) cannot distinguish between a request made by browser navigation, client code, or other utilities (e.g. cURL). This isn't bulletproof though, utilities like cURL can be configured to use special headers, unless you're really clever it's not really possible to distinguish between the various possible clients making a request to your server.
– Jake Holzinger
Nov 14 at 22:51
So for example if add header['X-Requested-With'] = 'XMLHttpRequest' for example to the XHR request i can pick it up by the server?
– Abouhassane Abdelhamid
Nov 14 at 22:54
So for example if add header['X-Requested-With'] = 'XMLHttpRequest' for example to the XHR request i can pick it up by the server?
– Abouhassane Abdelhamid
Nov 14 at 22:54
Yes, that's the basic idea, NGINX should have the ability to inspect the headers and conditionally forward the request, or respond with a 404.
– Jake Holzinger
Nov 14 at 23:00
Yes, that's the basic idea, NGINX should have the ability to inspect the headers and conditionally forward the request, or respond with a 404.
– Jake Holzinger
Nov 14 at 23:00
great, i'll look it up. thanks @JakeHolzinger
– Abouhassane Abdelhamid
Nov 14 at 23:03
great, i'll look it up. thanks @JakeHolzinger
– Abouhassane Abdelhamid
Nov 14 at 23:03
Are the XHR/AJAX requests using the POST method? You can arrange for the GET method to return a 404 response by adding
if ($request_method != POST) { return 404; }
– Richard Smith
Nov 15 at 9:14
Are the XHR/AJAX requests using the POST method? You can arrange for the GET method to return a 404 response by adding
if ($request_method != POST) { return 404; }
– Richard Smith
Nov 15 at 9:14
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309770%2fnginx-reverse-proxy-only-xhr-ajax-requests%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Unless you add some data (e.g. a special header) to the XHR request in your client code, the server (e.g. NGINX) cannot distinguish between a request made by browser navigation, client code, or other utilities (e.g. cURL). This isn't bulletproof though, utilities like cURL can be configured to use special headers, unless you're really clever it's not really possible to distinguish between the various possible clients making a request to your server.
– Jake Holzinger
Nov 14 at 22:51
So for example if add header['X-Requested-With'] = 'XMLHttpRequest' for example to the XHR request i can pick it up by the server?
– Abouhassane Abdelhamid
Nov 14 at 22:54
Yes, that's the basic idea, NGINX should have the ability to inspect the headers and conditionally forward the request, or respond with a 404.
– Jake Holzinger
Nov 14 at 23:00
great, i'll look it up. thanks @JakeHolzinger
– Abouhassane Abdelhamid
Nov 14 at 23:03
Are the XHR/AJAX requests using the POST method? You can arrange for the GET method to return a 404 response by adding
if ($request_method != POST) { return 404; }
– Richard Smith
Nov 15 at 9:14