Last active
August 27, 2024 13:00
-
-
Save revant/4baf8c4dadc9b6b28ff3d85bd049c066 to your computer and use it in GitHub Desktop.
Frappe CORS for nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
hi
In the nginx config file, in the server level there are additional config files
add_header X-Frame-Options "SAMEORIGIN";
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy "same-origin, strict-origin-when-cross-origin";
Dont we have to chage any of these for the CORS config to allow access ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,