Skip to content

Instantly share code, notes, and snippets.

@revant
Last active February 12, 2024 10:36
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save revant/4baf8c4dadc9b6b28ff3d85bd049c066 to your computer and use it in GitHub Desktop.
Save revant/4baf8c4dadc9b6b28ff3d85bd049c066 to your computer and use it in GitHub Desktop.
Frappe CORS for nginx
location / {
rewrite ^(.+)/$ $1 permanent;
rewrite ^(.+)/index\.html$ $1 permanent;
rewrite ^(.+)\.html$ $1 permanent;
# Allow CORS for static files
add_header Access-Control-Allow-Origin $cors_origin;
add_header Access-Control-Allow-Methods "GET, OPTIONS";
location ~* ^/files/.*.(htm|html|svg|xml) {
add_header Content-disposition "attachment";
# Allow CORS again, specifically for the above filetypes
add_header Access-Control-Allow-Origin $cors_origin;
add_header Access-Control-Allow-Methods "GET, OPTIONS";
try_files /$site_name/public/$uri @webserver;
}
try_files /$site_name/public/$uri @webserver;
}
location @webserver {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
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 = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Frappe-Site-Name site1.local.com;
proxy_set_header Host $host;
proxy_set_header X-Use-X-Accel-Redirect True;
proxy_read_timeout 120;
proxy_redirect off;
proxy_pass http://frappe-bench-frappe;
}
@revant
Copy link
Author

revant commented Dec 21, 2019

General note on CORS

  • If CORS is enabled your system becomes less secure (allowing wildcard * hosts is not secure)
  • Browser to backend calls can only happen over same origin
  • Non browser calls do not need CORS enabled. i.e. If python/java/php/android app calls frappe/erpnext there won't be any CORS issue.

This gist is not /etc/nginx/nginx.conf, it should be part of <frappe-bench>/config/nginx.conf

If requests fail with CORS error in browser even after enabling CORS on backend, try to call /api/method/version endpoint, if that works, CORS is enabled and error is due to something else. This happens because CORS is only enabled for success responses

@ysedky
Copy link

ysedky commented Nov 3, 2021

Hi Revant,
I m trying to connect to a 3rd party server using a client script.

The calls (from my ERPNEXT to the 3rd party server) get response and they are working fine "only" when I disable the Chrome security.

If I don't disable the browser security I get :

Access to fetch at 'https://*./connect/token' from origin 'https://my*.erpnext*.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I read your notes and I have your standard Docker Swarm installation (single Bench).
I noticed your mentioned "This gist is not /etc/nginx/nginx.conf, it should be part of /config/nginx.conf"

Does this means that I have to login to the frappe-bench-v13_erpnext-nginx container and add these lines or to another container?

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment