Mac 下配置nginx

8 篇文章 0 订阅
8 篇文章 0 订阅

1、先安装homebrew。官网。终端执行以下命令:

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

出现以下log表示安装完成了。

Installation successful!

2、安装Nginx,终端下执行:

brew install nginx

安装过程中会自己安装依赖:

$ brew install nginx
Warning: You have Xcode 8 installed without the CLT;
this causes certain builds to fail on OS X El Capitan (10.11).
Please install the CLT via:
  sudo xcode-select --install
==> Installing dependencies for nginx: openssl, pcre
==> Installing nginx dependency: openssl

安装完成后会有以下log:

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
==> Summary
��  /usr/local/Cellar/nginx/1.10.1: 7 files, 972.3K

3.鉴于安装过程中出现的Warning,为避免将来掉坑,运行

sudo xcode-select --install

终端会出现提示

xcode-select: note: install requested for command line developer tools

并会询问安装工具也许不会出现此界面


xcode-select

点击安装,等待软件安装完成即可。

4.启动nginx服务

brew services start nginx
$ brew services start nginx
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Checking connectivity... done.
Tapped 0 formulae (36 files, 46.1K)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

成功后,使用浏览器打开http://localhost:8080

如果打开页面如下,证明安装完成,可以使用了。


screenshot200.png

5.nginx文件目录

5.1 nginx安装文件目录
/usr/local/Cellar/nginx
5.2 nginx配置文件目录
/usr/local/etc/nginx
5.3 config文件目录
/usr/local/etc/nginx/nginx.conf
5.4 系统hosts位置
/private/etc/hosts

6.nginx常用命令

nginx  #启动nginx
nginx -s quit  #快速停止nginx
nginx -V #查看版本,以及配置文件地址
nginx -v #查看版本
nginx -s reload|reopen|stop|quit   #重新加载配置|重启|快速停止|安全关闭nginx
nginx -h #帮助


$ nginx -h #帮助
nginx version: nginx/1.10.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/Cellar/nginx/1.10.1/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

7.卸载nginx

brew uninstall nginx

8.配置nginx

#运行用户
user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;


#全局错误日志及PID文件
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;


#工作模式及连接数上限
events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,
    #仅用于linux2.6以上内核,可以大大提高nginx的性能
    use   epoll; 


    #单个后台worker process进程的最大并发链接数    
    worker_connections  1024;


    # 并发总数是 worker_processes 和 worker_connections 的乘积
    # 即 max_clients = worker_processes * worker_connections
    # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4  为什么
    # 为什么上面反向代理要除以4,应该说是一个经验值
    # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
    # worker_connections 值的设置跟物理内存大小有关
    # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
    # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
    # 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
    # $ cat /proc/sys/fs/file-max
    # 输出 34336
    # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
    # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
    # 使得并发总数小于操作系统可以打开的最大文件数目
    # 其实质也就是根据主机的物理CPU和内存进行配置
    # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
    # ulimit -SHn 65535


}




http {
    #设定mime类型,类型由mime.type文件定义
    include    mime.types;
    default_type  application/octet-stream;
    #设定日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  logs/access.log  main;


    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile     on;
    #tcp_nopush     on;


    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;


    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6].";


    #设定请求缓冲
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;




    #设定虚拟主机配置
    server {
        #侦听80端口
        listen    80;
        #定义使用 www.nginx.cn访问
        server_name  www.nginx.cn;


        #定义服务器的默认网站根目录位置
        root html;


        #设定本虚拟主机的访问日志
        access_log  logs/nginx.access.log  main;


        #默认请求
        location / {
            
            #定义首页索引文件的名称
            index index.php index.html index.htm;   


        }


        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }


        #静态文件,nginx自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #过期30天,静态文件不怎么更新,过期可以设大一点,
            #如果频繁更新,则可以设置得小一点。
            expires 30d;
        }


        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }


        #禁止访问 .htxxx 文件
            location ~ /.ht {
            deny all;
        }


    }
}


现在我们已经管理nginx了,接下来可以熟悉一下nginx的目录结构和一些重要的文件:

网站文件位置

      • /var/www/html: 网站文件存放的地方, 默认只有我们上面看到nginx页面,可以通过改变nginx配置文件的方式来修改这个位置。

服务器配置

      • /etc/nginx: nginx配置文件目录。所有的nginx配置文件都在这里。
      • /etc/nginx/nginx.conf: Nginx的主配置文件. 可以修改他来改变nginx的全局配置。
      • /etc/nginx/sites-available/: 这个目录存储每一个网站的"server blocks"。nginx通常不会使用这些配置,除非它们陪连接到  sites-enabled 目录 (see below)。一般所有的server block 配置都在这个目录中设置,然后软连接到别的目录 。
      • /etc/nginx/sites-enabled/: 这个目录存储生效的 "server blocks" 配置. 通常,这个配置都是链接到 sites-available目录中的配置文件
      • /etc/nginx/snippets: 这个目录主要可以包含在其它nginx配置文件中的配置片段。重复的配置都可以重构为配置片段。

日志文件

    • /var/log/nginx/access.log: 每一个访问请求都会记录在这个文件中,除非你做了其它设置。
    • /var/log/nginx/error.log: 任何Nginx的错误信息都会记录到这个文件中



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值