【问题标题】:Enabling CORS for VueJS devserver为 VueJS 开发服务器启用 CORS
【发布时间】:2021-04-12 10:05:24
【问题描述】:

我的设置如下:

  • 自定义 DNS 服务器将自定义 TLD 指向本地主机
  • nginx 作为反向代理
  • golang 后端
  • 通过 vue-cli 实现 VueJS 前端

我想要的是将我的 api 和我的前端都放在 sub.domain.tld 上,其中 /api 反向代理到我的 API,其他一切都转到我的 vuejs 开发服务器。

我当前的 nginx 配置可以满足我的需要,但它会阻止 vue devserver 连接:

server {
        listen      80;
        server_name sub.domain.tld;
        return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl http2;
        server_name sub.domain.tld;

        ssl_certificate /opt/ssl/domain.tld.pem;
        ssl_certificate_key /opt/ssl/domain.tld-key.pem;

        include snippets/ssl.conf;

        access_log /tmp/nginx.log combined;

        add_header 'Access-Control-Allow-Origin' "*" always;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,X-Refresh-Token';

        location /auth {
                if ($request_method = OPTIONS ) {
                        add_header "Access-Control-Allow-Origin"  "*" always;
                        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
                        add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Refresh-Token";
                        return 200;
                }

                proxy_pass http://localhost:2394;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
        }

        location / {
                if ($request_method = OPTIONS ) {
                        add_header "Access-Control-Allow-Origin"  "*" always;
                        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
                        add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Refresh-Token";
                        return 200;
                }

                proxy_pass http://localhost:8935;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
        }
}

还有 vue.config.js:

module.exports = {
        devServer: {
                disableHostCheck: true,
                proxy: 'https://sub.domain.tld/'
        }
}

唯一的问题是我仍然收到错误,我猜 vue-cli 中包含的实时错误显示/实时刷新(法语很抱歉,但它基本上是一条 CORS 错误消息):

Blocage d’une requête multiorigines (Cross-Origin Request) : la politique « Same Origin » ne permet pas de consulter la ressource distante située sur https://192.168.1.12:8935/sockjs-node/info?t=1609950092054. Raison : échec de la requête CORS.

【问题讨论】:

    标签: vue.js nginx webpack vue-cli devserver


    【解决方案1】:

    所以最后我得到了它使用以下设置:

    我让 vue-cli / webpack 知道我的浏览器将从给定的 url 访问:

    vue.config.js

    module.exports = {
      publicPath: '/app/', // Only required if the app is not at the root
      devServer: {
        "public": "sub.domain.tld"
      }
    }
    

    那么简单来说就是正确配置nginx的一个案例。在这种情况下不需要 CORS,因为所有内容都来自同一个域。

    server {
            listen      80;
            server_name sub.domain.tld;
            return 301 https://$server_name$request_uri;
    }
    
    server {
            listen 443 ssl http2;
            server_name sub.domain.tld;
    
            ssl_certificate /opt/ssl/domain.tld.pem;
            ssl_certificate_key /opt/ssl/domain.tld-key.pem;
    
            include snippets/ssl.conf;
    
            access_log /tmp/nginx.log combined;
    
            # My golang backend
            location / {
                proxy_pass http://localhost:1635;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header Host $host;
            }
    
            # My VueJS app
            location /app {
                proxy_pass http://localhost:8080;
            }
    
            # Access for webpack WS for debug
            location /sockjs-node {
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header Host $host;
            }
    
            # Since I'm building my app on a sub-path I want the root to redirect to it
            location = / {
                return 301 $scheme://$host/app;
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 2019-01-28
      • 2015-10-14
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2014-08-28
      相关资源
      最近更新 更多