nginx配置动静分离
原理:静态页面和里面的图片js资源有很多,如果这些都放到tomcat服务器,nginx每次都要转发反向代理。把一些今天资源和图片、js代码放到nginx服务器上就节省很多资源。
以前的做法:都放到tomcat上
配置nginx:
最简单配置静态资源:添加多个location{}
#user nobody;
worker_processes 1;events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;sendfile on;
keepalive_timeout 65;
#2台反向代理负载均衡,默认轮训
#按比例负载均衡
upstream httpds{
server 192.168.208.200:80 weight=8;
server 192.168.208.201:80 weight=2;
server 192.168.208.202:80 weight=1;}
server {
listen 80;
server_name localhost;location / {
#会被代理到这个地址,只写一个代理,需要写全名,配置外网#proxy_pass http://www.baidu.com;
#配置内网
proxy_pass http://https;
}
#css静态资源,本机路径下的资源
local /css{
root html;
index index.html index.htm;
}
#image静态资源,本机路径下的资源
local /image{
root html;
index index.html index.htm;
}
#js静态资源,本机路径下的资源
local /js{
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
}
正则配置动静分离:
#user nobody;
worker_processes 1;events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;sendfile on;
keepalive_timeout 65;
#2台反向代理负载均衡,默认轮训
#按比例负载均衡
upstream httpds{
server 192.168.208.200:80 weight=8;
server 192.168.208.201:80 weight=2;
server 192.168.208.202:80 weight=1;}
server {
listen 80;
server_name localhost;location / {
#会被代理到这个地址,只写一个代理,需要写全名,配置外网#proxy_pass http://www.baidu.com;
#配置内网
proxy_pass http://https;
}
location ~*/(js|image|css){
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
}
静态文件和缓存往前面放,还是比较好的。不要放到后台服务器。
配置URLRewrite的使用场景:
请求地址:http://192.168.208.200:8080/index?number=3
太暴露,修改为:
http://192.168.208.200:8080/3
rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,
重定向到replacement,结尾是flag标记。rewrite <regex> <replacement> [flag]
关键字 正则 替代内容 flag标记关键字:其中关键字error_log不能改变
正则:per1兼容正则表达式语句进行规则匹配
替代内容:讲座恒泽匹配的内容替换成replacement
flag标记:rewrite支持的flag标记rewrite参数的标签段位置:
service,location,ifflag标记说明:
last #本条规则匹配
break#本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent#返回301永久重定向,浏览器地址栏会显示跳转后的URL地址rewritr ^/([0-9]+).html$ /index.html?number=$1 break;
配置文件:
#user nobody;
worker_processes 1;events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;sendfile on;
keepalive_timeout 65;
#2台反向代理负载均衡,默认轮训
#按比例负载均衡
upstream httpds{
server 192.168.208.200:80 weight=8;
server 192.168.208.201:80 weight=2;
server 192.168.208.202:80 weight=1;}
server {
listen 80;
server_name localhost;location / {
#会被代理到这个地址,只写一个代理,需要写全名,配置外网#proxy_pass http://www.baidu.com;
#配置内网
rewrite ^/([0-9]+)/index$ /index?number=$1 break;
proxy_pass http://https;}
location ~*/(js|image|css){
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
}
网关配置:
重启防火墙:
systemctl restartfirewalld重载规则
firewall-cmd --reload查看已配置规则
firewall-cmd --list-all指定端口和ip访问
firewall-cmd --permament --add-rich-rule="rule family="1pv4" source address="192.168.44.101" port protocol="tcp" port="8080" accept"移除规则
firewall-cmd --permament --remove-rich-rule="rule family="1pv4" source address="192.168.44.101" port protocol="tcp" port="8080" accept"
配置网关nginx:也就是多台主机同时做负载均衡
#user nobody;
worker_processes 1;events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;sendfile on;
keepalive_timeout 65;
#2台反向代理负载均衡,默认轮训
#按比例负载均衡
upstream httpds{
server 192.168.208.200:80 weight=8 down;#这俩台服务器安装nginx,网关服务器
server 192.168.208.201:80 weight=2;
server 192.168.208.202:8080 weight=1;}
server {
listen 80;
server_name localhost;location / {
#会被代理到这个地址,只写一个代理,需要写全名,配置外网#proxy_pass http://www.baidu.com;
#配置内网
rewrite ^/([0-9]+)/index$ /index?number=$1 break;;
proxy_pass http://https;}
location ~*/(js|image|css){
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
}
nginx配置动静分离相关推荐
- Nginx——配置动静分离
Nginx的动静分离是把不同的资源放到不同的服务器中,动态资源可以放到tomcat中,而静态资源可以放到其他位置 所谓动静分离,是为了加快网站的解析速度,把动态页面和静态页面分别部署到不同的服务器上来 ...
- Nginx配置——动静分离
文章目录 0. 引言 1. 动静分离 1.1 何为动静分离 1.2 动静分离目的 2. 如何配置 2.1 资源准备 2.2 进行 Nginx 配置 2.3 测试结果 3. location 4. Ur ...
- nginx配置动静分离简单配置示例
- nginx php分离,nginx-php配置动静分离
实验目的:nginx-php配置动静分离 实验环境: 主机 192.168.88.100 NGINX服务器 主机 192.168.88.102 PHP和MYSQL服务器 注意:想要手工编译必须安装gc ...
- Docker构建Nginx+Tomcat动静分离架构
随着主流Nginx WEB服务器的发展,现在基于Nginx的WEB服务器已广泛应用于各大互联网企业.今天我们来使用docker构建我们的Linux+Nginx+Tomcat动静分离服务器. 1) ...
- nginx+tomcat动静分离结构
本文采用另一种策略对动静分离进行演示,它的大致结构如图 2 所示. 图 2. 本文设计的动静分离结构 在本文中,我们将静态资源放在 A 主机的一个目录上,将动态程序放在 B 主机上,同时在 A 上安装 ...
- tomcat 如何跳转到apache_第二十期:基于tomcat部署jforum站点,并结合nginx实现动静分离...
一. 基于tomcat部署站点,并nginx实现动静分离 1.1 Tomcat部署 1.1.1 配置jdk #二进制安装 #解压 [root@node2local]# tar -xzvf jdk ...
- K8S高可用集群架构部署 dashborad插件部署 Nginx实现动静分离 K8S在线升级
K8S官方文档 注意:该集群每个master节点都默认由kubeadm生成了etcd容器,组成etcd集群.正常使用集群,etcd的集群不能超过一半为down状态. docker的namespace: ...
- 使用nginx实现动静分离的负载均衡集群
架构图 本次要实现的架构图: 工作中我们希望这样: 静态文件处理:可以使用nginx 或apache 动文件处理: apache ,tomcat 图片文件处理: squid 我们可以使用nginx实现 ...
最新文章
- svn update 报错,必须先cleanup,然后cleanup失败解决方法
- python middle()_Python自学笔记(七):函数
- pycharm的安装和使用
- 二、数据库原理-设计理论
- 光通信调制方式MATLAB仿真,基于LED的紫外光通信调制方式研究
- 八数码c语言编程深度搜索,广度优先搜索解 八数码, 求意见, 求bug/
- idea导入tomcat源码
- linux上安装Weblogic11g 详解
- 企业文件分享/共享有哪些安全方式
- 宝塔环境PHP网站要配置HTTPS,SSL证书如何申请
- css3中的渐变色效果
- MybatisX 使用 示例
- 2021年12月27日多载波NOMA论文阅读
- Singleton模式的学习
- tar -zxvf xxx.tar,gz --Linux中解压语句-zxvf的含义
- 分享Python简短代码,实现TXT转换MySQL文件。
- 智能化防杀未知电脑病毒探讨
- 知识付费——一起瞄准行业新趋势
- win_size exceeds image extent
- html get请求打开新页面,通过AF get 请求html页面总是失败的问题