我在https://mydomain.co.uk
有一个网站这个表单使用nodemailer、server.js文件和app.js文件来处理表单数据。我一直在学习本教程,以利用nginx作为反向代理。我在我的mydomain.co.uk
服务器块中包含了以下内容
location / {
proxy_pass http://localhost:80;
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;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
}
现在,当我尝试访问https://mydomain.co.uk
时,我遇到了太多重定向错误。
我已经在mydomain.co.uk
上运行了certbot,在我尝试设置反向代理之前,它运行得非常好。
我知道代理配置是我浏览器中出现错误的原因,但我不知道如何设置配置。
我已经看过这个答案,但不知道它如何适用于我正在做的事情。
附加
我有用pm2
运行的server.js
文件,并且我已经用将env设置为生产
pm2 start ecosystem.config.js --env production
ecosystem.config.js
看起来像这个
module.exports = {
apps : [
{
name: "myserver",
script: "server.js",
watch: true,
env: {
"PORT": 3000,
"NODE_ENV": "development"
},
env_production: {
"PORT": 80,
"NODE_ENV": "production",
}
}
]
}
我错过了什么?
编辑
由于按照下面的答案,我的配置文件现在看起来像这个
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
add_header X-Cache-Status $upstream_cache_status;
}
我的server.js文件现在看起来像这个
var path = require("path");
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const PORT = process.env.PORT || 5500; // CHANGE TO WHATEVER
// middleware
var htmlPath = path.join(__dirname, "/html");
// app.use(express.static("public"));
app.use(express.static(htmlPath));
app.use(express.json());
然而,现在express似乎只提供一个html文件,而不是整个html目录。。
反向代理正在工作,因为它是从express服务的,但我需要做什么来确保express服务于整个html目录?
Hi-dude:(你需要代理传递到节点的端口。你现在可以在nginx中不循环地配置端口和指令。然而,nginx发送请求到80,因此,它是默认的,并且再次。。并且它以无限循环的形式重复。
正确指示
server {
listen 80;
listen [::]:80;
server_name mydomain.co.uk;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
# There 3000 port your development nodejs running port
location / {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}