1
votes

I'm trying to set up nginx to work with my backbonejs application and api server.

The API server is external and being routed through https://website.com/api/...

Essentially, I want any non-matched urls to be routed to /index.html for the backbone app to handle.

I've tried using try_files, but that just overrides my API. I've tried setting up another location where I check if the request is a GET and also if it doesn't match register or login or api, but that also doesn't work. Here's my server so far:

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";
    }

}

Currently, try_files overrides the API and just redirects to index.html. Any idea how I can get everything to play nicely with one another?

Here's what I want:

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

1 Answers

0
votes

Figured it out:

Add try_files @uri @rewrites; to Location / and also add the @rewrites function below.

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;
    }

}