nginx配置:如何加载index.html以外的静态文件

nginx配置:如何加载index.html以外的静态文件

我试图弄清楚如何使用 Ubuntu 18.04.4 Desktop 在 nginx 中加载除 index.html 之外的静态文件。

我将以下几行添加到 /etc/nginx/conf.d/default.conf 中:

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

但我在控制台日志中收到此错误:Uncaught (in Promise) SyntaxError: Unexpected token < in JSON atposition 0

据参与 github 问题的人员称,这应该与 Web 服务器中静态文件服务的错误配置有关:https://github.com/justadudewhohacks/face-api.js/issues/598#issuecomment-626346393

按照此处的指示进行操作:https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/ 我修改了 /etc/nginx/conf.d/default.conf 中的行,如下所示:

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

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

我还尝试添加这两个选项:

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 4 月 7 日 12:57 权重

但仍然遇到问题

更新1)

更新2)

在 /etc/nginx/conf.d/default.conf 中,我向第一个服务器块添加了位置 /weights。但我不太确定这个位置的质量和正确性。

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

}

我尝试通过网络。但 error.log 说:

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$

更新3)

按照此处找到的指示进行操作:Nginx 尝试加载目录中的文件时未找到 404我将所有静态文件移动到目录 /home/marco/www 中,将该文件夹的所有权更改为 root,并将其内容的 mod 更改为 755: sudo chown root www + sudo chmod 755 www

然后在/etc/nginx/conf.d/default.conf中我修改了配置如下:

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

}

但是当尝试通过 url 访问静态文件时“https://ggc.world/weights/mtcnn_model-weights_manifest.json“ 我收到此错误:“未捕获的语法错误:意外的标记 '<' ”

在此输入图像描述

如何解决问题?期待您的好意帮助。马可

答案1

您已添加使用 URIlocation /weights下载静态文件。/home/user/www/weights/foo.json/weights/foo.json

文件的路径是通过将 的值root与 URI 连接起来构建的。因此root需要将其设置/home/user/www为由/weights/foo.jsonURI 提供的部分。看这个文件了解详情。

最简单的形式是:

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

使用try_files您可以控制找不到文件时发生的情况。但参数就像 URI,而不是路径名。看这个文件了解详情。

例如:

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

这两个位置块具有本质上相同的行为。

相关内容