nginx安装和配置(mac)

nginx是高性能的http和反向代理服务器,也是一个IMAP/POP3/SMTP服务器

安装homebrew(mac套件管理器)

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

安装nginx

brew install nginx
安装后使用nginx -v查看nginx版本

配置nginx

  1. 进入nginx的安装目录,打开nginx.conf文件
cd /usr/local/etc/nginx/
sudo vi nginx.conf
  1. 对nginx.conf文件进行配置,详解:
user  tongtong staff;
#user root;
# 在配置文件的顶级main部分,代表worker角色的工作进程个数
worker_processes  1;
 
# 错误日志
error_log   /var/log/nginx/error_log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
 
# 进程文件
#pid        logs/nginx.pid;
 
events {
    # 写在events部分,每一个worker进程能并发处理(发起)的最大连接数
    worker_connections  1024;
}
 
http {
    # 文件扩展名与文件类型映射表
    include       mime.types;
     
    # 设定默认文件类型
    default_type  application/octet-stream;
 
    # 为nginx服务器设置详细的日志格式
    #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记录了哪些用户,哪些页面以及用户浏览器,ip和其他的访问信息,access_log路径
    #access_log  logs/access.log  main;
     
    # 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    sendfile        on;
 
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
 
    # 配置信息文件夹
    include conf.d/*.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;
 
            # 打开目录浏览功能,可以列出整个目录
            autoindex on;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
             
        # 指定FastCGI服务器监听端口与地址,可以是本机或者其它
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 
        # fastcgi配置
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #允许/禁止某个ip/ip段访问文件
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
}
  1. 测试配置是否成功
    nginx默认监听的端口是8080
  • 启动nginx:sudo nginx
  • 重启nginx:sudo nginx -s reload
  • 查看nginx进程:ps -ef | grep nginx
  • 杀死进程 kill [[pid]
➜  ~ ps -ef | grep nginx
  501  2489   382[pid]   0  6:50下午 ttys002    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx

补充 ps -ef | grep nginx:这条命令的意思是显示有关Apachejetspeed的进程

解释
ps 将某个进程显示出来
-A 显示所有程序
-e 此参数的效果和指定"A"参数相同
-f 显示UID,PPIP,C与STIME栏位。
grep 查找
| 管道命令,表示命令同时执行

你可能感兴趣的:(nginx安装和配置(mac))