基本介绍
Nginx是一个高性能的HTTP和反向代理的web服务器,也提供IMAP、POP3、SMTP等服务
web服务器 = web服务软件 + PC机 服务器主要功能为 对外共享资源
Nginx主要功能:虚拟主机、反向代理、负载均衡、动静分离
基本安装
1.准备nginx文件 cp nginx-1.8.1.tar.gz /usr/local 2.解压nginx文件 tar -zxvf /usr/local/nginx-1.8.1.tar.gz 3.安装插件 yum install pcre yum install pcre-devel yum install zliv yum insatll zlib-devel 4.nginx配置 cd nginx-1.8.1 && ./configure --prefix = /usr/local/nginx 5.nginx安装 make && make install 6.进入文件 cd /usr/local/nginx/sbin 7.启动文件 ./nginx ./nginx -s reload 重启 文件解析 conf nginx配置文件 html 类似webapps logs 日志文件 sbin 启动命令
虚拟主机
server{ listen 9999; server_name localhost; }
反向代理
location ~ \.jpg${ proxy_pass http://111.111.111.111:8080 }
负载均衡
此处为一机一次 upstream buliyat{ server 111.111.111.111:8080; server 222.222.222.222:8080; } server{ listen 9999; server_name localhost; location /buliyat.html{ root buliyat; } location ~ \.jpg${ proxy_pass http:///buliyat } } 此处设计权重 两机请求发送比例为1:2 upstream buliyat{ server 111.111.111.111:8080 weight=1; server 222.222.222.222:8080 weight; } server{ listen 9999; server_name localhost; location /buliyat.html{ root buliyat; } location ~ \.jpg${ proxy_pass http:///buliyat } } 此处为hash分配 根据ip的hash值进行分配主机 upstream buliyat{ ip_hash; server 111.111.111.111:8080; server 222.222.222.222:8080; } server{ listen 9999; server_name localhost; location /buliyat.html{ root buliyat; } location ~ \.jpg${ proxy_pass http:///buliyat } }
动静分离
动静分离 静态资源放到nginx 动态资源放到tomcat
main.js
window.onload = function(){ var btn = document.getElementById("btnClick"); btn.onclick = function(){ console.log("测试js文件引入"); } }Controller
@RestController @RequestMapping("/user") public class UserController { @RequestMapping("/test") public Object test() { return "测试动态资源成功"; } }配置Nginx
server{ listen 8888; server_name localhost; location /{ proxy_pass http://111.111.111.111:8080; } location ~* \.(jpg|png|gif|js|css|html)${ root /usr/local/webapp; expires 1d; } }index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>动静分离</title> </head> <body> <img src="img/meinv.jpg"/> <div>这里是测试</div> <button id="btnClick">点击</button><br /> <a href="/user/test"> 发送请求到Tomcat</a> </body> <script type="text/javascript" charset="utf-8"> window.onload = function(){ var btn = document.getElementById("btnClick"); btn.onclick = function(){ console.log("测试js文件"); } } </script> </html>