nginx配置动静分离
Posted 月疯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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配置动静分离的主要内容,如果未能解决你的问题,请参考以下文章