【问题标题】:Nginx reverse proxy config - 404 not foundNginx 反向代理配置 - 404 未找到
【发布时间】:2019-07-31 10:42:10
【问题描述】:

我已经在远程服务器(数字海洋)上部署了一个 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'))})
})

这就是我从 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>

【问题讨论】:

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


    【解决方案1】:

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

    试试:

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

    你也可以放弃:

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

    【讨论】:

    • 在删除重写之前,我曾尝试通过 fetch('jonasgroenbek.com/game/play/rock') 获取,但没有通过。我已经编辑了我的帖子指定。如果删除重写,我将如何访问端点?
    • 去掉重写,请重启nginx。
    • Nginx 特此重新启动 - 如果您在浏览器中查看它或使用邮递员进行测试,则会开启快速服务
    • 现在它正在工作。不再是 Nginx 404,现在你在 express 上有路由问题。但这意味着 Nginx conf 现在是正确的:)
    • 嗯,发生了一些事情,但我仍然收到 404。
    猜你喜欢
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多