配置 Nginx 和 Apache 的CORS跨域访问

配置 Nginx 和 Apache 的CORS跨域访问

很多时候需要跨域访问时需要配置 CORS,本文就来讨论一下 Nginx 和 Apache 的 CORS 跨域访问设置。

跨域访问是可以使用程序实现的,只要在入口文件中加上一句 header("Access-Control-Allow-Origin: *");即可允许跨站访问,当然这里也可以将 * 替换成指定域名,那么就是允许指定域名访问。

本文主要介绍 Nginx 和 Apache 的配置。

Nginx 配置

server
{
# 允许指定域名跨域访问
# 此处不要用 TAB 键代替空格
    set $cors_origin "";
    if ($http_origin ~* "^https://abc.example.com$") {
        set $cors_origin $http_origin;
    }
#允许本地 vue 项目跨域访问
    if ($http_origin ~* "^http://localhost:8080$") {
        set $cors_origin $http_origin;
    }
    add_header Access-Control-Allow-Origin $cors_origin;

    listen 80;
    listen 443 ssl http2;
...
}

PHP

任意跨域请求都支持:

server {
        listen      8080;
        server_name  localhost;
        
        add_header Access-Control-Allow-Origin '*';  
        add_header Access-Control-Allow-Credentials "true";
	add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
	add_header Access-Control-Allow-Headers  'token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With';
	if ($request_method = 'OPTIONS') {
		return 200;
	}
	
	location /appmains {
            proxy_pass http://appmainsserver;
        }

    }

PHP

指定格式的请求地址:

server {
        listen      8080;
        server_name  localhost;
        #设置变量 $cors_origin
        set $cors_origin "";
	#请求地址匹配格式  用来控制http请求地址 设置跨域白名单
        if ($http_origin ~ "http://(.*).baidu.com$") {
            set $cors_origin $http_origin;              
        }	

        add_header Access-Control-Allow-Origin '$cors_origin';  
        add_header Access-Control-Allow-Credentials "true";
	add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
	add_header Access-Control-Allow-Headers  'token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With';
	if ($request_method = 'OPTIONS') {
		return 200;
	}
	
	location /appmains {
            proxy_pass http://appmainsserver;
        }

    }

PHP

多个请求地址配置:

server {
        listen      8080;
        server_name  localhost;
	#设置变量 $cors_origin
        set $cors_origin "";
	#请求地址匹配格式  用来控制http请求地址 设置跨域白名单
	if ($http_origin ~ "www.aa.com") {
            set $cors_origin $http_origin;              
        }
	if ($http_origin ~ "www.bb.com") {
            set $cors_origin $http_origin;              
        }		
        add_header Access-Control-Allow-Origin '$cors_origin';  
        add_header Access-Control-Allow-Credentials "true";
	add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
	add_header Access-Control-Allow-Headers  'token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With';
	if ($request_method = 'OPTIONS') {
		return 200;
	}
	
	location /appmains {
            proxy_pass http://appmainsserver;
        }

    }

PHP

Apache 配置

<VirtualHost *:80>
    ServerName test.c.com
    DocumentRoot "D:/projects/c/www/public"
    <Directory  "D:/projects/c/www/public/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
#添加下面这行允许跨域访问
        Header set Access-Control-Allow-Origin *
    </Directory>
</VirtualHost>

PHP

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注