Answer the question
In order to leave comments, you need to log in
Why doesn't nginx proxy pass on node js?
I'm trying to set up a small node backend for a website.
CentOS+nginx
In the curl post and get console, requests to the back (on localhost:5000) go through and work fine.
CORS is installed in the project and worked when tested on the computer
In the nginx conf file for the site, I registered proxy and headers
server {
listen 80;
root /var/www/dist;
index index.html;
server_name site.com;
location /back/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header Access-Control-Allow-Credentials 'true';
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
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 Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
}
Access to XMLHttpRequest at 'http://site.com/back/sendemail/' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
var cors = require('cors'); //import cors module
var whitelist = ['*']; //white list consumers
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(null, false);
}
},
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
credentials: true, //Credentials are cookies, authorization headers or TLS client certificates.
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'device-remember-token', 'Access-Control-Allow-Origin', 'Origin', 'Accept']
};
app.use(cors(corsOptions));
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type, Accept,Authorization,Origin");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.setHeader("Access-Control-Allow-Credentials", true);
Answer the question
In order to leave comments, you need to log in
Yes, cors is a pain in the ass
Working option:
Nginx
A
slash to the end is proxy_pass http://127.0.0.1:3000/;
needed to remove /back/
server {
listen 80;
server_name site.localhost;
location /back/ {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_pass http://127.0.0.1:3000/;
}
}
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors());
app.post('/', (req, res) => {
res.json({ 1: 1 });
});
app.listen(3000);
(async () => {
const res = await fetch('http://site.localhost/back/', { method: 'POST' }).then(r => r.json())
console.log(res)
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question