web - Nginx v1.14.2——重定向太多
问题描述
在我有限的 Nginx 经验中,当我在一个域上部署许多 WEB 项目时,我得到了太多的重定向。
我想使用一个“位置”来匹配多个项目。
文件路径:/mnt/vue/web
和/mnt/vue/admin
和/mnt/page/web
当我访问时https://${domain}/page/single
,这是错误的。它重定向到https://${domain}/page/single/index.html/index.html/index.html/...
当我访问时https://${domain}/vue/web
,没关系。
我已经尝试了多种变体,但它们似乎都不起作用。有任何想法吗?提前致谢。
这是我的 Nginx 配置文件:
server {
listen 443 http2;
server_name ${domain};
ssl on;
ssl_certificate /etc/nginx/conf.d/cert/web/web.pem;
ssl_certificate_key /etc/nginx/conf.d/cert/web/web.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# wechat verify
root /mnt/wechat;
# for vue h5-wechat
location ~ /(.*)/web {
root /mnt;
try_files $uri $uri/ /$1/web/index.html =404;
}
# for vue admin-web
location ~ /(.*)/admin {
root /mnt;
try_files $uri $uri/ /$1/admin/index.html =404;
}
# for api
location ~ /(.*)/api/(.*)$ {
proxy_pass http://172.16.184.254:$1;
rewrite /(.*)/api/(.*)$ /$2 break;
}
# for single pages !!!!! Where the problem occurred.
location ~ /(.*)/single/ {
alias /mnt/$1/web/;
index index.html;
}
# for cache
location ~ /(css|img|js|fonts|image)/ {
add_header Cache-Control "public, max-age=300";
}
location /files {
rewrite /files/(.*)$ /$1 break;
proxy_pass https://172.16.92.152;
}
}
server {
listen 80;
server_name ${domain};
return 301 https://$server_name$request_uri;
#rewrite ^(.*)$ https://$host$1 permanent;
}
解决方案
alias
在正则表达式 块中使用location
,您需要捕获整个 URI。有关详细信息,请参阅此文档。
例如:
location ~ ^/(.*)/single/(.*)$ {
alias /mnt/$1/web/$2;
index index.html;
}
推荐阅读
- spring-boot - 在休眠约束验证器中自动写入返回空异常
- mapbox - 启用 LocationComponent 的 Mapbox 缩放 (android)
- javascript - 如何使用查询字符串传递对象表?
- ansible - 如何仅在使用特定标签时运行 ansible 任务?
- pivot - Tableau:筛选器中的多个列
- ios - 如何在 iOS 上测试非自然安装 Appsflyer
- python - 如何在python中每小时出现一次声音?
- django - 如何为 Django 中的应用程序设置自定义权限?
- c# - 阅读亚马逊云配置的服务部分
- python - 如何为所需的查询参数添加文档?