You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Learn how to use headers and middleware both globally and per route.
4
+
5
+
---
6
+
7
+
:ellipsis{right=0pxwidth=75%blur=150px}
8
+
9
+
Nuxt Security by default registers a set of **global** Nuxt `routeRules` that will make your application more secure by default. Both headers and middleware can be easily configured and even disabled when needed.
10
+
11
+
::alert{type="info"}
12
+
ℹ Read more about security headers [here](https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html#use-appropriate-security-headers).
13
+
::
14
+
15
+
## Global configuration
16
+
17
+
To override default behavior for Nuxt Security globally, follow this pattern:
18
+
19
+
```ts
20
+
exportdefaultdefineNuxtConfig({
21
+
security: {
22
+
headers: {
23
+
// certain header
24
+
xXSSProtection: '1',
25
+
},
26
+
27
+
// certain middleware
28
+
rateLimiter: {
29
+
// options
30
+
}
31
+
}
32
+
})
33
+
```
34
+
35
+
## Per route configuration
36
+
37
+
To enable per-route configuration, use the `routeRules` like following:
38
+
39
+
```ts
40
+
exportdefaultdefineNuxtConfig({
41
+
routeRules: {
42
+
'/custom-route': {
43
+
headers: {
44
+
// certain header
45
+
'Cross-Origin-Embedder-Policy': 'require-corp'
46
+
},
47
+
48
+
// certain middleware
49
+
security: {
50
+
rateLimiter: {
51
+
// options
52
+
}
53
+
}
54
+
}
55
+
}
56
+
})
57
+
```
58
+
59
+
You can also use route roules in pages like following:
60
+
61
+
```vue
62
+
<template>
63
+
<div>Hello from page</div>
64
+
</template>
65
+
66
+
<script setup lang="ts">
67
+
defineRouteRules({
68
+
headers: {
69
+
'X-XSS-Protection': '1'
70
+
},
71
+
security: {
72
+
rateLimiter: {
73
+
tokensPerInterval: 3,
74
+
interval: 60000,
75
+
},
76
+
}
77
+
})
78
+
</script>
79
+
```
80
+
81
+
::alert{type="warning"}
82
+
When using `routeRules`, make sure to:
83
+
84
+
1. use the proper HTTP Header names like `Cross-Origin-Embedder-Policy` instead of `crossOriginEmbedderPolicy` and to not set the headers inside `security`. These headers are handled by Nuxt and you can check more [here](https://nuxt.com/docs/guide/concepts/rendering#hybrid-rendering).
85
+
2. add middleware inside of `security` in certain route rule. This is a custom NuxtSecurity addition that does not exists in core Nuxt.
86
+
::
87
+
88
+
## Disabling functionality
89
+
90
+
To disable certain middleware or headers, follow this pattern:
Copy file name to clipboardexpand all lines: docs/content/1.documentation/5.advanced/2.faq.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ Find answers for difficult questions.
4
4
5
5
---
6
6
7
+
:ellipsis{right=0pxwidth=75%blur=150px}
8
+
7
9
## Testing CORS configuration
8
10
9
11
In the default configuration for CORS in Nuxt Security module, only the request that is coming from your origin (the same host by default) will be accepted and others will be rejected.
0 commit comments