简体   繁体   English

Vue 不会通过 NGINX 与 Express 通信

[英]Vue will not communicate with Express through NGINX

I have set up a Vue frontend and Express backend on a DigitalOcean droplet and I'm using NGINX to get them to talk through an API.我在 DigitalOcean 液滴上设置了 Vue 前端和 Express 后端,我正在使用 NGINX 让他们通过 API 交谈。 The frontend works, but when I try to access an API route I get a "CONNECTION_REFUSED" error in the console.前端工作正常,但是当我尝试访问 API 路由时,我在控制台中收到“CONNECTION_REFUSED”错误。 The error points to axios in webpack.错误指向 webpack 中的 axios。 I think I have NGINX configured properly (see code below) and when I access the API route with curl it works.我想我已经正确配置了 NGINX(请参见下面的代码),当我使用 curl 访问 API 路由时,它可以工作。

On the frontend I get axios to send a request to the backend which then returns json data.在前端,我得到 axios 向后端发送请求,然后返回 json 数据。

Question 1: If NGINX is now handling the communication between Vue and Express, am I correct in thinking that I can get rid of axios, as when an API request is made NGINX decides what to do with it (sends it to the backend and then sends the respose to the client) which effectively bypasses axios communicating with Express? Question 1: If NGINX is now handling the communication between Vue and Express, am I correct in thinking that I can get rid of axios, as when an API request is made NGINX decides what to do with it (sends it to the backend and then将响应发送给客户端)有效绕过与 Express 通信的 axios? Here's the axios code (api.js simply imports axios and sets the base url):这是 axios 代码(api.js 只需导入 axios 并设置基本 url):

import api from '@/services/api'

export default {
  fetchProjects () {
    return api().get('projects')
  },
  fetchProjectById (id) {
    return api().get(`projects/${id}`)
  }

Question 2: I am using https on the frontend but communicating with backend through http.问题 2:我在前端使用 https,但通过 http 与后端通信。 On Firefox I am getting "CORS request did not succeed" - I vaguely remember reading somewhere that https to http could cause problems with CORS. On Firefox I am getting "CORS request did not succeed" - I vaguely remember reading somewhere that https to http could cause problems with CORS. Could this be the issue?这可能是问题吗? If so, can I use the same SSL cert for the backend and frontend?如果是这样,我可以为后端和前端使用相同的 SSL 证书吗? Do I have to change anything in the NGINX config?我是否必须更改 NGINX 配置中的任何内容?

I've been at this for days and I just can't figure it out for the life of me.我一直在这几天,我只是无法为我的生活弄明白。 Any help would be greatly appreciated.任何帮助将不胜感激。

upstream backend {
server 127.0.0.1:3000;
keepalive 8;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    root /var/www/html/PersonalWebsite/frontend/dist;

    index index.html index.htm index.nginx-debian.html;

    server_name website.com www.website.com;
    ssl_certificate <link-to-cert>;
    ssl_certificate_key <link-to-key>;
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    # Redirect to https
    if ($scheme != "https") {
            return 301 https://$host$request_uri;
    } # managed by Certbot

    location / {

            add_header 'Access-Control-Allow-Origin' 'http://localhost:8080' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
            add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin' 
            always;
            try_files $uri $uri/ =404;


    }

    location /api/ {
            add_header 'Access-Control-Allow-Origin' 'http://localhost:3000' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
            add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin' 
            always;

            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_pass http://backend;

    }

} }

error probably because of "cors" but have you tried with "postman"?错误可能是因为“cors”,但你试过用“postman”吗? this links can help you https://www.npmjs.com/package/cors#usage这个链接可以帮助你https://www.npmjs.com/package/cors#usage
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM