Nginx反向代理配置-找不到404

时间:2019-03-09 16:32:56

标签: node.js express nginx http-status-code-404 reverse-proxy

我已经在远程服务器(数字海洋)上部署了React应用,并且我希望能够从React App客户端访问服务器的本地主机。

我尝试使用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'))})
})

这就是我从React应用程序中获取的方式

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>

1 个答案:

答案 0 :(得分:1)

问题是您使用的是IP地址,而不是您在server_name伪指令中设置的域。因此,它将使用默认的Nginx配置,而不是使用该站点配置。

尝试:

fetch('https://jonasgroenbek.com/game/play/rock')

您还可以放置:

    rewrite /game/(.*)  /$1 break;