不用给我解释,直接上代码
时间: 2023-07-10 17:30:12 浏览: 88
以下是一些常见的 Nginx 配置代码:
1. 启用 HTTPS 协议:
```
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert;
ssl_certificate_key /path/to/key;
...
}
```
2. 启用 HSTS:
```
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
```
3. 启用 Gzip 压缩:
```
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
```
4. 缓存设置:
```
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1d;
}
```
5. 使用 CDN 服务:
```
location / {
proxy_pass https://cdn.example.com;
...
}
```
6. 减少 HTTP 请求:
```
location / {
# 合并 CSS
concat on;
concat_css on;
concat_css_max_files 5;
# 合并 JS
concat_js on;
concat_js_max_files 5;
# 延迟加载图片
lazyload_images on;
lazyload_images_threshold 100;
...
}
```
7. 优化图片:
```
location ~* \.(jpg|jpeg|png|gif)$ {
# 使用图片优化工具 TinyPNG
tinypng on;
tinypng_key YOUR_API_KEY_HERE;
...
}
```
8. 减少重定向:
```
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
...
}
```
阅读全文