在一个Nginx虚拟主机中,我添加了这些位置信息,其中两个使用正则表达式:
但是我可以像这样做,来“隐藏”正则表达式吗?
我认为这样可以避免解析任何正则表达式,用于匹配请求的
同样,我已将视频上传与其他上传分开,因为视频上传使用了一个正则表达式:
location ~ /-/pubsub/subscribe/(.*)$ {
# subscribe to websocket
}
location ~ /-/pubsub/publish/(.*)$ {
# websocket publish endpoint
}
location / {
# reverse proxy to the application server
}
但是我可以像这样做,来“隐藏”正则表达式吗?
location /-/pubsub/ { <-- can be tested without any regex matching
location ~ subscribe/(.*)$ { ... }
location ~ publish/(.*)$ { ... }
}
location / {
# reverse proxy
}
我认为这样可以避免解析任何正则表达式,用于匹配请求的
location /
,因为它们将与location /-/pubsub/
(无正则表达式)进行比较,而不是与location ~ /-/pubsub/whatever/(.*)$
(有正则表达式)进行比较,对吗?同样,我已将视频上传与其他上传分开,因为视频上传使用了一个正则表达式:
location /-/uploads/public/video/ {
location ~ \.(mp4|m4v|m4a)$ { <-- regex matching for videos only
mp4;
}
}
location /-/uploads/public/ {
# all other files: no regex matching needed
}
但我不确定为了避免正则表达式而进行这种有点复杂的配置是否有意义。将视频保存在另一个文件夹中,只是为了避免使用正则表达式。这样做会更快吗?值得这么做吗?
location ~ /-/pubsub/subscribe/(.*)$
应该完全替换为无正则表达式的location /-/pubsub/subscribe/
。 - Alexey Ten