서버에서 CORS 설정하기
사내 사이트 유지보수 하면서 그동안 cors 관련해서 할일도 없었는데
개발 관련 일을 계속하다보니 cors 관련 에러를 접함....
그리하여 조금 고생을 함.
1.php 설정
어느분 블로그를 보고 한건데 페이지가 기억이 안남..... 암튼 allow 된 사이트만 허용하게
하는것.
$http_origin = $_SERVER['HTTP_ORIGIN'];
$allowed_origin = array('http://사이트', 'https://사이트');
if (in_array($http_origin, $allowed_origin))
{
header("Access-Control-Allow-Origin: {$http_origin}");
}
2. aws apigateway 설정
angular + s3 + lambda + apigateway 조합으로 사이트 만들었는데
여기에서 cors 삽질을 함.... (기본 지식이 부족해서)
암튼 요 링크에 cors 설정들이 대부분 있는거 같음.
https://enable-cors.org/server.html
요거보고 해결.
https://enable-cors.org/server_awsapigateway.html
내용들 :
개발 관련 일을 계속하다보니 cors 관련 에러를 접함....
그리하여 조금 고생을 함.
1.php 설정
어느분 블로그를 보고 한건데 페이지가 기억이 안남..... 암튼 allow 된 사이트만 허용하게
하는것.
$http_origin = $_SERVER['HTTP_ORIGIN'];
$allowed_origin = array('http://사이트', 'https://사이트');
if (in_array($http_origin, $allowed_origin))
{
header("Access-Control-Allow-Origin: {$http_origin}");
}
2. aws apigateway 설정
angular + s3 + lambda + apigateway 조합으로 사이트 만들었는데
여기에서 cors 삽질을 함.... (기본 지식이 부족해서)
암튼 요 링크에 cors 설정들이 대부분 있는거 같음.
https://enable-cors.org/server.html
요거보고 해결.
https://enable-cors.org/server_awsapigateway.html
내용들 :
- Log into API Gateway console
- Create all the REST resources that needs to be exposed with their methods before setting up CORS (if new resources/methods are created after enabling CORS, these steps must be repeated)
- Select a resource
- Add OPTIONS method, choose as integration type "mock"
- For each Method of a resource
- Go to Response Method
- Add all the response method that should be supported (i.e. 200, 500, etc.)
- For each response code set Response Headers to
- X-Requested-With
- Access-Control-Allow-Headers
- Access-Control-Allow-Origin
- Access-Control-Allow-Methods
- Go to Integration Response, select one of the created response codes, then Header Mappings
- Insert default values for headers
example:- X-Requested-With: '*'
- Access-Control-Allow-Headers: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with'
- Access-Control-Allow-Origin: '*'
- Access-Control-Allow-Methods: 'POST,GET,OPTIONS'
- Deploy the API to a stage
- Check using http://client.cors-api.appspot.com/client that CORS requests have been successfully enabled
댓글
댓글 쓰기