Nginx代理静态资源

本文最后更新于:几秒前

以下是一个静态文件代理的示例配置,启用了SSL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {
listen 443 ssl;
server_name example.com;

# SSL证书配置
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

# 设置根目录,静态文件将从这个目录提供
root /path/to/your/static/files;

# 代理静态文件
location /static/ {
alias /path/to/your/static/files/;
expires 30d;
access_log /var/log/nginx/static_access.log;
}

# 针对特定静态资源类型设置缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
root /path/to/your/static/files;
expires 7d;
access_log off;
}
}

# 配置HTTP自动跳转到HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}

配置解析

  • listen 443 ssl:开启443端口的SSL监听,启用HTTPS。
  • ssl_certificatessl_certificate_key:指定SSL证书文件和私钥文件的路径。
  • ssl_protocolsssl_ciphers:设置SSL协议版本和加密套件,以确保安全性。
  • return 301 https://$server_name$request_uri;:将HTTP请求自动重定向到HTTPS,保证所有访问都通过加密连接。

这样配置后,访问静态资源时会通过HTTPS加密传输,提升了数据的安全性。


Nginx代理静态资源
https://yan-sheng-li.github.io/blog/posts/29122.html
作者
李延胜
发布于
2024年11月8日
许可协议