使用
# 安裝後配置文件默認在 /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/;
}
}