1 / 7

How to Enable CORS in NGINX

CORS allows users to make cross domain requests to your website. Here's how to enable CORS in NGINX to allow cross domain requests to your web server. #nginx #webdevelopment #corsair <br><br>Visit https://ubiq.co/tech-blog/enable-cors-nginx/

Download Presentation

How to Enable CORS in NGINX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to Enable CORS in NGINX

  2. Open NGINX configuration file If you are using NGINX’s main configuration file nginx.conf, without virtual hosts, then run the following command $ sudo vi /etc/nginx/nginx.conf If you have configured separate virtual hosts for your website (e.g www.mysite.com), such as /etc/nginx/sites-enabled/mysite.conf then open it with the following command $ sudo vi /etc/nginx/sites-enabled/mysite.conf

  3. Enable CORS in NGINX Add add_header directive to server block of your NGINX configuration file. server{ ... add_header Access-Control-Allow-Origin *; ... }

  4. Enable CORS from all websites If you want to enable CORS for all websites, that is, accept cross domain requests from all websites, add the following add_header Access-Control-Allow-Origin *; In the above statement, we use wildcard (*) for NGINX Access-Control-Allow-Origin directive

  5. Enable CORS from one domain If you want to enable CORS for one website domain (e.g example.com), specify that domain in place of wildcard character *. add_header Access-Control-Allow-Origin "example.com"; If you want to enable CORS for multiple domains (e.g example1.com, example2.com,example3.com), specify them separately one after another add_header Access-Control-Allow-Origin "example1.com"; add_header Access-Control-Allow-Origin "example2.com"; add_header Access-Control-Allow-Origin "example3.com";

  6. Restart NGINX Run the following command to check syntax of your updated config file. $ sudo nginx -t If there are no errors, run the following command to restart NGINX server. $ sudo service nginx reload #debian/ubuntu $ systemctl restart nginx #redhat/centos

  7. Thank You Visit for details https://ubiq.co/tech-blog/enable-cors-nginx/

More Related