美文网首页iOS Developer
Mac利用Nginx搭建静态文件服务器

Mac利用Nginx搭建静态文件服务器

作者: 97227b8677ae | 来源:发表于2017-05-31 21:10 被阅读1512次

1. 安装Homebrew

Homebrew, Mac系统的包管理器,用于安装一些必需的工具软件。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装完毕后可以执行命令,查看是否安装成功

brew -v

2. 安装Nginx

brew install nginx

安装完毕同样可以查看下版本确认是否成功

nginx -v

3. Nginx命令

  • 启动
sudo nginx
  • 停止
sudo nginx -s stop
  • 更新
sudo nginx -s reload

执行sudo nginx后,打开http://localhost:8080, 如果看到欢迎页面证明Nginx已经启动

9A642DE2-66A6-4D27-8FF4-5682BA336E41.png

执行brew info nginx查看配置文件路径等信息

Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx

4. 配置Nginx

要配置文件服务器需要修改默认路径

  • 打开Finder,点击前往 -> 前往文件夹, 输入/usr/local/etc/nginx/nginx.conf,进入到配置文件所在位置,用xcode或者sublime打开nginx.conf文件。
  • 默认端口为8080,修改为81(随你喜欢,最好设置为不常用的,以免冲突)
server {
        listen       81; //默认为8080
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        ...
}
  • 修改服务器根目录
location / {
      root   /Users/lee/WorkSpace;  // 默认是html, 替换为你自己电脑文件夹的路径
      index  index.html index.htm;
}

修改完再次访问http://localhost:81,会发现出现403 Forbidden
出现403的原因有几点

  • 文件夹权限,设置目录文件夹的访问权限即可。
  • 没有设置显示索引,在 location中加入autoindex on;
location / {
            root   /Users/lee/WorkSpace;
            index  index.html index.htm;
            autoindex on;               ##显示索引  
            autoindex_exact_size on;    ##显示大小
            autoindex_localtime on;     ##显示时间  
        }

最后再次访问http://localhost:81,出现文件列表,即为成功。

B3B075BD-B014-4B71-99E1-8B5227CE2C86.png

相关文章

网友评论

    本文标题:Mac利用Nginx搭建静态文件服务器

    本文链接:https://www.haomeiwen.com/subject/lbijfxtx.html