在 Nginx 中使用代理传递时 Express 上“无法获取”

bon*_*onk 4 ssl https nginx node.js express

我在 DigitalOcean Droplet 上运行两个网站,现在我已将这两个网站升级为 https。在其中一个站点上,我正在向我在那里运行的节点服务器发出请求以发送电子邮件,但由于我启用了 https,所以它会阻止此请求,因为它不是 https(逻辑上)。因此,我尝试使用节点邮件服务器作为启用了 https 的站点之一的代理传递。

为了测试,我想要代理的节点服务器如下所示:

exports.start = () => {
    app.use(bodyParser.json());
    app.use(cors());
    app.get('/', (req, res) => res.send('Hello World!'));
    app.listen(4004, () => console.log('Server listening on port 4004'));
};

this.start();
Run Code Online (Sandbox Code Playgroud)

我的 Nginxsites-enabled/default看起来像这样:

server {

server_name mywebsite.nl www.website.nl;

location / {
    proxy_pass http://<PIVATE IP>:4000;
    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;
}

location /ws {
    proxy_pass http://<PIVATE IP>:4002;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
}


location /mailmeister {
            proxy_pass http://<PIVATE IP>:4004;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
}


listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mywebsite.nl/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mywebsite.nl/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

}
Run Code Online (Sandbox Code Playgroud)

所以这/mailmeister是我想将所有请求重定向到节点服务器的地方。当我跑步curl localhost:4004curl PRIVATE IP:4004得到预期的 Hello World!反应时。但是当我运行时curl https://mywebsite.nl/mailmeister它会返回一个网页,上面写着

Cannot GET /mailmeister
Run Code Online (Sandbox Code Playgroud)

有人知道怎么修这个东西吗?

Nag*_*gev 5

简而言之,因为您有:

location /mailmeister {
            proxy_pass http://<PIVATE IP>:4004;
}
Run Code Online (Sandbox Code Playgroud)

应该这样处理:

app.get('/mailmeister ', (req, res) => res.send('Hallo World!'));
Run Code Online (Sandbox Code Playgroud)

未测试此特定设置。有一个相关的答案对此进行了进一步解释。一旦你得到它,文档就更有意义了。