nginx配置:如何載入index.html以外的靜態文件
我試圖弄清楚如何使用 Ubuntu 18.04.4 桌面在 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 at position 0
根據參與 github 問題的人員的說法,這應該與網路伺服器中的靜態文件服務配置錯誤有關:https ://github.com/justadudewhohacks/face-api.js/issues/598#問題comment-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)
- 由於我的知識缺乏,我想填寫,我不清楚我應該使用什麼具體的 url 來檢查我是否可以訪問靜態文件 /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 ”訪問靜態文件時,我得到這個錯誤:“Uncaught Syntax Error: Unexpected token ‘<’”
如何解決問題?期待您的幫助。馬可
您已添加
location /weights
以/home/user/www/weights/foo.json
使用 URI下載靜態文件/weights/foo.json
。文件的路徑是通過連接
root
URI 的值來構造的。所以root
需要設置為由URI提供/home/user/www
的部分。/weights/foo.json
有關詳細資訊,請參閱此文件。最簡單的形式是:
location /weights { root /home/user/www; }
使用
try_files
您可以控制如果找不到文件會發生什麼。但是參數就像 URI 而不是路徑名。有關詳細資訊,請參閱此文件。例如:
location /weights { root /home/user/www; try_files $uri $uri/ =404; }
這兩個位置塊具有基本相同的行為。