title: Enabling CORS for VueJS devserver
body: <p>My setup is the following:</p>
  <ul>
  <li>Custom DNS server pointing a custom TLD to localhost</li>
  <li>nginx as a reverse proxy</li>
  <li>golang backend</li>
  <li>VueJS front-end through vue-cli</li>
  </ul>
  <p>What I want is to have both my api and my frontend on sub.domain.tld where /api is reverse-proxying to my API and everything else to go to my vuejs dev server.</p>
  <p>The current nginx config I have is working for my need but it prevents the vue devserver to connect:</p>
  <pre><code>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' &quot;*&quot; 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 &quot;Access-Control-Allow-Origin&quot;  &quot;*&quot; always;
                          add_header &quot;Access-Control-Allow-Methods&quot; &quot;GET, POST, OPTIONS, HEAD&quot;;
                          add_header &quot;Access-Control-Allow-Headers&quot; &quot;Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Refresh-Token&quot;;
                          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 &quot;Access-Control-Allow-Origin&quot;  &quot;*&quot; always;
                          add_header &quot;Access-Control-Allow-Methods&quot; &quot;GET, POST, OPTIONS, HEAD&quot;;
                          add_header &quot;Access-Control-Allow-Headers&quot; &quot;Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Refresh-Token&quot;;
                          return 200;
                  }
  
                  proxy_pass http://localhost:8935;
                  proxy_set_header Host $host;
                  proxy_set_header X-Real-IP $remote_addr;
          }
  }
  
  </code></pre>
  <p>And the vue.config.js:</p>
  <pre><code>module.exports = {
          devServer: {
                  disableHostCheck: true,
                  proxy: 'https://sub.domain.tld/'
          }
  }
  </code></pre>
  <p>The only problem is that I still get errors from I guess the live error displaying / live refresh included in vue-cli (In french sorry, but it's basically a CORS error message):</p>
  <pre><code>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.
  </code></pre>