微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Get Request with Axios to Express 返回 HTML 而不是 JSON - 怀疑 NGINX 反向代理设置存在问题

如何解决Get Request with Axios to Express 返回 HTML 而不是 JSON - 怀疑 NGINX 反向代理设置存在问题

当我从 react.js 应用程序向 Express.js 后端获取请求数据时遇到问题,我返回的是 HTML 垃圾,而不是后端应该返回的内容。在调试和研究后,我发现这是因为我的 GET 路由“/order/getTimeSlots”实际上返回了当我去 mywebsite.com/order/getTimeSlots 而不是我的后端时显示的 HTML 内容。我已经在我的开发环境中设置了代理并且它可以工作,但是,它在生产中不起作用。我正在使用 Nginx 来为我的 React 应用程序提供服务,这是我的 Nginx 配置

    server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/mywebsite.com;

    index index.html index.htm index.Nginx-debian.html;

    server_name mywebsite.com www.mywebsite.com;



    location / {
            # First attempt to serve request as file,then
            # as directory,then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            try_files $uri /index.html;
    }



    location /ordersubmit {
    proxy_pass http://localhost:8080; #this is where my backend app is running on
    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 /order/getTimeSlots {
    proxy_pass https://localhost:8080;  #this is where my backend app is running on
    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;


    }
    }

从我的服务器控制台在 localhost:8080/getTimeSlots 上尝试 curl 时,我得到了正确的响应。

我知道我的反向代理设置有问题,但我无法弄清楚问题是什么,所以我想知道这里是否有人可以提供帮助

谢谢大家

解决方法

自己解决了这个问题,我意识到我是在监听端口 80 的服务器下添加这些位置,但是由于我使用的是 https,我应该使用端口 443

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。