2

I'm trying to figure out how to load static files, other than index.html, in nginx with Ubuntu 18.04.4 Desktop.

I added to /etc/nginx/conf.d/default.conf the following lines:

server {
    location / {
        root /home/marco/webMatters/vueMatters/ggc/src/components/auth/weights;
    }
}

But I'm getting this error in console log: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

which, according to the people intervened in the github issue, should be related to a mis-configuration in the web-server for static files serving: https://github.com/justadudewhohacks/face-api.js/issues/598#issuecomment-626346393

Following the indications here: https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/ I modified the lines in /etc/nginx/conf.d/default.conf as follows:

server {
    root /home/marco/webMatters/vueMatters/ggc/src/components/auth/weights;

    location / {
        try_files $uri /weights/ssd_mobilenetv1_model-shard1;
    }
}

I also tried to add these two options:

server {
    root /home/marco/webMatters/vueMatters/ggc/src/components/auth/weights;

    location / {
        sendfile on;
        tcp_nopush on;
        try_files $uri /weights/ssd_mobilenetv1_model-shard1;
    }
}

drwxr-xr-x 2 root marco 4,0K apr 7 12:57 weights

But still got the problem

Update 1)

Update 2)

In /etc/nginx/conf.d/default.conf I added to the first server block the location /weights. But I'm not so sure about the quality and correctness of this location.

server {
    listen 443 ssl http2 default_server;
    server_name ggc.world;
    ssl_certificate /etc/letsencrypt/live/ggc.world-
0002/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ggc.world-
0002/privkey.pem; # managed by Certbot

    ssl_trusted_certificate /etc/letsencrypt/live/ggc.world/chain.pem;

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by 
Certbot

    ssl_session_timeout 5m;
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-    
    draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
    #ssl_stapling on;
    #ssl_stapling_verify on;

    access_log /var/log/nginx/ggcworld-access.log combined;

    add_header Strict-Transport-Security "max-age=31536000";
    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        proxy_pass http://127.0.0.1:8080;
        #proxy_pass http://127.0.0.1:2000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        #proxy_set_header Host $host;
    }

    location /weights {
      #sendfile on;
      #tcp_nopush on;
      #root /home/marco/webMatters/vueMatters/ggc/
/src/components/auth/weights;
      try_files $uri $uri/ /home/marco/webMatters/vueMatters/ggc/src
/components/auth/weights/ssd_mobilenetv1_model-shard1;
      proxy_pass http://127.0.0.1:9091/weights/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      # Following is necessary for Websocket support
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

}

I tried through the web. But the error.log says:

2020/05/12 20:14:31 [error] 13354#13354: *1 connect() failed (111: 
Connection refused) while connecting to upstream, client: 
109.116.164.135, server: ggc.world, request: "GET /sockjs-
node/info?t=15893072$
2020/05/12 20:14:33 [error] 13354#13354: *1 connect() failed (111: 
Connection refused) while connecting to upstream, client: 
109.116.164.135, server: ggc.world, request: "GET /sockjs-
node/info?t=15893072$

Update 3)

Following the indications found here: Nginx 404 not found when trying to load files in the directory I moved all the static files to a directory /home/marco/www, changed the ownership of this folder to root, and changed the mod of its content to 755: sudo chown root www + sudo chmod 755 www

then in /etc/nginx/conf.d/default.conf I modified the configuration as follows:

server {
    listen 443 ssl http2 default_server;
    server_name ggc.world;
    ssl_certificate /etc/letsencrypt/live/ggc.world-
0002/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ggc.world-
0002/privkey.pem; # managed by Certbot

    ssl_trusted_certificate /etc/letsencrypt/live/ggc.world/chain.pem;

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by 
Certbot

    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-
    draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:
!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;

    access_log /var/log/nginx/ggcworld-access.log combined;

    add_header Strict-Transport-Security "max-age=31536000";
    location = /favicon.ico { access_log off; log_not_found off; }

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

location /weights {
  try_files $uri $uri/ /home/marco/www/;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

}

But when trying to access the static file through the url "https://ggc.world/weights/mtcnn_model-weights_manifest.json" I get this error : "Uncaught Syntax Error: Unexpected token '<' "

enter image description here

How to solve the problem? Looking forward to your kind help. Marco

8
  • Try to give the simplest possible example that reproduces your problem. It looks like you are using some Javascript libraries that may be confounding the issue. Can you confirm that static files are in fact a problem by giving a simple example? In other words, you try to access http://localhost/static_file.html but you get an error message?
    – cherdt
    May 11, 2020 at 18:56
  • Use nginx -T (uppercase T) to view the entire configuration that Nginx is reading and ensure that there are no errors and warnings, and that that is the only server block. Also, look at the access log and error log. May 11, 2020 at 19:01
  • Hi @cherdt and Hi Richard ! Thank you for your kind help. I updated my question with more information May 11, 2020 at 19:36
  • @RichardSmith what useful clues can we grasp from the output of nginx -T, the access log and error log? I see some "Connection refused" but I do not understand why May 12, 2020 at 16:45
  • Yes. The server block in your question is not being used. You have default servers defined that send all requests to a service listening on port 8080. You should be adding new locations to the existing server block, not adding new server blocks unless you are also defining a new server_name. May 12, 2020 at 17:25

1 Answer 1

2

You have added location /weights to download a static file at /home/user/www/weights/foo.json using the URI /weights/foo.json.

The path to a file is constructed by concatenating the value of the root with the URI. So the root needs to be set to /home/user/www as the /weights/foo.json part is provided by the URI. See this document for details.

The simplest form would be:

location /weights {
    root /home/user/www;
}

Using try_files you can control what happens if the file is not found. But parameters are like URIs not pathnames. See this document for details.

For example:

location /weights {
    root /home/user/www;
    try_files $uri $uri/ =404;
}

The two location blocks have essentially identical behaviour.

1
  • now it works fine. Thank very much for your very kind help. Marco May 14, 2020 at 9:40

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .