Mac上安装nginx

来源:互联网 发布:老虎机算法作弊器 编辑:程序博客网 时间:2024/04/30 06:07

1.brew install nginx

安装完成后,输出:

Docroot is: /usr/local/var/www


The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that 监听8080端口

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

==> Summary

��  /usr/local/Cellar/nginx/1.10.1: 7 files, 972.2K这里的目录指的是默认的web目录。

2.访问localhost:8080/:输出:

nginx欢迎界面。查看nginx.conf配置文件:

server {

        listen       8080;

        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

            root   html;

            index  index.html index.htm;

        } 根目录是在html文件夹下。访问/usr/local/Cellar/nginx/1.10.1: 可以看到该目录下有:

xxxxxxxx

lrwxr-xr-x   1 xxx  admin      16  4 23 14:24 html -> ../../../var/www

xxxxxxxxx

html目录下包含index.html.我们在html下新建一个文件夹,例如test,将/usr/local/Cellar/nginx/1.10.1/html/index.html移至/usr/local/Cellar/nginx/1.10.1/html/test/index.html。再次访问localhost:8080.提示forbidden。403.看来nginx不会自动去子文件夹下去寻找index.html.访问localhost:8080/test.正常加载了nginx欢迎页。


所以,可以在设置的root目录下,设置多个子文件夹,每个文件夹内有自己的html文件。访问时,路径上要带上子文件夹的名字。否则,会403.



2.root 和alias

使用nginx设置root时要注意一个问题,就是如果该root设置的前端目录不是根目录,那么在写root的绝对地址时,要把前端目录的部分省略掉。
我们用设置虚拟目录指向的alias来和root比较一下就非常明显了

alias

1
2
3
location/abc/{
    alias/home/html/abc/;
}

在这段配置下,http://test/abc/a.html就指定的是 /home/html/abc/a.html。这段配置亦可改成

root

1
2
3
location /abc/ {
    root /home/html/;
}

可以看到,使用root设置目录的绝对路径时,少了/abc,也就是说,使用root来设置前端非根目录时,nginx会组合root和location的路径。

另外,使用alias时目录名后面一定要加“/”





0 0