我已将 Nginx 设置为 Node 的反向代理,并为我的静态文件和用户上传的图像提供服务。
一切都运行良好,只是我不明白为什么 Nginx 找不到我的 .zip 文件。这是我的 nginx.conf。
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
proxy_cache_path /var/www/web_cache levels=1:2 keys_zone=ooparoopaweb_cache:8m max_size=1000m inactive=600m;
sendfile on;
upstream *******_node {
server 172.27.198.66:8888 max_fails=3 fail_timeout=20s;
#fair weight_mode=idle no_rr
}
upstream ******_json_node {
server 172.27.176.57:3300 max_fails=3 fail_timeout=20s;
}
server { #REDIRECT ALL HTTP REQUESTS FOR FRONT-END SITE TO HTTPS
listen 80;
server_name *******.com www.******.com;
return 301 https://$host$request_uri;
}
server { #MOBILE APPLICATION PROXY TO NODE JSON
listen 3300 ssl;
ssl_certificate /*****/*******/json_ssl/server.crt;
ssl_certificate_key /*****/******/json_ssl/server.key;
server_name json.*******.com;
location / {
proxy_pass http://******_json_node;
proxy_redirect off;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto https;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 90s;
proxy_send_timeout 90s;
proxy_read_timeout 90s;
proxy_buffers 32 4k;
}
}
server { #******.COM FRONT-END SITE PROXY TO NODE WEB SERVER
listen 443 ssl;
ssl_certificate /***/***/web_ssl/********.crt;
ssl_certificate_key /****/*****/web_ssl/myserver.key;
server_name mydomain.com www.mydomain.com;
add_header Strict-Transport-Security max-age=500;
location / {
gzip on;
gzip_types text/html text/css application/json application/x-javascript;
proxy_pass http://mydomain_node;
proxy_redirect off;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto https;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 90s;
proxy_send_timeout 90s;
proxy_read_timeout 90s;
proxy_buffers 32 4k;
}
}
server { #ADMIN SITE PROXY TO NODE BACK-END
listen 80;
server_name admin.mydomain.com;
location / {
proxy_pass http://mydomain_node;
proxy_redirect off;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 90s;
proxy_send_timeout 90s;
proxy_read_timeout 90s;
proxy_buffers 32 4k;
}
}
server { # SERVES STATIC FILES
listen 80;
listen 443 ssl;
ssl_certificate /**/*****/server.crt;
ssl_certificate_key /****/******/server.key;
server_name static.domain.com;
access_log static.domain.access.log;
root /var/www/mystatic/;
location ~*\.(jpeg|jpg|png|ico)$ {
gzip on;
gzip_types text/plain text/css application/json application/x-javascript text/xml
application/xml application/rss+xml text/javascript image/svg+xml
application/vnd.ms-fontobject application/x-font-ttf font/opentype image/png image/jpeg
application/zip;
expires 10d;
add_header Cache-Control public;
}
location ~*\.zip {
#internal;
add_header Content-Type "application/zip";
add_header Content-Disposition "attachment; filename=gamezip.zip";
}
}
}
include tcp.conf;
Tcp.conf 包含允许 Nginx 代理 websocket 的设置。我认为其中包含的任何内容都与这个问题无关。我还想补充一点,我希望强制下载 zip 文件。
答案1
尝试像这样重写它:
location ~ .*\.zip$ {
add_header Content-Type "application/zip";
add_header Content-Disposition "attachment; filename=gamezip.zip";
}
此外,如果上述方法不起作用,作为一种黑客攻击,尝试重新定义位置内的根:
location ~ .*\.zip$ {
root /var/www/mystatic/;
add_header Content-Type "application/zip";
add_header Content-Disposition "attachment; filename=gamezip.zip";
}
最后,出于对 $DEITY 的热爱,请从您的 gzip 类型中删除 application/zip:
gzip_types text/plain text/css application/json application/x-javascript text/xml
application/xml application/rss+xml text/javascript image/svg+xml
application/vnd.ms-fontobject application/x-font-ttf font/opentype image/png image/jpeg **application/zip;**
由于 .zip 文件已被压缩,因此它毫无用处!