Fixing CORS on nginx

Post Reply
User avatar
myVesta
Site Admin
Posts: 932
Joined: Fri Jun 19, 2020 9:59 am
Has thanked: 8 times
Been thanked: 6 times

How to fix the Cross-origin resource sharing (CORS) policy on myVesta on the nginx level

On SSH, logged in as root, do the following.

First, define your myVesta user and domain as a variable:

Code: Select all

USER='youruser'
DOMAIN='yourdomain.com'
Now you can copy-paste the following block:

Code: Select all

cd /home/$USER/conf/web
echo "add_header Access-Control-Allow-Origin '*';" > snginx.$DOMAIN.conf
ln -s snginx.$DOMAIN.conf nginx.$DOMAIN.conf
systemctl restart nginx
Or, to allow specific domains/subdomains, run this:

Code: Select all

cd /home/$USER/conf/web
touch snginx.$DOMAIN.conf
echo "add_header Access-Control-Allow-Origin 'domain1.com';" >> snginx.$DOMAIN.conf
echo "add_header Access-Control-Allow-Origin 'domain2.com';" >> snginx.$DOMAIN.conf
echo "add_header Access-Control-Allow-Origin 'sub.domain3.com';" >> snginx.$DOMAIN.conf
ln -s snginx.$DOMAIN.conf nginx.$DOMAIN.conf
systemctl restart nginx
Here, you need to adjust domain1.com, etc...
Post Reply