Nginx服务器的强大功能可以有很多用途,例如不仅可以做静态资源的web服务器,还可以以集群方式处理动态资源的请求,本文就大致建立这些功能;
一,利用Nginx做动静态资源分离,分别处理静态资源和动态资源
目的是利用nginx不仅作为静态资源服务器,例如 html 页面,js,css,图片等,同时还利用nginx把动态资源请求转发到集群中的各个web服务器(如Tomcat)处理, 这样静态资源分离出来, 动态资源又由集群处理,这将大大加快服务器的相应,处理各个请求,关键的就是 nginx 配置,如下;
#user nobody;
worker_processes 1; #工作进程的个数,一般与计算机的cpu核数一致
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
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"';
#access_log logs/access.log main;
sendfile on;
# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,
# 如果用来进行下载等应用磁盘 IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,
# 降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#长连接超时时间,单位是秒
gzip on;
#启用Gizp压缩
#服务器的集群
upstream myGroupServer.com { #服务器集群名字
#ip_hash; #此处不启用是为了看到不同的群集服务器
#当不启用ip_hash可以看到访问按设定的权重随机转发到不同的群集服务器,
#出现的问题是如果访问在某服务器已经登录,当每次刷新后重新访问有可能被转发到另一群集服务器上,这导致在该新服务器上出现未登录的情况;
#当启用ip_hash可以解决登录session丢失假象的问题,因同一IP来源访问经ip_hash算法后都会转发到相同的某个集服务器上,
#出现的问题同一IP都会访问相同的某个群集服务器,对用户来说可能出现仅有一个服务器的假象,并且如果正好该群集服务器司机,请求则重转发到另一服务器;
server 127.0.0.1:18080 weight=1;
server 127.0.0.1:28080 weight=2;
#server 127.0.0.1:38080 weight=2;
#集群服务器列表,每个服务器配置weight是权重的意思,权重越大,分配的概率越大。
#假设虚拟配置一个不存在的群集服务器(如上端口38080),出现的问题是nginx同样会把请求按指定的权重随机转发到该不存在的服务器上, #并尝试连接,直到连接达到keepalive_timeout设定时间,如果还没有连接上则按新一轮把请求重新转发,如果连上某个群集服务器则响应请求,
#如果都没连上超时了则返回5xx错误;
}
#当前的Nginx的配置
server {
listen 80;
#监听默认的web 80 端口,可以改成其他端口
server_name localhost;
# 当前服务的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $host;
#反向代理服务器把请求头的host改为跟来源处一样,如果不设置则就跟proxy_pass指令值一样,会引起URL错误或丢失等问题, 关键
proxy_set_header X-Forwarded-For $remote_addr;
#如果存在多级反向代理需要设置该指令,这个值是经过代理之后的前一个代理 $remote_addr值(一般即IP),如第三级代理保存第二级代理IP,关键
proxy_pass http://myGroupServer.com;
#对本服务器请求 localhost:80 将转发到名为 myGroupServer.com 的群集处理
proxy_redirect default;
#指令作用是对发送给客户端的URL进行修改,
#使用 default 参数,将根据location和proxy_pass参数的设置来决定
}
# 静态页面资源处理, 如 html 页面,js,css
location /www/ {
root data;
#相对路径,相对nginx安装目录下的data为根目录,work ok
#root E:/J2EEServer01/nginx-1.14.0/data;
#也可指定绝对路径,如window系统下,注意是/分隔符,work ok
index index.htm index.html;
#指定默认页面,如果匹配路径没有指定页面名称,则会从index的配置中查找名称,再找不到则报错
}
# 静态页面资源处理,对www开头的URL会映射到本服务器的 /data 目录下,例如:
# http://localhost:80/www/pages/page01.html 会映射到本服务器的 /data/www/pages/page01.html
# 即 root 配置的路径加上URL中匹配部分起的直到结尾,即为服务器的真实路径
# 静态图片资源处理,
location /images {
#root data;
#相对路径,相对nginx安装目录下的data为根目录,work ok
root E:/J2EEServer01/nginx-1.14.0/data;
#也可指定绝对路径,如window系统下,注意是/分隔符,work ok
index index.jpg index.gif index.png;
#指定默认图片名称,如果匹配路径没有指定图片名称,则会从index的配置中查找名称,再找不到则报错
}
# 静态图片资源处理,对images开头的URL会映射到本服务器的 /data 目录下,例如:
# http://localhost:80/images/news/newspic.jpg 会映射到本服务器的 /data/images/news/newspic.jpg
# 即 root 配置的路径加上URL中匹配部分起的直到结尾,即为服务器的真实路径
#location / {
# root html; #本地根路径
# index index.html index.htm; #默认页面列表
#}
error_page 404 /404.html;
location = /404.html {
root html;
index 404.html 404.htm;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
index 50x.html 50x.htm;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#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;
# 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;
# }
#}
}
通过以上配置 nginx 既可以作为静态资源web服务器,处理静态资源,同时动态资源请求又可以转发到集群中的动态web服务器去处理;当然在系统架构时需要把上传的图片通过分布式方式(如dubbo处理)上传到静态资源服务器,同样页面等资源也类似处理,各个静态资源的路径需要按 nginx 配置规划好;
二,location 配置和处理顺序规则
1、“ =”前缀的指令严格匹配这个查询。如果找到,停止搜索。
2、所有剩下的常规字符串,匹配最精确的(一般最长的那个)。如果这个匹配使用^〜前缀,搜索停止。
3、正则表达式,在配置文件中是从上往下匹配的
4、如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用
特殊情况:
两种情况下,不需要继续匹配正则 location :
( 1 ) 当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普 通 location 一旦匹配上,则不需要继续正则匹配。
( 2 ) 当普通location 恰好严格匹配上 ,不是最大前缀匹配,则不再继续匹配正则;