You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx反向代理Express时出现Cannot GET错误的问题咨询

Fixing "Cannot GET" Error with Nginx Reverse Proxy for Node.js Mail Server

Let's walk through the most common fixes for this issue—since you've already set up HTTPS for your sites and are trying to proxy requests to your non-HTTPS Node server, the "Cannot GET" error almost always boils down to routing mismatches between Nginx and Express, or misconfigured proxy settings.


1. Fix Your Nginx Reverse Proxy Configuration

The biggest culprit here is usually missing or misplaced trailing slashes in your proxy_pass directive. Here's a correct example to add to your existing HTTPS site's Nginx server block:

# Inside your HTTPS server block for the site that needs to call the mail server
location /mail-api/ {
    # Forward requests to your Node server's address/port
    proxy_pass http://localhost:3000/;
    # Important headers to pass along to Express
    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_set_header X-Forwarded-Proto $scheme;  # Tells Express the original request was HTTPS
}

Key Note: The Trailing Slash

If your location ends with /, your proxy_pass must also end with /. This tells Nginx to strip the /mail-api/ prefix from the request path before forwarding it to Express. Without the trailing slash, Nginx would send the full path (e.g., /mail-api/send instead of just /send), which your Express server doesn't have a route for—hence the "Cannot GET" error.


2. Verify Your Express Routes Match the Forwarded Paths

Make sure your Express routes are listening for the paths that Nginx is sending. For example:

  • If Nginx forwards /mail-api/send to /send (thanks to the trailing slash), your Express route should be:
    app.post('/send', (req, res) => {
        // Your mail sending logic here
        res.status(200).json({ success: true });
    });
    
  • If you accidentally set up Express routes with the /mail-api/ prefix, you'd need to remove the trailing slash from proxy_pass (but the first approach is cleaner, keeping your Node server agnostic of the proxy prefix).

3. Confirm Your Node Server is Listening Correctly

Double-check that your Express server is listening on the right address and port (matching what's in your Nginx proxy_pass):

// Make sure you're listening on localhost (or 0.0.0.0 for broader access)
app.listen(3000, 'localhost', () => {
    console.log('Mail server running on http://localhost:3000');
});

Avoid listening only on a public IP unless necessary—localhost is safer for internal proxy requests.


4. Test the Proxy Directly

To isolate whether the issue is with Nginx or Express, run this curl command directly on your Droplet:

curl http://localhost/mail-api/send -X POST -H "Content-Type: application/json" -d '{"to":"test@example.com"}'
  • If you get a 404 here: Check your Nginx config (run sudo nginx -t to validate syntax, then sudo nginx -s reload to apply changes) and your Express routes.
  • If you get a successful response: The proxy is working, and the issue is with your frontend's request path (make sure it's calling https://your-site.com/mail-api/send instead of the old non-HTTPS Node server address).

5. Optional: Handle CORS (If Needed)

While this isn't directly related to the "Cannot GET" error, if you run into CORS issues after fixing the proxy, add a CORS middleware to Express:

const cors = require('cors');
app.use(cors({ origin: 'https://your-site.com' })); // Restrict to your HTTPS site

内容的提问来源于stack exchange,提问作者milo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:03:40