环境:
nginx 1.22.1
宝塔8.0
php lavarel
在nginx里加入下面的设置
#这里填*就是任何域名都允许跨域add_header Access-Control-Allow-Origin "*";#CORS请求默认不发送Cookie和HTTP认证信息。但是如果要把Cookie发到服务器,要服务器同意,指定 #Access-Control-Allow-Credentials字段。add_header Access-Control-Allow-Credentials 'true';#设置跨域请求允许的Header头信息字段,以逗号分隔的字符串add_header Access-Control-Allow-Headers 'Origin,X-Requested-With,Content-Type,Accept,Authorization,token';#设置跨域允许的请求add_header Access-Control-Allow-Metthods 'POST,GET,PUT,OPTIONS,DELETE';
完整配置如下
server
{listen 80;listen 50003;server_name www.aa.com;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/www.aa.com;#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则#error_page 404/404.html;#SSL-END#ERROR-PAGE-START 错误页配置,可以注释、删除或修改#error_page 404 /404.html;#error_page 502 /502.html;#ERROR-PAGE-END#PHP-INFO-START PHP引用配置,可以注释或修改include enable-php-82.conf;#PHP-INFO-END#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效include /www/server/panel/vhost/rewrite/new.shopn.conf;#REWRITE-END#这里填*就是任何域名都允许跨域add_header Access-Control-Allow-Origin "*";#CORS请求默认不发送Cookie和HTTP认证信息。但是如果要把Cookie发到服务器,要服务器同意,指定 #Access-Control-Allow-Credentials字段。add_header Access-Control-Allow-Credentials 'true';#设置跨域请求允许的Header头信息字段,以逗号分隔的字符串add_header Access-Control-Allow-Headers 'Origin,X-Requested-With,Content-Type,Accept,Authorization,token';#设置跨域允许的请求add_header Access-Control-Allow-Metthods 'POST,GET,PUT,OPTIONS,DELETE';#禁止访问的文件或目录location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md){return 404;}#一键申请SSL证书验证目录相关设置location ~ \.well-known{allow all;}#禁止在证书验证目录放入敏感文件if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {return 403;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;error_log /dev/null;access_log /dev/null;}location ~ .*\.(js|css)?${expires 12h;error_log /dev/null;access_log /dev/null;}access_log /www/wwwlogs/new.shopn.log;error_log /www/wwwlogs/new.shopn.error.log;
}