documentation icon indicating copy to clipboard operation
documentation copied to clipboard

Add optional CORS settings to nginx-reverse-proxy

Open schwamic opened this issue 3 years ago • 7 comments

Since the prod tutorial didn't work for me due to cors issues, I thought it might be helpful for others if there is an optional cors setting in the docs for an nginx.conf.

I added this cors settings for my environments (code based on enable-cors.org):

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name _;
  location / {
    proxy_pass  http://127.0.0.1:7700;
    # START - Enable CORS
    proxy_hide_header Access-Control-Allow-Origin;
    if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'X-Meili-Api-Key,Access-Control-Allow-Origin,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain; charset=utf-8';
      add_header 'Content-Length' 0;
      return 204;
    }
    if ($request_method != 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'X-Meili-Api-Key,Access-Control-Allow-Origin,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Allow-Credentials' true;
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }
    # END - Enable CORS
  }
}

schwamic avatar Jan 06 '21 10:01 schwamic

Thanks for the suggestion @schwamic ! 😄

dichotommy avatar Feb 01 '21 21:02 dichotommy

@schwamic Thanks a lot, saved my day!

Sharvadze avatar Nov 10 '21 13:11 Sharvadze

@alallema The X-Meili-Api-Key is outdated. Could you please verify if the rest of the settings are up to date? Thanks! 🙏

maryamsulemani97 avatar Mar 30 '23 15:03 maryamsulemani97

I ran into the exact same problem with my Laravel forge deployed Meilisearch Instance. This is how my location block now looks like:

location / {
  proxy_hide_header Access-Control-Allow-Origin;
  
  if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' 'https://meilisearch-ui.riccox.com';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' 'x-meilisearch-client,Authorization,Content-Type';
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
    
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    return 204;
  } 
  
  if ($request_method != 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' 'https://meilisearch-ui.riccox.com';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' 'x-meilisearch-client,Authorization,Content-Type';
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
  }
  proxy_pass http://127.0.0.1:7700;
}

internetztube avatar Sep 17 '23 13:09 internetztube

Hello @internetztube

The link in this issue is outdated, here is the new one: https://www.meilisearch.com/docs/learn/cookbooks/running_production#a-quick-introduction

Also, the issues of this repository are focused on the maintenance of the Meilisearch documentation. If you want direct support, I recommend asking your question on our Discord. People will be there to help you

curquiza avatar Sep 18 '23 08:09 curquiza

I just left this comment here to help other folks who are stumbling across the same issue. Regarding the documentation, I cannot find any resources that mention CORS.

site:meilisearch.com "cors"

internetztube avatar Sep 18 '23 11:09 internetztube

PRs are welcome to fix documentation about this ❤️

curquiza avatar Oct 31 '23 10:10 curquiza