無法使用 Nginx 從 express 應用程序將 .gz 文件作為靜態文件訪問
我在文件夾上有一個快速應用程序,在這個應用程序上我有一個包含一些文件
/home/xxx/example.com/public_html/APIBackend
的靜態文件文件夾。/home/xxx/example.com/public_html/APIBackend/sitemapfiles``.gz
在 express 應用程序中,我有程式碼來提供這樣的靜態文件:
app.use('/sitemapfiles', express.static(path.join(__dirname, '../../sitemapfiles'))) // works on localhost
但是當從example.com/sitemapfiles/sitemap.gz等域訪問 gz 文件時,它顯示來自 Ngnix 的 404 err。所以我認為問題來自 Ngnix,我有 Ngnix 配置文件如下:
server { listen 80; server_name example.com www.example.com; root /home/xxx/example.com/public_html; location / { proxy_buffering off; proxy_pass http://127.0.0.1:3000; proxy_set_header X-Client-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_hide_header Upgrade; } include /etc/nginx/extra/staticfiles.conf; include /etc/nginx/extra/security.conf; } server { listen 443 ssl http2; server_name example.com www.example.com; return 404; error_page 404 /hvn_404.html; root /home/xxx/example.com/public_html; ssl_certificate /etc/nginx/ssl/server/server.crt; ssl_certificate_key /etc/nginx/ssl/server/server.key; location / { proxy_buffering off; proxy_pass http://127.0.0.1:3000; proxy_set_header X-Client-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_hide_header Upgrade; } include /etc/nginx/extra/staticfiles.conf; include /etc/nginx/extra/security.conf; }
靜態文件.conf
location = /favicon.ico { allow all; log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location ~* \.(gif|jpg|jpeg|png|ico|webp)$ { gzip_static off; brotli_static off; #add_header Access-Control-Allow-Origin *; add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800"; access_log off; expires 365d; break; } location ~* \.(3gp|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ { gzip_static off; brotli_static off; sendfile off; sendfile_max_chunk 1m; #add_header Access-Control-Allow-Origin *; add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800"; access_log off; expires 365d; break; } location ~* \.(js)$ { #add_header Access-Control-Allow-Origin *; add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800"; access_log off; expires 365d; break; } location ~* \.(css)$ { #add_header Access-Control-Allow-Origin *; add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800"; access_log off; expires 365d; break; } location ~* \.(eot|svg|ttf|woff|woff2)$ { #add_header Access-Control-Allow-Origin *; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; access_log off; expires 365d; break; }
安全配置文件
location ^~ /GponForm/ { deny all; access_log off; log_not_found off; } location ^~ /GponForm/diag_Form { deny all; access_log off; log_not_found off; } # Return 403 forbidden for readme.(txt|html) or license.(txt|html) or example.(txt|html) or other common git repository files location ~* "/(^$|readme|license|example|LICENSE|README|LEGALNOTICE|INSTALLATION|CHANGELOG)\.(txt|html|md)" { deny all; } location ~ ^/(\.user.ini|\.htaccess|\.htpasswd|\.user\.ini|\.ht|\.env|\.git|\.svn|\.project) { deny all; access_log off; log_not_found off; } # Deny backup extensions & log files and return 403 forbidden location ~* "\.(love|error|kid|cgi|old|orig|original|php#|php~|php_bak|save|swo|aspx?|tpl|sh|bash|bak?|cfg|cgi|dll|exe|git|hg|ini|jsp|log|mdb|out|sql|svn|swp|tar|rdf|gz|zip|bz2|7z|pem|asc|conf|dump)$" { deny all; access_log off; log_not_found off; }
那麼在這種情況下如何提供 gz 文件。我是 devOps 的新手,只是在網上關注 tuts,我對 Ngnix 配置了解不多。
謝謝你的幫助
更新,當我訪問 url 時
example.com/APIBackend/sitemapfiles/file.gz
,它可以工作。那麼如何使 url 像mydomain.com/sitemapfiles/file.gz
作品一樣有效呢?
您
root
的設置為/home/xxx/example.com/public_html
.這意味著,請求
https://example.com/APIBackend/sitemapfiles/file.gz
將在/home/xxx/example.com/public_html/APIBackend/sitemapfiles/file.gz
.如果要從不同目錄提供文件,則需要使用
alias
指令:location /sitemapfiles { alias /home/xxx/example.com/public_html/APIBackend/sitemapfiles; try_files $uri $uri/ =404; }
此外,您的配置將所有流量代理到您的應用程序。
location ~
由於 nginx處理位置的方式,沒有應用任何塊。location /
覆蓋您的其他位置。