V
V
Viktor2020-09-05 10:43:00
Nginx
Viktor, 2020-09-05 10:43:00

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;
        }
}


but neither get nor post requests go through. Not from the browser, not through insomnia.
I get an error in the browser
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.


on the cors back, as soon as I didn’t try to connect, I also tried to send headers in the response.
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));


tried that too:
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);


It seems to me that I have already tried all the options from Google, but I can not get through to node from the network.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2020-09-05
@notiv-nt

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/;
  }
}

node
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);

Well, the request
(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 question

Ask a Question

731 491 924 answers to any question