🆕 新机装了 nginx 后,新建自己喜欢的文件夹,然后指定到 nginx 上挂载。

📷

📋 步骤:

  • 📦 安装 nginx
  • 🔍 查看配置文件
  • 📁 新建文件夹
  • ✏️ 修改配置文件
  • 🚀 启动 nginx

📦 安装 nginx

# 安装
brew install nginx
# 查看版本
nginx -v
# 查看配置文件位置
nginx -t

📁 在桌面新建一个 nginx_static 文件夹,然后在文件夹中新建一个 index.html,写入:

📝 index.html 内容如下

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>Hello World</h1>
</body>

</html>

🛠️ 修改配置文件

  1. 🧑‍💻 要修改最顶部的 user xxx staff,不然默认都是 nobody 执行,可能访问文件夹没有权限
  2. 📂 修改 server 的 location 的 root,指向刚才新建的文件夹

📷

server {
        listen       8080;
        server_name  localhost;

        location / {
            root   /Users/wangwenbo/Desktop/nginx_static;
            index  index.html index.htm;
        }
}

🔍 启动或重启 nginx

# 检查配置文件语法
sudo nginx -t

# 启动 nginx
sudo nginx

# 如果之前启动过,使用 reload
sudo nginx -s reload

# 或者 stop 后再启动
sudo nginx -s stop && sudo nginx


🌐 测试访问

访问 http://localhost:8080/ 就能看到 「Hello World」了 🎉