使用
# 安装后配置文件默认在 /usr/local/etc/nginx nginx.conf
# 配置文件默认服务地址:访问http://localhost:8080
# 检测配置文件内容正确性
$ nginx -t
# 刷新配置文件
$ nginx reload
# 产看版本
$ nginx -v
# 启动 访问http://localhost:8080
$ brew services start nginx
# 重启 访问http://localhost:8080
$ brew services restart nginx
# 停止
$ brew services stop nginx
# nginx.conf 文件内容如下
# 最大连接数=1024*3=3072
# 运行用户
user wangwenjian;
# 程序进程数,建议设置为等于CPU总核心数
worker_processes 3;
# 错误日志路径及级别 [debug|info|notice|warn|error|crit]
error_log /var/log/nginx/error.log warn;
# 进程文件
pid /var/run/nginx.pid;
# 工作模式及连接数上限
events {
# epoll是多路复用IO(I/O Multiplexing)的一种方式,但仅用于linux2.6以上内核,提升性能
use epoll;
# 单个进程最大连接数
worker_connections 1024;
};
# 最大文件打开数(连接)。可不设置或设置为系统优化后的ulimit -HSn的结果
worker_rlimit_nofile 51200;
# 设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
#配置1 监听80端口,这个server 不写也默认存在的
server {
#server_name localhost;
listen 80;
}
#配置2 监听89端口
server {
#server_name localhost;
listen 89;
# 访问 ip:89/ 转发到百度
location / {
proxy_pass http://www.baidu.com/;
}
# 访问 ip:89/sina/ 直接转发到新浪,不使用负载均衡
location /sina/ {
proxy_pass https://www.sina.com/;
}
# 访问 ip:89/163/ 转发到网易,使用负载均衡
location /163/ {
proxy_pass https://163_pool;
}
}
#网易负载均衡池,可以配置多个服务,权重,轮训
upstream 163_pool{
server https://www.163.com/ weight=1 max_fails=2 fail_timeout=30s;
server https://www.163.com/;
server https://www.163.com/;
}
}