70 likes | 90 Views
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/
E N D
How to Enable CORS in NGINX
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
Enable CORS in NGINX Add add_header directive to server block of your NGINX configuration file. server{ ... add_header Access-Control-Allow-Origin *; ... }
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
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";
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
Thank You Visit for details https://ubiq.co/tech-blog/enable-cors-nginx/