我试图弄清楚如何使用 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)
由于我的知识缺乏,我想填补这一点,我不清楚应该使用哪个具体网址来检查是否可以访问静态文件 /home/marco/webMatters/vueMatters/GraspGlobalChances/src/components/auth/weights /ssd_mobilenetv1_model-shard1
我尝试使用 localhost/sd_mobilenetv1_model-shard1.html :
https://drive.google.com/open?id=14YkcqCd_FAax9BYBnGZXBQsi4NZ3wUnC这是 nginx -T 的输出: https://drive.google.com/open?id=1eTry9p4RLHWFDaHS3QiPhzeEwIFADR4Y
- 这是错误日志: https://drive.google.com/open?id=1lwylAPCBqWV31jCocTLxLvnw9v2iSfl_
- 这是访问日志: https://drive.google.com/open?id=1HKIe8RQoLlwfGQ6FFYzN3WLZbaqjCVjM
更新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.json
URI 提供的部分。看这个文件了解详情。
最简单的形式是:
location /weights {
root /home/user/www;
}
使用try_files
您可以控制找不到文件时发生的情况。但参数就像 URI,而不是路径名。看这个文件了解详情。
例如:
location /weights {
root /home/user/www;
try_files $uri $uri/ =404;
}
这两个位置块具有本质上相同的行为。