0
votes

I have an angular app that loads on https://xxx.xxx.xxx.xxx/ being served behind a proxy-server (Nginx).

On the same server, I have hosted my backend(django) and when I try to access any 'API' services with domain-ip/api/users I get

Page not found (404)
Request Method: GET
Request URL:    http://xxx.xx.xx.xxx/api/users/

When I inspect the nginx logs, there's no recent log that is recorded to highlight any error.

  1. Here's my /etc/nginx/sites-available/default

    server { listen 80 default_server; listen [::]:80 default_server;

        #root points to my angular app
        root /home/bc_sparrow/bc_s/frontend/dist;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name 138.xx.55.xxx;
    
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
        }
    
        location /api/ {
                include proxy_params;
                proxy_pass http://unix:/run/gunicorn.sock;
                #proxy_set_header Host $http_host; #adding this line made things worse
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
        }
    

    }

With this configuration above, when I run these two commands I do not get an error.

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled
sudo nginx -t

Also when I run sudo systemctl status gunicorn I get

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-05-17 03:31:33 UTC; 29min ago
 Main PID: 14011 (gunicorn)
    Tasks: 4 (limit: 1151)
   CGroup: /system.slice/gunicorn.service
           ├─14011 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env           ├─14033 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env           ├─14038 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env           └─14039 /home/bc_sparrow/bc_s/backend/merchant/env/bin/python /home/bc_sparrow/bc_s/backend/merchant/env
May 17 03:31:33 SparrowsV1 systemd[1]: Stopped gunicorn daemon.
May 17 03:31:33 SparrowsV1 systemd[1]: Started gunicorn daemon.
May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14011] [INFO] Starting gunicorn 20.0.4    
May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14011] [INFO] Listening at: unix:/run/guniMay 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14011] [INFO] Using worker: sync
May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14033] [INFO] Booting worker with pid: 140May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14038] [INFO] Booting worker with pid: 140May 17 03:31:34 SparrowsV1 gunicorn[14011]: [2020-05-17 03:31:34 +0000] [14039] [INFO] Booting worker with pid: 140

Other sources suggested that I edit my default nginx profile without the (http://) on the location /api/ block

proxy_pass unix:/run/gunicorn.sock;

This change throws the error

[emerg] 14127#14127: invalid URL prefix in /etc/nginx/sites-enabled/defaul

I think I have exhausted all possible links that address this issue so any assistance is welcomed.

1

1 Answers

1
votes

For some reason/not here's my problem.

in dev environment my django end-points were accessible via

localhost:8000/cars/

and this was what i'd pushed to my remote-server.

in my nginx profile I expected that the location block

location /api/ {
...
}

will only get requests from domain.com/api/ and send em to my django app.

but my django app returned domain.com/cars/ not domain/api/cars/.

Answer:

I had to append 'api/' to my urls such that my backend server(django) returns/responds to

localhost:8000/api/resources

rather than

localhost:8000/resources