1

我正在尝试设置 nginx 以与我的骨干网应用程序和 api 服务器一起使用。

API 服务器是外部的,并通过https://website.com/api/路由...

本质上,我希望将任何不匹配的 url 路由到 /index.html 以供骨干应用程序处理。

我试过使用try_files,但这只是覆盖了我的 API。我尝试设置另一个位置,在该位置检查请求是否为 GET 以及是否不匹配registeror loginor api,但这也不起作用。这是我server到目前为止:

server {
    listen 80; ssl off;
    listen  443 ssl;
    server_name app.io;
    ssl_certificate /etc/nginx/conf/ssl.crt;
    ssl_certificate_key /etc/nginx/conf/app.key;

    root /home/ubuntu/app/public;

    access_log /var/log/nginx/app.access.log;
    error_log /var/log/nginx/app.error.log;

    index index.html;

    location / {
                    if ($scheme = "http") {
                            rewrite ^ https://$http_host$request_uri? permanent;
                    }
            }

    location ~ ^/(api)|(auth).*$ {
        proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass https://app.aws.af.cm;
    }

    location ~ ^(/(register)|(login)).*$ {
        proxy_set_header X-Forwarded-Host $host;
                    proxy_set_header X-Forwarded-Server $host;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # GETs only
        limit_except POST {
            proxy_pass https://app.aws.af.cm;
        }
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

}

目前,try_files 覆盖 API 并仅重定向到 index.html。知道我怎样才能让所有东西都能很好地相互配合吗?

这就是我想要的:

if / - /index.html
else if /api/*|/auth/* - external proxy
else if /login|/register - POST - external proxy
else /* - /#$1
4

1 回答 1

0

弄清楚了:

添加try_files @uri @rewrites;Location /添加以下@rewrites功能。

server {
    listen 80; ssl off;
    listen  443 ssl;
    server_name app.io;
    ssl_certificate /opt/nginx/conf/ssl.crt;
    ssl_certificate_key /opt/nginx/conf/app.key;

    root /home/ubuntu/app/public;

    access_log /var/log/nginx/app.access.log;
    error_log /var/log/nginx/app.error.log;

    index index.html;

    location / {
                    if ($scheme = "http") {
                            rewrite ^ https://$http_host$request_uri? permanent;
                    }

                               try_files $uri @rewrites;
            }

    location ~ ^/(api)|(auth)|(logout)|(register)|(login).*$ {
        proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass https://app.cm;
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location @rewrites {
            rewrite ^/.+ /#$uri redirect;
    }

}
于 2013-02-03T00:08:04.370 回答