[英]Nginx reverse proxy config - 404 not found
我已經在遠程服務器(數字海洋)上部署了一個 React 應用程序,我希望能夠從 React 應用程序客戶端訪問服務器的本地主機。
我試過用 Nginx 設置反向代理,但是我得到了一個可怕的 404。
該站點已部署, https://www.jonasgroenbek.com 。 當您選擇石頭、剪刀或紙,然后選擇與 AI 戰斗的按鈕時,會在游戲部分下出現問題。
這是我在 Nginx 中啟用的站點
server {
root /var/www/jonasgroenbek.com/build;
index index.html;
server_name jonasgroenbek.com www.jonasgroenbek.com;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/jonasgroenbek.com/fullchain.pem; # managed b$
ssl_certificate_key /etc/letsencrypt/live/jonasgroenbek.com/privkey.pem; # managed$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location /game {
rewrite /game/(.*) /$1 break;
proxy_pass http://localhost:1234;
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;
}
}
server {
if ($host = www.jonasgroenbek.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = jonasgroenbek.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name jonasgroenbek.com www.jonasgroenbek.com;
return 404; # managed by Certbot
}
這是 node.js express 服務正在偵聽的端口和路徑:
app.listen(PORT, function(){
console.log(`listening on port:${PORT}...`)
})
app.get("/game/play/:choice", function(req,res){
pythonProcess = spawn('python',["./script.py", req.params.choice]);
pythonProcess.stdout.on('data', function(data) {
res.status(200).send(data.toString('utf-8'))})
})
這就是我從反應應用程序中獲取的方式
fetch(`104.248.28.88/game/play/rock`)
是要檢測的問題,因為我正在慢慢失去理智。
編輯
我都試過
fetch("104.248.28.88/game/play/rock")
產生此錯誤消息: Game.js:46 GET https://jonasgroenbek.com/jonasgroenbek.com/game/play/rock 404 (Not Found)
並fetch("jonasgroenbek.com/play/rock")
這會產生此錯誤消息:
Game.js:46 GET https://jonasgroenbek.com/jonasgroenbek.com/game/play/rock 404 (Not Found)
嘗試通過郵遞員訪問它
jonasgroenbek.com/game/play/rock
給出以下錯誤信息:
<html>
<head>
<title>404 Not Found</title>
</head>
<body bgcolor="white">
<center>
<h1>404 Not Found</h1>
</center>
<hr>
<center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
問題是您使用的是 IP 地址,而不是您在server_name
指令上設置的域。 因此,不是使用該站點配置,而是使用默認的Nginx
配置。
嘗試:
fetch('https://jonasgroenbek.com/game/play/rock')
您還可以刪除:
rewrite /game/(.*) /$1 break;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.