我已經使用 pm2 和 nginx 成功地將 MERN 堆疊網站部署到了 Ubuntu 20.x 服務器。我面臨的問題是手動輸入的 URL 鏈接,或者從我的 Express 路由重定向的鏈接不起作用。這是因為我的 nginx 組態檔設定如下:
location / {
root /root/amfmfx.com/build;
index index.html;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a
# 404.
try_files $uri $uri/ =404;
}
location /user {
proxy_pass http://loadbalancer;
proxy_buffering on;
}
location /api {
proxy_pass http://loadbalancer;
proxy_buffering on;
}
location /email {
proxy_pass http://loadbalancer;
proxy_buffering on;
}
/usr、/api 和 /email 位置是我的后端路由接收器。我在 React 中設定了 Routes 和 Links,點擊后可以完美運行,但是如果我輸入“http://mywebsite.com/login”,它會回傳一個 nginx 錯誤,因為它正在嘗試將 /login 發送到后端顯然。
如何配置我的 nginx Web 服務器以允許前端讀取手動鏈接條目,同時仍向我的 Express 服務器發送路由請求?
謝謝!
uj5u.com熱心網友回復:
由于您有root并且index在您的root位置,任何不匹配的位置都不會有默認的 webroot。我會將您的反應應用程式的配置更改為
server {
// listen, server_name and friends ...
root /root/amfmfx.com/build;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /user/ {
proxy_pass http://loadbalancer;
proxy_buffering on;
}
location /api/ {
proxy_pass http://loadbalancer;
proxy_buffering on;
}
location /email/ {
proxy_pass http://loadbalancer;
proxy_buffering on;
}
}
我會使用尾部斜杠來定義后端服務的位置,例如/api/和/email/。原因是,如果沒有斜杠,nginx 會將您的user位置與傳入請求相 匹配/userbob,與user.
第二,proxy_buffering如果您沒有將其關閉server或按默認設定http水平,則可以將其洗掉。on
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/505006.html
