Mac下安装Nginx

1.安装HomeBrew:

  HomeBrew是Mac OS X中的包管理器。打开HomeBrew的主页:http://brew.sh,可以看到安装指令,在终端中输入 ruby -e "$(curl -fsSLhttps://raw.github.com/mxcl/homebrew/go)" 回车,接下来就开始在线安装HomeBrew了,安装时间可能较长,视网络状况而定。

2.更新HomeBrew:

  安装完成后,需要更新下homebrew,终端输入:brew update  然后手动将/usr/local/homebrew/bin添加到环境变量path中即可。

3.利用brew安装

  终端执行  brew search nginx

  终端执行 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 nginx can run without sudo.  ----修改端口号

 

Nginx will load all files in /usr/local/etc/nginx/servers/.   -----nginx启动时会加载此文件夹下的所有文件

 

To have launchd start nginx now and restart at login: brew services start nginx  ------重启nginx

 

Or, if you don't want/need a background service you can just run:nginx   ------启动nginx

 

   访问localhost:8080,说明安装成功

 

4.利用nginx实现负载均衡,可以参考我的另一篇博客:

 http://www.cnblogs.com/malcolmfeng/p/6902635.html

 

注:nginx默认配置文件结构 (/usr/local/etc/nginx/nginx.conf)

 

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
  worker_connections 1024;
}


http {
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 on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

#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_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}

 

 
原文地址:https://www.cnblogs.com/malcolmfeng/p/6900531.html