跨域入門-Nginx解決跨域問題

通過Nginx解決跨域問題,請看如下:

1.修改系統host文件,添加被調用方的域名:

127.0.0.1 b.com

2.在nginx的conf目錄下新建一個vhost目錄,用來存放虛擬主機的配置文件

3.在conf/nginx.conf文件下面添加內容,告訴nginx載入vhost目錄下的所有以conf結尾的文件

include vhost/*.conf;

4.在vhost目錄裏創建b.com.conf文件

server{
	listen 80;
	server_name b.com;
	
	location /{
		proxy_pass http://localhost:8080/;
		
		add_header Access-Control-Allow-Methods *;
		add_header Access-Control-Max-Age 3600;
		add_header Access-Control-Allow-Credentials true;
		
		
		add_header Access-Control-Allow-Origin $http_origin;
		add_header Access-Control-Allow-Headers $http_access_control_request_headers;
		
		if ($request_method = OPTIONS){
			return 200;
		}
	}
}

5.配置完畢,附加幾個nginx命令

啓動命令:start nginx.exe

測試配置文件是否正確 nginx.exe -t

重啓命令 nginx.exe -s reload

停止命令 nginx.exe -s stop

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章