我正在測驗在代理上運行的快速服務器。我在可用站點中的 nginx 組態檔是:
server {
server_name open.yousico.com;
gzip on;
gzip_proxied any;
gzip_types application/javascript application/x-javascript text/css text/javascript;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 256;
location /_next/static/ {
alias /var/www/yousi-website/.next/static/;
expires 365d;
access_log off;
}
location / {
proxy_pass http://127.0.0.1:3000;
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 /api {
rewrite ^/codify(.*) $1 break;
proxy_pass "http://127.0.0.1:3001";
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/open.yousico.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/open.yousico.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 = open.yousico.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name open.yousico.com;
return 404; # managed by Certbot
}
我在 3001 上監聽的快速服務器的服務器檔案是:
const express = require('express');
const app = express();
app.listen(3001, console.log("server is running on 3001"));
app.get('/', (req, res) => {
res.send('API is running');
});
app.get('/hello', (req, res) => {
res.send('Hello World');
});
我在我的本地機器上測驗了這個快速服務器,它似乎作業正常,但是當我將它部署到我的云服務器時,它顯示“Cannot GET /api”,我的組態檔有問題嗎?我的理解是我將我的位置設定為 /api 并將其定向到埠 3001,并且我確定服務器在 3001 上運行。

uj5u.com熱心網友回復:
當路徑 YYY 沒有方法 XXX 的處理程式時,“Cannot XXX /YYY”是 Express 默認回應。
在您的情況下,我app.get("/api", ...)在您的服務器代碼中看不到處理程式,因此該錯誤是完全有根據的。
nginx 配置中的重寫陳述句,
rewrite ^/codify(.*) $1 break;
也沒有任何意義,因為無法location /api匹配/codify。
如果您打算/api在 URL 傳遞到您的 Express 服務器之前將其洗掉,
rewrite ^/api(.*) $1 break;
在這種情況下/api將被傳遞為/,這將與您的app.get("/")...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511192.html
上一篇:nginx位置塊不回傳靜態內容
