ubuntu25.04
1、安装nginx,启动,开启开机自启
apt install nginx service nginx start systemctl enable nginx2、配置静态文件的配置
Nginx的配置文件通常位于 /etc/nginx/nginx.conf,但为了更好地管理静态资源,我们通常会在 /etc/nginx/sites-available 目录下创建一个新的配置文件,并在 /etc/nginx/sites-enabled 目录下创建一个软链接。
/etc/nginx/sites-available 目录下创建一个新的配置文件:
vim /etc/nginx/sites-available/static-site.conf添加如下内容
server{listen80;server_name 你的ip或者域名;root 你的静态文件目录本地绝对路径;# 关闭访问日志(可选)access_log off;location /{try_files$uri$uri/=404;# 开启自动索引(显示目录列表)autoindex on;autoindex_exact_size off;# 显示文件大小(KB/MB)autoindex_localtime on;# 使用本地时间# 设置文件类型default_type application/octet-stream;# 支持大文件下载client_max_body_size 1000M;}# 可选:配置缓存location ~*\.(jpg|jpeg|png|gif|ico|css|js)${expires 30d;add_header Cache-Control"public";}}listen 80;:监听80端口。
index index.html;:指定默认的索引文件。
location / { try_files $uriKaTeX parse error: Expected 'EOF', got '}' at position 12: uri/ =404; }̲:尝试匹配请求的文件,如果没有…{ … }:可选的缓存配置,用于缓存图片、CSS和JavaScript文件。
创建软链接启用新的配置文件:
sudo ln -s /etc/nginx/sites-available/static-site.conf /etc/nginx/sites-enabled/测试配置文件是否有语法错误:
sudo nginx -t如图成功:
重新启动一下
sudo service nginx restart放一个测试文件到静态文件目录里测试