系统:Ubuntu
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.2 LTS
Release: 24.04
Codename: noble
安装nginx
sudo apt update
sudo apt install nginx
sudo apt install nano
nginx -v
# nginx version: nginx/1.24.0 (Ubuntu)
配置nginx
配置的路径在/etc/nginx/conf.d文件夹中,nginx会读取这个文件夹中的所有*.conf文件;
cd /etc/nginx/conf.d
# 新建*.conf文件
nano b1.conf
以IP地址192.168.120.23,资源绝对路径/home/document/为例
server {
listen 10086;
server_name 192.168.120.23;
location /document/ {
alias /home/document/; # 根目录服务器文件夹绝对目录
autoindex on; # 允许列出目录内容
autoindex_exact_size off; # 显示文件大小(可选)
autoindex_localtime on; # 显示本地时间(可选)
charset utf-8; # 指定 UTF-8 编码
}
}
设置文件夹权限:
sudo chmod -R 755 /home/document/
sudo chown -R www-data:www-data /home/document/ # Ubuntu/Debian
应用并重新启动
sudo systemctl restart nginx
打开网址,访问:http://192.168.100.23:10086/document/ 网站,如果能访问成功,说明nginx设置成功