V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kestrelBright
V2EX  ›  程序员

求教 nginx 同一域名配置多端

  •  
  •   kestrelBright · 2022-08-23 12:20:34 +08:00 · 10171 次点击
    这是一个创建于 583 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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/;
            }
        }
    

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

    3 条回复    2022-08-23 15:35:23 +08:00
    kestrelBright
        1
    kestrelBright  
    OP
       2022-08-23 13:42:25 +08:00
    想要去掉 index.html
    putyy
        2
    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
        3
    kestrelBright  
    OP
       2022-08-23 15:35:23 +08:00
    @putyy 本想不用前缀,搞了半天没办法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3487 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:08 · PVG 08:08 · LAX 17:08 · JFK 20:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.