How to set up CORS (cross-origin resource sharing) in Plesk for Linux?

Follow

Comments

23 comments

  • Avatar
    Alex

    This actually worked for me, had to apply rule to both apache and nginx

    0
    Comment actions Permalink
  • Avatar
    Maghreb Services SARL

    Is this still the best way to enable CORS? In Plesk Obsidian we've been able to enable CORS for both Apache and nginx (as long as Proxy mode is enabled, so not for nginx standalone) by setting additional headers in the Common Apache settings section, with this format:

    Access-Control-Allow-Origin: example1.com

    0
    Comment actions Permalink
  • Avatar
    Ivan Postnikov

    Hello @Maghreb,

    Thank you for sharing your user experience.

    Your solution also works fine as a server-wide one.

    However, the solution specified in the article is more flexible as allows more granular setting per domain.

    0
    Comment actions Permalink
  • Avatar
    Kingsley Felix

    How do we add this when using PLESK OBSIDIAN 

    0
    Comment actions Permalink
  • Avatar
    Denis Bykov

    @Kingsley Felix
    Exactly the same method still applies.

    0
    Comment actions Permalink
  • Avatar
    Marco Maranao

    How would you setup for multiple domains?

    1
    Comment actions Permalink
  • Avatar
    Maxim Krasikov

    Hi @Marco Maranao,

    Additional directives for Apache or Nginx can also be applied for Service Plans. In this case, the directives can be applied for multiple domains.
    Please add directives in Plesk > Service Plans > plan_name > Web Server.

    0
    Comment actions Permalink
  • Avatar
    Peter Debik

    The examples given as a solution do not seem to match the situation

    "Plesk to display content from example.org on example.com?"

    I think the examples are showing the wrong way around. They allow content from example.com to be displayed in example.org. So should the introductory phrase not be

    "Plesk to display content from example.com on example.org?"

    0
    Comment actions Permalink
  • Avatar
    Mikhail Shport

    Hello Peter Debik,

    The "Access-Control-Allow-Origin" option allows getting access to the resources of the server for defined clients.

    In that case, you are right. The next directive allows example.org to get resources from example.com:

    Header set Access-Control-Allow-Origin "http://example.org"

    Thank you for bringing our attention to this fact. We will review the article and fix it in order to make it more correct and clear.

    0
    Comment actions Permalink
  • Avatar
    Bingo (Edited )

    Why do I get this error?

    has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.

    I tried to add Header set Access-Control-Allow-Origin "https://example.org" but it didn't worked, i still get this error.

    EDIT: I found it my self, it was already 2 times in the htaccess file, so I deleted one of the lines, and now it is working like a charm :D

    0
    Comment actions Permalink
  • Avatar
    Kernel Labs (Edited )

    Something weird I encountered in a Plesk where nginx act as a proxy for Apache :

    I added

    add_header 'Access-Control-Allow-Origin' '*'; 

    to nginx additional directives for website example.com whose static files (fonts, images) where called by example.org.

    Everything's fine for these static resources till the developer told me about a problem with his plupload implementation : the xhr sent by plupload falls in a CORS error.

    The solution was to quote the

    Header set Access-Control-Allow-Origin "*" 

    he had put in his .htaccess.
    It happens just as if htaccess instruction was in conflict with nginx instruction.

    Note :

    If .htaccess instruction is unquoted and the 'handle static resources by nginx' disabled and access control directive from nginx additional directives removed, the CORS error happens on plupload xhr and static files as well.

    Wish I understood why such a behaviour

    0
    Comment actions Permalink
  • Avatar
    Andrea Tadioli

    Hi,

    I need to set only one value:

    access-control-allow-origin: *

    and not 

    access-control-allow-origin: https://example.org

    How to?

    0
    Comment actions Permalink
  • Avatar
    Taras Ermoshin (Edited )

    Hello @Andrea Tadioli!

    You just need to replace the URL with *:

    for Apache:

    Header set Access-Control-Allow-Origin "*"

    for nginx:

    add_header 'Access-Control-Allow-Origin' '*';
    0
    Comment actions Permalink
  • Avatar
    Venkat (Edited )

    I have added this exactly as it says in both apache and ngnix but still having this issue.

    apache: Header set Access-Control-Allow-Origin "https://example.org"

    ngnix: add_header 'Access-Control-Allow-Origin' 'https://example.org';

     

    0
    Comment actions Permalink
  • Avatar
    Ikunyemi Ngor

    I added the following for both Apache and Ngnix but to no avail:

    Apache: Header set Access-Control-Allow-Origin "*"

    Ngnix: add_header 'Access-Control-Allow-Origin' '*';

    I was able to resolved the CORS issue by disabling Apache http2 module from the this instruction and removing all traces of Header set Access-Control-Allow-Origin "*" in project .htaccess files.

    0
    Comment actions Permalink
  • Avatar
    Marco Borla

    Taras Ermoshin in this guide you said:

    Only one header Access-Control-Allow-Origin can be added. CORS will not work if the header is defined both in nginx and Apache, or twice for Apache or nginx respectively

    but this sound to me a no sense because I followed your guide to go Domains > example.com > Apache & nginx Settings.

    Here I just added:Access-Control-Allow-Origin: "*"

    and saved but seems Plesk add this to Apache and Nginx so I have:

    has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.

    I don't know how to solve. Is because Apache and Nigix are active and Plesk put in both? So How can I fix this?
    I'm asking also in the community and waiting support.

    https://talk.plesk.com/threads/issue-with-cross-origin-plesk-is-locking.363032/

    To me seems Plesk is not doing a great job as put my single directive twice and I don't see how resolve.

    I hope in a reply.

    0
    Comment actions Permalink
  • Avatar
    Egbert.Frankenberg (Edited )

    I just tried this too on both options. apache and nginx but I still get the CORS error on the fonts which are coming from an s3 bucket. 

    Access to font at 'https://www.domain.com/wp-content/uploads/2021/08/KiaSignatureRegular.woff2' from origin 'https://domain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    I also have CORS policy on the s3 bucket which is open with '*'

    I tried with specifying the domain and with '*' on the PLESK configurations for the domain.

    0
    Comment actions Permalink
  • Avatar
    Marco Borla (Edited )

    Egbert.Frankenberg you can read the solution here.

    0
    Comment actions Permalink
  • Avatar
    Patrick Meppe

    Good evening. I'm having a similar problem.
    In my case I'm trying to run a NodeJs server. http2 & Nginx are active.
    The NodeJS server is directly accessible over the browser, but not as the target of an AJAX request.

    I have tried all the suggestions above and more:
    Apache & nginx Settings for

    I also temporally deactivated HTTP2. No success.

    Any other suggestions?

    0
    Comment actions Permalink
  • Avatar
    Marco Borla

    Hi Patrick Meppe.
    Sorry but I think to be not able to help with that.
    If you never get a reply here you can try ask on the Plesk forum https://talk.plesk.com/

    Good luck!

    0
    Comment actions Permalink
  • Avatar
    Arib Ansari

    Where can i find my .htaccess file?

    I've setup Nodejs server and facing cors error

    0
    Comment actions Permalink
  • Avatar
    Bhavesh Ramburn

    I'd like to add to help others, if you're running phusion passenger, for some reason my .net headers for allow origins were not being passed on properly. I had to manually insert it into my nginx conf for it to work.

    0
    Comment actions Permalink
  • Avatar
    Ahmad Khairun

    If you have control over your Server's Plesk. Try change your PHP Handler to Dedicated FPM application served as NGINX

    0
    Comment actions Permalink

Please sign in to leave a comment.

Have more questions? Submit a request