美文网首页
在Mac上配置Nginx-前端资源

在Mac上配置Nginx-前端资源

作者: kyle背背要转运 | 来源:发表于2017-06-26 15:15 被阅读0次
    1. brew 安装 nginx 最便捷,自己安装太费劲,不用单独安装open ssl\pcre。
    2. sudo开启nginx后,查看端口也需要用sudo才能看到
    3. 开启nginx的时候Hosts必须指向好,要不然就会报host not found in upstream
    4. 接口转发必须要用 rewrite 重写,不能直接proxy_pass
    5. location 匹配规则 中间有空格
    6. root alias 使用情况是不一样的,root是实际目录,alias是虚拟目录的时候使用
    7. 配置文件 是可以include的
    8. 目前还没研究明白 Map 和 set 自定义变量咋用,好像说nginx,不支持全局变量,只能一层一层传过去。
    9. location 匹配规则是有优先级,匹配到了就不会进入到其他的了,具体的自行百度
    10. vscode 和 Atom 编辑器 都有language-nginx 或者nginx语法提示
      11.如果用到https 还是需要安装open ssl的,open ssl可以生成本地自用证书,开发的时候完全够用,如果生成证书会在新文章中做补充

    常用命令:

    #修改配置文件和Hosts文件后,需要执行:
    sudo nginx -s reload
    
    #停止nginx
    sudo nginx -s stop
    
    #开启nginx
    sudo nginx
    
    #MAC 查看端口占用
    sudo lsof -i:80
    

    具体安装步骤:

    1. 安装brew
      先检查Xcode是否安装然后 brew安装
    2. 安装brew完毕后,用brew 安装 nginx
      命令行输入
    brew install nginx
    
    1. 安装完毕后,打开nginx路径
    /usr/local/etc/nginx/
    
    1. 把nginx.conf文件替换成以下内容
    worker_processes  1;
    user root owner;
    events {
        worker_connections  1024;
    }
    http {
        include      mime.types;
        default_type  application/octet-stream;
        sendfile  on;
        keepalive_timeout  20;
        gzip  on;
        include /usr/local/etc/nginx/config/*.conf;
    }
    
    1. 新建一个文件夹【config】,里面存放*个 .conf 文件
      ****alias 后面的文件路径需要改成自己的
      root 后面的文件路径需要改成自己的****
    server {
    #监听端口号
            listen 80;
            
            #默认页面
            index index.html index.htm;
    
            #要绑定的域名
            #server_name r03.*.com;
            server_name r.*.com r01.*.com r02.*.com r03.*.com;
    
            autoindex on;#查看目录文件
    
            autoindex_exact_size off;#查看目录文件大小
    
            autoindex_localtime on;#查看服务器时间
    
            location / {
                
                #root /Users/*/Documents/statics-hybrid/statics-hybrid/debug/;
                root /Users/*/Documents/git/statics/;
                #root  /Users/*/Documents/lokiJS/;
                #root  /Users/*/Documents/statics-subject/hybrid-subject/;
                try_files $uri $uri/ =404;
    
            }
      }
    
    1. 配置文件配置好了以后配置 Hosts
    2. 以上都配置好 Hosts生效,命令行输入以下内容,就可以了
    sudo nginx
    

    相关文章

      网友评论

          本文标题:在Mac上配置Nginx-前端资源

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