简体   繁体   English

如何从配置了 nginx 并配置了 ssl 的 React 应用程序向 nodejs 后端发送请求

[英]How to send requests to a nodejs backend from a React app served by nginx with ssl configured

I have generated static files for a reactjs app using create react app tool.我使用 create react app 工具为 reactjs 应用程序生成了 static 文件。 I then started an nginx server on a docker container to serve the front end built using reactjs.然后,我在 docker 容器上启动了 nginx 服务器,为使用 reactjs 构建的前端提供服务。

The server is interacting with a node js in a different container.服务器正在与不同容器中的节点 js 交互。 The application was working fine until I integrated an ssl certificate to Nginx.在我将 ssl 证书集成到 Nginx 之前,该应用程序运行良好。

I started to have this error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://ip_address:8000/api_endpoint.我开始遇到此错误Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://ip_address:8000/api_endpoint. Both containers are hosted on the same machine.两个容器都托管在同一台机器上。

here is configuration file for Nginx:这是 Nginx 的配置文件:

server {
        #listen [::]:80;
        server_name domain_name.com www.domain_name.com;
        root /var/www/react_app/html;
        index index.html;

        #server_name affiliates-pal.com;
        access_log /var/log/nginx/react-app.access.log;
        error_log /var/log/nginx/react-app.error.log;
       location / {
               try_files $uri /index.html;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain_name.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain_name.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.domain_name.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = domain_name.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80;
        server_name domain_name.com www.domain_name.com;
    return 404; # managed by Certbot

}

Should I add anything to CORS defintion in the following startup script for node back end?我应该在以下节点后端启动脚本中的 CORS 定义中添加任何内容吗?

const express = require("express");
const bodyparser = require("body-parser");

var app = express();
var cron = require("node-cron");
var sleep = require("system-sleep");

const usersRouter = require("./api-routes");


if(process.env.IP_ADDRESS)
{
    IP_ADDRESS = "http://"+process.env.IP_ADDRESS

}
else
{
    IP_ADDRESS= "http://localhost:3000"
} 
console.log("IP ADDRESS FROM GLOBAL VAR")
console.log(process.env.IP_ADDRESS)
console.log(IP_ADDRESS)

app.use(
  bodyparser.urlencoded({
    extended: true,
  })
);
app.use(bodyparser.json());

var cors = require("cors");
app.use(
  cors({
    origin: [IP_ADDRESS],
    credentials: true,
  })
);

//"http://localhost:3000"
app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", IP_ADDRESS);
  res.header("Access-Control-Allow-Headers", true);
  res.header("Access-Control-Allow-Credentials", true);
  res.header(
    "Access-Control-Allow-Methods",
    "GET, POST, OPTIONS, PUT, PATCH, DELETE"
  );
  next();
});

app.use("/", usersRouter);

cron.schedule("40 11 10 * * *", () => {
  console.log("before sleep");
  sleep(20000);
  console.log("after sleep");
});

const port = process.env.PORT || 8000;
app.listen(port, () => console.log(`Listening on port ${port}..`));

Should I add a proxy configuration for nginx as suggested in this answer ?我是否应该按照此答案中的建议为 nginx 添加代理配置?

We have the same setup and we dont have issues with CORS.我们有相同的设置,CORS 没有问题。 Nginx config troubles me. Nginx 配置让我很困扰。 Can you please try this?你能试试这个吗?

server {

        root /path/to/your/static/files;
        index index.html index.htm index.nginx-debian.html;

        server_name subdomain.domain.com(or domain.com); # CHANGE THE NAME UNDER REDIRECT BELOW AS WELL
        location / {

                try_files $uri $uri/ /index.html;
        }

    listen 443 ssl;
    ssl_certificate /path/to/your/cert/fullchain.pem;
    ssl_certificate_key /path/to/your/key/privkey.pem;
}
server {
    if ($host = subdomain.domain.com(or domain.com)) {
        return 301 https://$host$request_uri;
    }



        server_name subdomain.domain.com(or domain.com);
    listen 80;
    return 404;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 图像未从 nodejs 后端正确提供 - Angular image not served properly from nodejs backend 无法从 React Native 移动应用程序向 Localhost 后端发送 HTTP 请求 - Can't send HTTP requests to Localhost Backend from React Native Mobile App 如何从nodejs后端检索图像以做出反应 - How to retrieve image from nodejs backend to react 如何从后端(nodejs)重定向到前端(react)? - How to redirect from backend (nodejs) to frontend (react)? Nginx 配置 ssl 证书 - Nginx configured ssl certificate Nginx 如何处理来自 nodejs 作为客户端的传出请求? - How Nginx handles outgoing requests going from nodejs as a client? 如何在 React Page 上显示从后端(ExpressJS)提供的 static 文件? - How to display static files served from backend (ExpressJS) onto React Page? 如何在子域中的同一目录中部署 React 应用程序和 Nodejs 后端? - How to deploy React app and Nodejs backend on the same directory in the subdomain? 将 dockerized Nginx 上的 React App 连接到 Express 服务器 - Connect React App served on dockerized Nginx to Express server 如何从后端(快递)在nodeJS中创建html文件并将其发送到客户端? - How to create and send html file to client in nodeJS from backend(express)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM