Nginx多个路由和不同的服务器

时间:2018-09-05 22:01:08

标签: nginx

我需要使用nginx在/和(admin | api | auth)上将SPA应用程序配置为django proxy_pass,这是我当前的nginx配置设置:

server {
    listen 80;
    server_name localhost;

    access_log  off;
    charset     utf-8;
    client_max_body_size 750M;

    location ^~ /static/ {
        root /opt/youtubeapp;
        expires max;
        break;
    }

    location ^~ /media/ {
        root /opt/youtubeapp;
        expires max;
        break;
    }

    location ~ ^/(robots\.txt|favicon\.ico\.html)$ {
        root /opt/youtubeapp/static/;
    }

    location / {
        root /opt/bulkvideo-front;
        expires max;
        break;
    }

    location ^~ /(admin|api|auth) {
        proxy_pass http://127.0.0.1:4600/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Scheme $scheme;

        # added 4 lines above. Remove or comment it if fail
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods "POST, GET, OPTIONS";
        add_header Access-Control-Allow-Headers "Origin, Authorization, Accept";
        add_header Access-Control-Allow-Credentials true;

    }

}

我当前的配置有一个问题,当我的SPA向http://localhost/auth/signin/服务器发送请求并返回错误消息Page not foundRequest URL: http://localhost/signin/时,问题是nginx,因为未发送完整的路由路径,我该怎么办解决nginx中的这个问题?

1 个答案:

答案 0 :(得分:1)

您只需要更改此内容:

proxy_pass http://127.0.0.1:4600/;

对此:

proxy_pass http://127.0.0.1:4600;

您添加到proxy_pass指令的任何内容,甚至只是一个斜杠,都将替换原始请求url中与位置块匹配的部分。