是什么
笼统来说,“nginx 是一个服务器程序”,它可以:
- 托管静态文件
- 反向代理
- 负载均衡
- ……
nginx 的功能太多了,本篇只介绍 nginx 作为静态文件服务器程序时的用法
什么是静态文件服务器?比如你有一些 html、css、js 文件,你想用这些文件“构成一个网站”,那么此时,你就需要一个“静态文件服务器程序”
另一个角度。程序有很多种,比如聊天程序(微信、qq);比如服务器程序(apache、nginx)。html、css、js 都可以叫做“静态文件”。所以……
安装
以 ubuntu 为例
把下列命令复制到命令行(一次只执行一行命令),回车
中途可能会问你“是否确认……?”,输入
y
即可
sudo apt install curl gnupg2 ca-certificates lsb-release
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
sudo apt update
sudo apt install nginx
上述过程可能比较慢
然后,启动 nginx:
sudo nginx
检查安装
找个电脑,访问 http://[ip]/
把文件“交给”nginx
nginx 只需要你把文件放到“某个目录”下,就可以了
这个目录,由 nginx 的配置文件决定
nginx 的配置文件,一般在 /etc/nginx/nginx.conf
(文件)或 /etc/nginx/conf.d
(文件夹)
查看 nginx 的配置文件
你可以在 nginx 的配置文件里找到“那个目录”
cat nginx.conf
你可能会看到类似:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
这样的配置。如果没有,在 /etc/nginx/conf.d
里面的几个配置文件里找一找
其中 /usr/share/nginx/html
就是“那个目录
访问静态文件
只需要把文件放入“那个目录”,比如/usr/share/nginx/html/hello/test.html
只需要访问:http://ip/hello/test.html