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?










share|improve this question
























  • 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

















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?










share|improve this question
























  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 adding if ($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












  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?