is supported by our audience. We may earn affiliate commissions from buying link on this site.

Invicti web Application Security Scanner - the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Restrict or allow resource sharing between sites using CORS header.

Bạn đang xem: Example nginx configuration for adding cross

CORS (Cross-Origin Resource Sharing) header is supported on all modern browsers.

Can I Use cors? Data on tư vấn for the cors feature across the major browsers from

By default, the browser restricts cross-origin HTTP requests through scripts. And, CORS can be handy lớn reuse the common application resources on other web applications. Once it is added correctly, it instructs the browser khổng lồ load the application from a different origin.

There are six popular types of CORS headers a server can send. Let’s explore them.


The most popular one that it tells the browser to lớn load the resources on the allowed origin. It supports wildcard (*) and doing so any tên miền can load the resources. However, it does have an option lớn allow a specific origin.


Add the following in httpd.conf or any other in-use configuration file.

Header set Access-Control-Allow-Origin "*"Restart the Apache to lớn test. You should see them in response headers.


And, lớn allow from a specific origin (ex:, you can use the following.

Header set Access-Control-Allow-Origin ""


Here is an example lớn allow origin Showroom the following in the hệ thống block of nginx.conf or in-use configuration file.

add_header Access-Control-Allow-Origin "";


The browser can initiate one or more HTTP methods to lớn access the resources. Ex: – GET, PUT, OPTIONS, PUT, DELETE, POST


To allow only GET & POST only.

Header add Access-Control-Allow-Methods "GET, POST"


Let’s say you need to địa chỉ cửa hàng DELETE & OPTIONS methods, then you can địa chỉ as below.

add_header Access-Control-Allow-Methods "DELETE, OPTIONS";After the restart, you should see them in the response headers.



The following headers are in safelist means you don’t need to địa chỉ one. It should work by default.


However, if you need to showroom custom one, you can vì chưng it. It supports one or more headers.


Let’s say you want khổng lồ allow X-Custom-Header và X-Powered-By headers.

Header always set Access-Control-Allow-Headers "X-Custom-Header, X-Powered-By"After a restart, you should see the result in response headers.

Xem thêm:



An example of adding X-Customer-Software and X-My-Custom header.

add_header Access-Control-Allow-Headers "X-Custom-Software, X-My-Custom";


The following headers are already safe list. Means, you don’t need to địa chỉ cửa hàng if you want lớn expose them.


But, if you need other than the safe list, then you can allow them as following.


Use a wildcard to expose all headers.

Header always mix Access-Control-Expose-Headers "*"Note: a wildcard still doesn’t expose Authorization header, và if you need one, you need lớn mention explicitly.

Header always set Access-Control-Expose-Headers "Authorization, *"The result should look lượt thích this.



If you want khổng lồ expose Origin header.

add_header Access-Control-Expose-Headers "Origin";


Do you know the data from Access-Control-Allow-Headers & Access-Control-Allow-Methods headers can be cached? It can be cached for up lớn 24 hours in Firefox, 2 hours in Chrome (76+).

To disable the caching, you can keep the value as -1


To cache for 15 minutes.

Header always mix Access-Control-Max-Age "900"As you can see, the value is in seconds.


To cache for one hour.

add_header Access-Control-Max-Age "3600";Once added, restart Nginx to see the results.



There is only one option lớn set here – true. This is to allow if you want khổng lồ expose credentials such as cookies, TLS certificates, authorization.


Header always phối Access-Control-Allow-Credentials "true"


add_header Access-Control-Allow-Credentials "true";and the result.


Verifying the results

Once the necessary headers are added, you can either use browser in-built developer tools or an online HTTP header checker.



I hope the above helps you khổng lồ implement the CORS header in Apache HTTP và the Nginx web vps for better security. You may also be interested in applying OWASP recommended secure headers.