我真的很喜欢Nginx,我不知道如何正确配置nginx
,我的项目文件夹是
/Client (react)
/Server (node)
/ (server config)
我之前有这个nginx
配置,并且一切正常。
server {
listen 80;
server_name IP;
location / {
proxy_pass http://IP:3030;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
后来在Cloudflare
上,我仅激活 https ,我的网站无法连接到端口:4000上的后端,根据一些研究,我发现我需要更改nginx
的配置,这就是Cloudflare
指导我创建的内容。
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /root/certificate.pem;
ssl_certificate_key /root/key.key;
server_name mydomain.org www.mydomain.org;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / { # i just paste everything i had on the previous config here
proxy_pass https://IP:3030;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
因此,基本上,网站已加载,但无法处理对后端的请求。
如果您不知道为什么我有/server
文件夹和/
文件夹,这是我的server.js
文件:
const express = require("express");
const path = require("path");
const app = express();
const port = process.env.PORT || 3030;
app.use(express.static(path.join(__dirname, "client/build")));
app.get("/*.png", (req, res) => {
console.log(req.params[0]);
res.sendFile(path.join(__dirname, "client/public/", req.params[0] + ".png"));
});
app.get("/*.jpeg", (req, res) => {
console.log(req.params[0]);
res.sendFile(path.join(__dirname, "client/public/", req.params[0] + ".jpeg"));
});
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build/", "index.html"));
});
app.listen(port, () => console.log(`Listening on port ${port}`));
很抱歉,很长的帖子,但是我对nginx
一无所知,我想报道几乎所有的东西。
答案 0 :(得分:1)
也许您的后端服务器没有准备好处理SSL请求。您可以让NGINX为您处理该问题,并与您之前未使用SSL的后端进行通信。您可以通过将proxy_pass
指令改回http://
地址来实现。
server {
# Other stuff ...
location / {
# Comunicate proxy with backend without encryption until you setup SSL on your application...
proxy_pass http://IP:3030;
# Other stuff...
}
}