0

I am trying to setup an nginx server to reverse proxy data from two different servers via ajax. Only one of the requests is working however. The other gives me a No 'Access-Control-Allow-Origin' header error.

Running the same front-end that does the ajax requests on a node.js server works however:

"use strict";
let express = require('express');
let request = require('request');

let PORT = process.env.PORT || 3000;
let app = express();

app.use(express.static(__dirname + "/public"));

app.get("/a", (req, res) => {
        let url = "http://external-website-a.com/rest.exe/etc?...";
        request(url).pipe(res);
});

app.get("/b", (req, res) => {
        let url = "http://external-website-b.com/webservice.svc/etc?...";
        request(url).pipe(res);
});

let server = app.listen(PORT, () => {
        console.log("Listening on port: " + PORT);
});

With node.js the client gets both json files, while "b" fails on nginx. Here is my sites-available/default config:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        index index.html

        server_name _;

        location / {
                sendfile off; #http://stackoverflow.com/questions/37742950
                try_files $uri $uri/ /index.html;
        }

        location /a {
                proxy_pass http://external-website-a.com/rest.exe/etc?...;
        }

        location /b {
                proxy_pass http://external-website-b.com/webservice.svc/etc?...;
        }
}

I have been trying to add all the proxy_set_header information I could find on the web.. but nothing has worked so far.. Furthermore when I directly access "localhost:8080/a" I get shown a json (as I should), but when I access "localhost:8080/b" I instead get redirected to "external-website-b.com/webservice.svc/etc?..." even with proxy_redirect off; Why? And does that mean anything related to the issue?

How come my node.js/express server can serve the data, but nginx fails?

0

proxy_set_header Access-Control-allow-Origin "*" . add this in nginx configuration(inside all location directive )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.