求教 nginx 同一域名配置多端

2022-08-23 12:20:34 +08:00
 kestrelBright
server
    {
        listen 80;
        server_name www.xxx.cc;

        set $base_path "/xxx/xxx";

        root "${base_path}/vue/dist";

        index index.html;
        
        location / {
            try_files $uri $uri/ /index.html$is_args$args;

            if (!-f $request_filename) {
                 proxy_pass http://www.xxx1.cc;
            }
        }

        location  /bb/ {
            alias   "${base_path}/vue1/dist/";
            index  index.html index.htm;
            try_files $uri $uri/ /index.html$is_args$args;
        }

        location /bbapi/ {
            proxy_pass http://127.0.0.1:8095/;
        }
    }

现在必须是 www.xxx.cc/index.html 才能访问,try_files 没生效,想默认走前端,前端找不到就代理到后端。如果去掉 if ,try_files 有效,但后端访问不能有前缀。该怎么改?

还有如果加上 upstream

upstream xxx_server_name{
    server http://www.xxx1.cc;
 }
 server
    {
        listen 80;
        server_name www.xxx.cc;

        set $base_path "/xxx/xxx";

        root "${base_path}/vue/dist";

        index index.html;
        
        location / {
            try_files $uri $uri/ /index.html$is_args$args;

            if (!-f $request_filename) {
                 proxy_pass http:/xxx_server_name;
            }
        }

        location  /bb/ {
            alias   "${base_path}/vue1/dist/";
            index  index.html index.htm;
            try_files $uri $uri/ /index.html$is_args$args;
        }

        location /bbapi/ {
            proxy_pass http://127.0.0.1:8095/;
        }
    }

这样改了就代理就不到后端去了,求教大佬们

10188 次点击
所在节点    程序员
3 条回复
kestrelBright
2022-08-23 13:42:25 +08:00
想要去掉 index.html
putyy
2022-08-23 13:53:09 +08:00
location /api/ {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8801;
}
}

location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}


这是我惯用的配置,通常通过前缀标识 api, 其他的默认走前端
kestrelBright
2022-08-23 15:35:23 +08:00
@putyy 本想不用前缀,搞了半天没办法

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/874762

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX