(一)常用命令
(1)安装
前提是安装好了 brew
brew install nginx
(2)查看 nginx 版本
nginx -v
(3)启动nginx
sudo nginx
(4)停止nginx
nginx -s stop
(5)重启 nginx
nginx -s reload
(二)配置文件
安装好的 nginx 目录路径如下:
/usr/local/etc/nginx
下面有这么多文件,其中配置文件是 nginx.conf
#user nobody;
#nginx进程,一般数值为cpu核数,worker_processes值越大,说明可以支持的并发请求也就越多
worker_processes 1;
#错误日志存放目录
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程pid存放位置
#pid logs/nginx.pid;
#工作模式及连接数上限
events {
#单个后台worker process进程的最大并发链接数
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"';
#nginx访问日志
#access_log logs/access.log main;
#开启高效传输模式
sendfile on;
#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布, 积极的作用是减少网络报文段的数量
#tcp_nopush on;
#连接超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩功能
#gzip on;
#基于域名的虚拟主机
server {
#监听端口
listen 80;
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;
# 将服务器错误页面重定向到静态页面/50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#代理PHP脚本到Apache上监听127.0.0.1:80
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#将PHP脚本传递到正在监听127.0.0.1:9000的FastCGI服务器
#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;
#}
#如果Apache的文档根目录与nginx的根目录一致,则拒绝访问.htaccess文件
#location ~ /\.ht {
# deny all;
#}
}
#另一个虚拟主机,混合使用IP、名称和基于端口的配置
#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;
# 服务端key
# 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;
# }
#}
}
配置文件分为三个模块:
(1)全局块:主要影响 nginx 服务器整体运行的配置指令
(2)event 块:主要影响Nginx 服务器与用户网络的链接
(3)http 块:负载均衡、反向代理、动静分离都是在这个模块里设置的
http 块 包含 http 块 和 server 块