2

Edit 2

After more testing, I found that the site was connecting to my dev computer's Express. When running node ./bin/www on the dev computer, the site on AWS interacts with it and runs without a problem.
ExpressCommands

When the dev Express is not running, the site produces this error, “Http failure response for (unknown url): 0 Unknown Error.”
NoExpressRunning

The production server's Express was running in both of these cases, but the site did not connect with the production Express. The site is using nginx to fallback on the index.html when refreshing the page, though.

I've checked my Angular, server.js, and www files for a specific IP Address, but I did not see one, nor do I remember setting one. I did build the production Angular bundle on my dev computer (and FTPd it into the production system), but the Angular deployment docs seem to say that's okay.

My current nginx file is as follows:

upstream api {
    server <specific production server IP>:3000;
}

server {
    listen 80;

    server_name *******.compute.amazonaws.com;

    root /var/www/project/code/dist/project;

    index index.html index.htm;
    include /etc/nginx/mime.types;

    location / {

        try_files $uri$args $uri$args/ /index.html;
    }

    location /api/ {
        rewrite /asdf(.*) $1 break;
        proxy_pass http://api;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Does anyone have any suggestions to fix this or insights into what I'm doing wrong?


Original Post Below “Http failure response for (unknown url): 0 Unknown Error” in Angular 6 app w/ nginx

I am trying to deploy an Angular 6 application on an AWS Ubuntu server and nginx. The Angular app is in a dist folder. An Express server is running on http://localhost:3000. The trouble I'm having is when I try to submit form data I receive the following error: "Http failure response for (unknown url): 0 Unknown Error".

I read from this site that I could include code shown below, but it seems to conflict with try_files as was mentioned in a response to this post.

server {
      listen 80;
      server_name http://*******.compute.amazonaws.com;

      root <path/to/dist>;

      index index.html index.htm;

      location / {
          try_files $uri $uri/ /index.html;
/* Code recommended for CORS */
          if ($request_method = 'OPTIONS') {
               add_header 'Access-Control-Allow-Origin' '*';
               add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
               add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
               add_header 'Access-Control-Max-Age' 1728000;
               add_header 'Content-Type' 'text/plain; charset=utf-8';
               add_header 'Content-Length' 0;
               return 204;
        }
         if ($request_method = 'POST') {
              add_header 'Access-Control-Allow-Origin' '*';
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
              add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
              add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
         if ($request_method = 'GET') {
              add_header 'Access-Control-Allow-Origin' '*';
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
              add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
              add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }    

     }

    location /api {
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_set_header X-NginX-Proxy true;

           proxy_pass http://localhost:3000;
           proxy_redirect off;
    }

I've tried the following:

  • adding cors headers to my server.js (ref)
  • adding shorter headers without if statements to the location / and location /api blocks (ref, ref)
  • removing the try_files- the same cor message still appeared
  • adding try_files inside the if tags (ref)

EDIT 1

Since the initial posting, I've tried some work on the Express server as opposed to just the nginx file.

  • revised server.js based on several sites (ref)
  • tried revising the nginx using upstream (ref)
  • tried different options from the cors - npm documentation (ref)

Does anyone have any suggestions for how to fix this error message? I thought that my nginx set available-sites file did not incorporate cors correctly, but I can't see how to get it to work regardless of the changes I make. Any help would be greatly appreciated.

1 Answers1

0

It turns out that I used several calls to the api with "localhost" in the address. Once I replaced "localhost" with the production server's link, the setup worked fine.