在mac下使用Nginx

安装Nginx


配置虚拟域名

用一个例子来说明:要给项目文件夹为 /Users/phoenix/Web/HTMLhttp/ 的项目配置虚拟域名为 http://html.server.cn

  1. 在 /etc/hosts 文件里添加

    1
    127.0.0.1       html.server.cn
  2. 在 nginx.conf 文件里添加(我的nginx.conf文件在 /usr/local/etc/nginx 目录下)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    http {
    ...

    //添加以下代码
    server {
    listen 80;
    server_name html.server.cn;
    root /Users/phoenix/Web/HTMLhttp/;
    location / {
    index index.php index.html index.htm;
    }
    }
    }

    或者(我更推荐的方式)
    /usr/local/etc/nginx 目录下新建一个文件夹 servers,接着在 servers 文件夹下新建一个配置文件 mynginx.conf (文件名可自定义)
    在 mynginx.conf 文件里添加配置

    1
    2
    3
    4
    5
    6
    7
    8
    server {
    listen 80;
    server_name html.server.cn;
    root /Users/phoenix/Web/HTMLhttp/;
    location / {
    index index.php index.html index.htm;
    }
    }

    然后在 nginx.conf 配置文件里引用 mynginx.conf 配置

    1
    2
    3
    4
    5
    http {
    ...

    include servers/mynginx.conf; // 新增这行代码
    }