首页 > 解决方案 > 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;
}

标签: webnginx

解决方案


alias正则表达式 块中使用location,您需要捕获整个 URI。有关详细信息,请参阅此文档

例如:

location ~ ^/(.*)/single/(.*)$ {
    alias /mnt/$1/web/$2;
    index index.html;
}

推荐阅读