Keep all fly apps on the same origin

I’m using nginx to proxy my fly apps. This is my dev nginx config. I’m using docker compose to keep them in the same network.

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {

    # prevent css, js files sent as text/plain objects
    include /etc/nginx/mime.types;

    server {
        listen 443 ssl;
        client_max_body_size 10M;

        ssl_certificate /etc/nginx/ssl/localhost.crt;
        ssl_certificate_key /etc/nginx/ssl/localhost.key;

        location / {
            proxy_pass http://client:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

         location /api {
             proxy_set_header Host $http_host;
             proxy_pass http://server:8000;
         }
         location /admin {
         # Copy the same as /api
            proxy_set_header Host $http_host;
            proxy_pass http://server:8000;
         }
         location /static/ {
            proxy_pass http://server:8000;
            autoindex on;
            autoindex_exact_size off;
        }

    }
}

This is my nginx for deployment:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {

    # prevent css, js files sent as text/plain objects
    include /etc/nginx/mime.types;

    server {
        listen 443;
        listen [::]:443;
        client_max_body_size 10M;

        location / {
            proxy_pass https://client.fly.dev/;
            proxy_http_version 1.1;
            proxy_set_header Host $http_host;
            proxy_ssl_server_name on;
        }

         location /api {
             proxy_set_header Host $http_host;
             proxy_pass https://server.fly.dev;
             proxy_ssl_server_name on;
         }
         location /admin {
         # Copy the same as /api
            proxy_set_header Host $http_host;
            proxy_pass https://server.fly.dev;
            proxy_ssl_server_name on;
         }
         location /static/ {
            proxy_pass https://server.fly.dev;
            autoindex on;
            autoindex_exact_size off;
            proxy_ssl_server_name on;
        }

    }
}
  1. since I’m making different apps they are on differnet domain names which causes cors errors. how can i fix this?

Hi,

You will indeed run into CORS issues across domains however that should only apply between the browser and server. What domain the request is actually served from (proxied to) would be hidden, and so I’m not sure why that would be causing issues. Unless … your app is looking at the host header to decide on its CORS response, seeing the wrong one and so then not returning CORS headers.

I’d make a request in a browser, see what headers are being sent to the server, and what the response contains.

I also wonder whether you’d be better off proxying using the internal, private network Private Networking · Fly Docs since you mention they are all within the same network (I assume that means … same organisation?). That would then avoid the additional overhead (TLS, DNS etc) of using a public domain name.fly.dev e.g

proxy_pass http://app-name.internal:8080 (or whatever port you are using)

That may even help with CORS but this is all a guess. Not sure how the internal domains are resolved but it’s worth trying.

My domain name is app.com. When I do app.com/api, the request url header returns server.fly.dev/api. On my local dev environment when I call the server everything stays on localhost so there are no CORS errors. I want everything to be on app.com, similar to my dev environment.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.