nginx是一个反向代理工具
主要功能模块划分
user wyl;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid pid/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#include include 文件目录/*.conf;
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream 自定义名 {
#ip_hash;
server localhost:8081;
server localhost:8082;
#fair;
#server localhost:8081 weight=90;
#server localhost:8082 weight=10;
}
server {
listen 80;
server_name localhost;
#error_page 404 /404.html;
location / {
root html;
proxy_pass http://localhost:8082/test/isId/This_server_1a;
#proxy_pass http://http块负载均衡的自定义名/test/isId/This_server_1a;
index index.html index.htm;
}
}
}
全局块
配置nginx服务器的用户(组)
user wyl;
允许创建的工作进程数,匹配并发数
worker_processes 2;
日志存储
error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
进程pid
pid pid/nginx.pid;
events块
配置影响nginx服务器或与用户的网络连接
支持的最大连接数
worker_connections 1024;
http块
引入文件类型和MIME类型映射表,里面有各种文件类型映射
include mime.types;
默认拓展名设置,application/octet-stream默认未知的应用程序文件
default_type application/octet-stream;
引入分配置,比如你可能会配置很多的server块,这样你整个配置文件就很臃肿,此时就可将server块分到一个配置文件里
include include 文件目录/*.conf;
用于定义日志格式,以下为默认
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 logs/access.log main;
是否使用sendfile传输文件
sendfile on;
连接超时时间
keepalive_timeout 65;
开启gzip压缩
gzip on;
用于负载均衡
将"自定义名"代替地址放入location中的proxy_pass中
weight:分配权重
ip_hash:让一段时间内同一个客户ip一直访问同一个地址
fair:外部负载模块,需要安装外部模块,根据后台接口的反应速度负载
upstream 自定义名 {
#ip_hash;
server localhost:8081;
server localhost:8082;
#fair;
#server localhost:8081 weight=90;
#server localhost:8082 weight=10;
}
server块
(相当于一台虚拟机服务模块,可定义多个server)
访问的端口和服务名地址
listen 80;
server_name localhost;
错误页
error_page 404 /404.html;
location块
location / 通用匹配,任何请求都会匹配到
location /ab 匹配/ab、/abc等文件夹,类似于模糊匹配
location =/ab 只匹配/ab文件夹,类似于精确匹配
location ~/ab 只匹配/ab文件夹,指定正则表达式区分大小写
location ~*/ab 只匹配/ab、/AB文件夹,指定正则表达式不区分大小写
首页根目录
root html;
设置默认首页
index index.html index.htm;
代理地址
proxy_pass http://localhost:8082/test/isId/This_server_1a;
当然若实现负载均衡代理地址就配合http块的自定义名
proxy_pass http://http块负载均衡的自定义名/test/isId/This_server_1a;