[英]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.