- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解前端到底可以用nginx做什么由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
这篇文章是收集我在工作中经常会用到的nginx相关知识点,本文并不是基础知识的讲解更多的是一些方案中的简单实现.
注意的是,nginx的匹配优先顺序按照上面的顺序进行优先匹配,而且注意的是一旦某一个匹配命中直接退出,不再进行往下的匹配 。
剩下的普通匹配会按照最长匹配长度优先级来匹配,就是谁匹配的越多就用谁.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
server {
server_name website.com;
location
/document
{
return
701;
}
location ~* ^
/docume
.*$ {
return
702;
}
location ~* ^
/document
$ {
return
703;
}
}
curl -I website.com:8080
/document
702
# 匹配702 因为正则的优先级更高,而且正则是一旦匹配到就直接退出 所以不会再匹配703
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
server {
server_name website.com;
location ~* ^
/docume
.*$ {
return
701;
}
location ^~
/doc
{
return
702;
}
location ~* ^
/document
$ {
return
703;
}
}
curl http:
//website
.com
/document
HTTP
/1
.1 702
# 匹配702 因为 ^~精确匹配的优先级比正则高 也是匹配到之后支持退出
|
1
2
3
4
5
6
7
8
9
10
|
server {
server_name website.com;
location
/doc
{
return
702;
}
location
/docu
{
return
701;
}
}
# 701 前缀匹配匹配是按照最长匹配,跟顺序无关
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# html设置history模式
location / {
index index.html index.htm;
proxy_set_header Host $host;
# history模式最重要就是这里
try_files $uri $uri/
/index
.html;
# index.html文件不可以设置强缓存 设置协商缓存即可
add_header Cache-Control
'no-cache, must-revalidate, proxy-revalidate, max-age=0'
;
}
# 接口反向代理
location ^~
/api/
{
# 跨域处理 设置头部域名
add_header Access-Control-Allow-Origin *;
# 跨域处理 设置头部方法
add_header Access-Control-Allow-Methods
'GET,POST,DELETE,OPTIONS,HEAD'
;
# 改写路径
rewrite ^
/api/
(.*)$ /$1
break
;
# 反向代理
proxy_pass http:
//static_env
;
proxy_set_header Host $http_host;
}
location ~* \.(?:css(\.map)?|js(\.map)?|gif|svg|jfif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
# 静态资源设置七天强缓存
expires 7d;
access_log off;
}
|
因为不可能每一个项目开启一个域名,仅仅指向通过增加路径来划分多个网站,比如:
1
2
3
4
5
6
7
8
9
|
server {
listen 80;
server_name taobao.com;
index index.html index.htm;
# 通过正则来匹配捕获 [tmall|alipay]中间的这个路径
location ~ ^/([^\/]+)/(.*)$ {
try_files $uri $uri/ /$1
/dist/index
.html =404;
}
}
|
基于upstream做负载均衡,中间会涉及一些相关的策略比如ip_hash、weight 。
1
2
3
4
5
6
7
8
|
upstream backserver{
# 哈希算法,自动定位到该服务器 保证唯一ip定位到同一部机器 用于解决session登录态的问题
ip_hash;
server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)
server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)
}
|
如何根据headers头部来进行灰度,下面的例子是用cookie来设置 。
如何获取头部值在nginx中可以通过$http_xxx来获取变量 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
upstream stable {
server xxx max_fails=1 fail_timeout=60;
server xxx max_fails=1 fail_timeout=60;
}
upstream canara {
server xxx max_fails=1 fail_timeout=60;
}
server {
listen 80;
server_name xxx;
# 设置默认
set
$group
"stable"
;
# 根据cookie头部设置接入的服务
if
($http_cookie ~*
"tts_version_id=canara"
){
set
$group canara;
}
if
($http_cookie ~*
"tts_version_id=stable"
){
set
$group stable;
}
location / {
proxy_pass http:
//
$group;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
|
常用于ssr的node服务挂了返回500错误码然后降级到csr的cos桶或者nginx中 。
优雅降级主要用error_page参数来进行降级指向备用地址.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
upstream ssr {
server xxx max_fails=1 fail_timeout=60;
server xxx max_fails=1 fail_timeout=60;
}
upstream csr {
server xxx max_fails=1 fail_timeout=60;
server xxx max_fails=1 fail_timeout=60;
}
location ^~
/ssr/
{
proxy_pass http:
//ssr
;
# 开启自定义错误捕获 如果这里不设置为on的话 会走向nginx处理的默认错误页面
proxy_intercept_errors on;
# 捕获500系列错误 如果500错误的话降级为下面的csr渲染
error_page 500 501 502 503 504 = @csr_location
# error_page 500 501 502 503 504 = 200 @csr_location
# 注意这上面的区别 等号前面没有200 表示 最终返回的状态码已 @csr_location为准 加了200的话表示不管@csr_location返回啥都返回200状态码
}
location @csr_location {
# 这时候地址还是带着/ssr/的要去除
rewrite ^
/ssr/
(.*)$ /$1
break
;
proxy_pass http:
//csr
;
rewrite_log on;
}
|
这套方案不像常见的由nginx把png转为webp的方案,而是先经由图床系统(node服务)上传两份图片
然后通过nginx检测头部是否支持webp来返回webp图片,不支持的话就返回原图即可。这其中还做了错误拦截,如果cos桶丢失webp图片及时浏览器支持webp也要降级为png 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
http {
include
/etc/nginx/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"'
'"$proxy_host" "$upstream_addr"'
;
access_log
/var/log/nginx/access
.log main;
sendfile on;
keepalive_timeout 65;
# 开启gzip
gzip
on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text
/plain
text
/css
text
/xml
application
/json
application
/javascript
application
/rss
+xml application
/atom
+xml image
/svg
+xml;
# 负载均衡 这里可以是多个cos桶地址即可
upstream static_env {
server xxx;
server xxx;
}
# map 设置变量映射 第一个变量指的是要通过映射的key值 Accpet 第二个值的是变量别名
map $http_accept $webp_suffix {
# 默认为 空字符串
default
""
;
# 正则匹配如果Accep含有webp字段 设置为.webp值
"~*webp"
".webp"
;
}
server {
listen 8888;
absolute_redirect off;
#取消绝对路径的重定向
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
root
/usr/share/nginx/html
;
location / {
index index.html index.htm;
proxy_set_header Host $host;
try_files $uri $uri/
/index
.html;
add_header Cache-Control
'no-cache, max-age=0'
;
}
# favicon.ico
location =
/favicon
.ico {
log_not_found off;
access_log off;
}
# robots.txt
location =
/robots
.txt {
log_not_found off;
access_log off;
}
#
location ~* \.(png|jpe?g)$ {
# Pass WebP support header to backend
# 如果header头部中支持webp
if
($webp_suffix ~* webp) {
# 先尝试找是否有webp格式图片
rewrite ^/(.*)\.(png|jpe?g)$ /$1.webp
break
;
# 找不到的话 这里捕获404错误 返回原始错误 注意这里的=号 代表最终返回的是@static_img的状态吗
error_page 404 = @static_img;
}
proxy_intercept_errors on;
add_header Vary Accept;
proxy_pass http:
//static_env
;
proxy_set_header Host $http_host;
expires 7d;
access_log off;
}
location @static_img {
#set $complete $schema $server_addr $request_uri;
rewrite ^/.+$ $request_uri
break
;
proxy_pass http:
//static_env
;
proxy_set_header Host $http_host;
expires 7d;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|gif|svg|jfif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
proxy_pass http:
//static_env
;
proxy_set_header Host $http_host;
expires 7d;
access_log off;
}
error_page 500 502 503 504
/50x
.html;
location =
/50x
.html {
root
/usr/share/nginx/html
;
}
}
}
|
nginx的特点:
同时,nginx也可以实现高效的反向代理、负载均衡.
前端可以用nginx做些什么?
到此这篇关于前端到底可以用nginx做什么的文章就介绍到这了,更多相关前端使用nginx内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://juejin.cn/post/7064378702779891749 。
最后此篇关于详解前端到底可以用nginx做什么的文章就讲到这里了,如果你想了解更多关于详解前端到底可以用nginx做什么的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
全称“Java Virtual Machine statistics monitoring tool”(statistics 统计;monitoring 监控;tool 工具) 用于监控虚拟机的各种运
主要是讲下Mongodb的索引的查看、创建、删除、类型说明,还有就是Explain执行计划的解释说明。 可以转载,但请注明出处。  
1>单线程或者单进程 相当于短链接,当accept之后,就开始数据的接收和数据的发送,不接受新的连接,即一个server,一个client 不存在并发。 2>循环服务器和并发服务器
详解 linux中的关机和重启命令 一 shutdown命令 shutdown [选项] 时间 选项: ?
首先,将json串转为一个JObject对象: ? 1
matplotlib官网 matplotlib库默认英文字体 添加黑体(‘SimHei')为绘图字体 代码: plt.rcParams['font.sans-serif']=['SimHei'
在并发编程中,synchronized关键字是常出现的角色。之前我们都称呼synchronized关键字为重量锁,但是在jdk1.6中对synchronized进行了优化,引入了偏向锁、轻量锁。本篇
一般我们的项目中会使用1到2个数据库连接配置,同程艺龙的数据库连接配置被收拢到统一的配置中心,由DBA统一配置和维护,业务方通过某个字符串配置拿到的是Connection对象。  
实例如下: ? 1
1. MemoryCahe NetCore中的缓存和System.Runtime.Caching很相似,但是在功能上做了增强,缓存的key支持object类型;提供了泛型支持;可以读缓存和单个缓存
argument是javascript中函数的一个特殊参数,例如下文,利用argument访问函数参数,判断函数是否执行 复制代码 代码如下: <script
一不小心装了一个Redis服务,开了一个全网的默认端口,一开始以为这台服务器没有公网ip,结果发现之后悔之莫及啊 某天发现cpu load高的出奇,发现一个minerd进程 占了大量cpu,googl
今天写这个是为了 提醒自己 编程过程 不仅要有逻辑 思想 还有要规范 代码 这样可读性 1、PHP 编程规范与编码习惯最主要的有以下几点: 1 文件说明 2 funct
摘要:虚拟机安装时一般都采用最小化安装,默认没有lspci工具。一台测试虚拟网卡性能的虚拟机,需要lspci工具来查看网卡的类型。本文描述了在一个虚拟机中安装lspci工具的具体步骤。 由于要测试
1、修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统
目录 算术运算符 基本四则运算符 增量赋值运算符 自增/自减运算符 关系运算符 逻
如下所示: ? 1
MapperScannerConfigurer之sqlSessionFactory注入方式讲解 首先,Mybatis中的有一段配置非常方便,省去我们去写DaoImpl(Dao层实现类)的时间,这个
Linux的网络虚拟化是LXC项目中的一个子项目,LXC包括文件系统虚拟化,进程空间虚拟化,用户虚拟化,网络虚拟化,等等,这里使用LXC的网络虚拟化来模拟多个网络环境。 本文从基本的网络设备讲
? 1
我是一名优秀的程序员,十分优秀!