小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
关于Nginx
是什么 一个高性能的 HTTP 和反向代理 web 服务器 占有内存小,并发能力强,能够支持高达5w个并发连接数的响应; 安装简单,配置简洁;
作用 反向代理、负载均衡、动静分离
基本使用
安装
官网下载压缩包:nginx.org/en/download… 解压即可直接使用。
配置
配置 nginx.conf (可以先使用默认配置跑起来看看)
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
启动
双击 nginx.exe 运行,或在 nginx.exe 所在根目录打开 cmd 命令行窗口执行 start nginx 。
会有个窗口弹出来,闪一下又消失,我刚开始还以为是什么错误,后来发现正常启动了也是这样的 - - ..
检查是否正常运行
浏览器打开 localhost(http 默认80端口),如果出现这样的界面,就是已经启动成功了。
或者,到
logs 文件夹查看,见到有 nginx.pid 文件,也能说明是成功的(应该是放了个进程编号)
检查错误原因
如果没有,打开
logs/error.log 查看是否有报错信息。比如说出现下面这样的报错,就是端口被占用了。nginx 默认配置是80端口的,若端口被占用,打开配置文件,修改端口后重新运行即可。
温馨提示:每次修改配置文件后需要重新加载才会生效喔。nginx 支持热部署,可直接执行
nginx -s reload重载配置文件
项目部署
修改配置文件 nginx.conf,在 server 块配置监听端口、location 块配置项目目录及反向代理等;
支持配置多个 server 块。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80; #指定监听端口
server_name localhost;
location / {
root E:\ms-test\dist1; #配置项目根目录
index index.html index.htm;
}
}
server {
listen 8080; #指定监听端口
server_name localhost;
location / {
root E:\ms-test\dist2; #配置项目根目录
index index.html index.htm;
}
# 反向代理配置:匹配 /api 的,代理到 http://localhost:9001
location /api {
proxy_pass http://localhost:9001;
}
# 匹配多个地址,可使用正则
location ~ ^/(javascripts|vizportal|views|vizql|img)/.*$ {
proxy_pass http://localhost:9002;
}
}
}
书写 Tips
每个指令必须有分号结束;
代理的ip地址要写上前缀, http://
# 号开头的都是注释信息(同 js 中的 // ),就是没有起作用的
配置文件解读
nginx.conf 文件结构
... #全局块
events { ... } #events块
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] { ... } #location块
location [PATTERN] { ... }
}
server { ... }
... #http全局块
}
全局块
配置影响nginx全局的指令。一般有运行nginx服务器的用户组(user),nginx进程pid存放路径(pid),日志存放路径(error_log),配置文件引入,允许生成worker process数(worker_processes)等。
events块
配置影响nginx服务器或与用户的网络连接(accept_mutex)。有每个进程的最大连接数(multi_accept),选取哪种事件驱动模型处理连接请求(use),是否允许同时接受多个网路连接(worker_connections),开启多个网络连接序列化等。
http块
可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件(sendfile),连接超时时间(keepalive_timeout),单连接请求数等。
server块
配置虚拟主机的相关参数,如监听端口(listen)、监听地址(server_name)等。一个http中可以有多个server。
location块 配置请求的路由,以及各种页面的处理情况。
nginx.conf 配置项详解
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug;
#制定日志路径,级别。
#这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
# 负载均衡配置
upstream mysvr {
server 127.0.0.1:7878 weight=3;
server 127.0.0.1:7979 weight=1; # weight 权重分配
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
# 反向代理配置
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
nginx 常用命令
启动:start nginx
停止:nginx -s stop
重载配置文件:nginx -s reload (很常用,修改配置文件后直接 reload)
重启:nginx -s reopen
配置语法检查:nginx -c ./conf/nginx.conf -t
查看版本信息:nginx -v
查看详细信息:nginx -V (注意 V 大小写含义有区别)
拓展
-
403 forbidden root 指向的目录中没有
index.php,index.html的时候,会报403 forbidden。 -
正向代理和反向代理 正向不知客户端,反向不知服务端。
参考链接
40分钟Nginx教程(狂神说):www.bilibili.com/video/BV1F5…
Nginx 配置详解(菜鸟教程):www.runoob.com/w3cnote/ngi…
Nginx配置详解:www.cnblogs.com/knowledgese…
Nginx 403 报错的几种处理方法:www.cnblogs.com/williamjie/…