
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 caniuse.com.
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.
Access-Control-Allow-Origin
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.
Apache
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: https://gf.dev), you can use the following.
Header set Access-Control-Allow-Origin "https://gf.dev"
Nginx
Here is an example lớn allow origin https://huannghe.edu.vn.dev. Showroom the following in the hệ thống block of nginx.conf or in-use configuration file.add_header Access-Control-Allow-Origin "https://huannghe.edu.vn.dev";
Access-Control-Allow-Methods
The browser can initiate one or more HTTP methods to lớn access the resources. Ex: – GET, PUT, OPTIONS, PUT, DELETE, POSTApache
To allow only GET & POST only.
Header add Access-Control-Allow-Methods "GET, POST"
Nginx
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.

Access-Control-Allow-Headers
The following headers are in safelist means you don’t need to địa chỉ one. It should work by default.
Content-TypeAcceptContent-LanguageAccept-LanguageHowever, if you need to showroom custom one, you can vì chưng it. It supports one or more headers.
Apache
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: Diễn Viên Ngọc Lan Bikini 2 Mảnh, Tiết Lộ Cân Nặng "Khó Tin" Ở Hiện Tại

Nginx
An example of adding X-Customer-Software and X-My-Custom header.
add_header Access-Control-Allow-Headers "X-Custom-Software, X-My-Custom";
Access-Control-Expose-Headers
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.ExpiresPragmaCache-ControlLast-ModifiedContent-LanguageContent-TypeBut, if you need other than the safe list, then you can allow them as following.
Apache
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.

Nginx
If you want khổng lồ expose Origin header.
add_header Access-Control-Expose-Headers "Origin";
Access-Control-Max-Age
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
Apache
To cache for 15 minutes.
Header always mix Access-Control-Max-Age "900"As you can see, the value is in seconds.
Nginx
To cache for one hour.
add_header Access-Control-Max-Age "3600";Once added, restart Nginx to see the results.

Access-Control-Allow-Credentials
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.
Apache
Header always phối Access-Control-Allow-Credentials "true"
Nginx
add_header Access-Control-Allow-Credentials "true";and the result.
Once the necessary headers are added, you can either use browser in-built developer tools or an online HTTP header checker.

Conclusion
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.