无法使用 Nginx 从 Express 应用程序访问 .gz 文件作为静态文件

无法使用 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

但是当从域访问 gz 文件时example.com/sitemapfiles/sitemap.gz,它显示 Ngnix 的 404 错误。所以我认为问题出在 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 新手,只是在网上关注教程,对 Ngnix 配置不太了解。

谢谢你的帮助


更新,当我访问 url like 时example.com/APIBackend/sitemapfiles/file.gz,它​​可以正常工作。那么如何让 url likemydomain.com/sitemapfiles/file.gz正常工作?

答案1

您的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 /将覆盖您的其他位置。

相关内容