一、安装
-
下载地址 nginx: download
-
解压到指定目录,即nginx目录,下面称“根目录”。
二、启动
-
方式一:双击“根目录”下的nginx.exe即可运行;
-
方式二:进入cmd窗口切换到“根目录”下,执行start nginx
start nginx
-
重载配置
nginx -s reload
三、 关闭
-
方式一:资源管理器里面杀掉进程(两个)
-
方式二:cmd窗口进入“根目录”执行:nginx -s stop;
nginx -s stop #快速停止 nginx -s quit #完整停止
四、配置
-
主配置文件:“根目录”/conf/nginx.conf,配置前备份原始文件
#号为注释行 #user nobody; #使用用户 worker_processes 1;#nginx进程数,建议设置为等于CPU总核心数。 events { #单个进程最大连接数(最大连接数=连接数*进程数) #根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。 worker_connections 1024; } http { include mime.types; #导入外部文件mime.types,将所有types提取为文件,然后导入到nginx配置文件中 default_type application/octet-stream; #默认文件类型 #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。 sendfile on; #长连接超时时间,单位是秒 keepalive_timeout 65; # 第一个Server区块开始,表示一个独立的虚拟主机站点 server { # 提供服务的端口,默认80 listen 80; # 提供服务的域名主机名 server_name localhost; #对 "/" 启用反向代理,第一个location区块开始 location / { root html; #服务默认启动目录 index index.html index.htm; # 默认的首页文件,多个用空格分开 } # 错误页面路由 error_page 500 502 503 504 /50x.html; # 出现对应的http状态码时,使用50x.html回应客户 location = /50x.html { # location区块开始,访问50x.html root html; # 指定对应的站点目录为html } } }
-
虚拟主机
一般在“根目录”/conf/vhosts下建立相应的.conf文件
在主配置中配置includes
http { ... server { ... } include vhosts/*.conf #多个server模块 }
server模块如下:
server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } }
检查nginx代码防止语法错误,无误后重启nginx
nginx -t
-
反向代理
location / { proxy_pass http://127.0.0.1:8080; index index.html index.htm index.jsp; }
-
负载均衡
http { ... upstream test { # ip_hash; #轮询方式 server hosts1 weight=5; #权重配置 server hosts2 weight=5; #fair; #响应时间短的优先分配 } ... server { location / { root html; index index.html index.htm; proxy_pass http://test; # 等价于http://hosts1或http://hosts2 } } }