我希望浏览器与后端服务器保持持续通信(websocket),因为它们不断接收数据。但我还希望 nginx 向它们提供静态文件。换句话说,我希望 nginx 提供静态文件,并且还代理我的后端服务器。我尝试将两个指令放在同一个位置块中:
location / {
# SERVE STATIC FILES:
root C:blah/blah/blah;
index mysite.html;
# ANDDDD REVERSE PROXY TO BACKEND SERVERS:
proxy_pass https://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
..但这只是跳过了静态文件的服务并直接进入我的后端服务器。
我也尝试过:
location / {
# SERVE STATIC FILES:
root C:blah/blah/blah;
index mysite.html;
try_files $uri $uri/ @proxy;
}
location @proxy {
# ANDDDD REVERSE PROXY TO BACKEND SERVERS:
proxy_pass https://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
..但当然这只是提供静态文件,并且由于找到了 mysite.html,所以不会继续使用代理。
我也尝试过:
location / {
# SERVE STATIC FILES:
root C:blah/blah/blah;
index mysite.html;
}
location / {
# ANDDDD REVERSE PROXY TO BACKEND SERVERS:
proxy_pass https://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
.. 但这会产生错误,因为现在有两个位置/块。
有谁知道如何让 nginx 提供静态文件和代理到后端服务器?
答案1
index mysite.html
指令使 nginx 服务器mysite.html
处理所有以 结尾的请求/
。
如果您只想在找不到静态文件时从后端提供文件,则需要:
location / {
# SERVE STATIC FILES:
root C:blah/blah/blah;
try_files $uri $uri/ @proxy;
}
location @proxy {
# ANDDDD REVERSE PROXY TO BACKEND SERVERS:
proxy_pass https://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}