我有这个 nginx 配置:
# Default server definition
server {
listen [::]:8080 default_server;
listen 8080 default_server;
server_name _;
tcp_nodelay on;
absolute_redirect off;
#this one works perfectly, it is webserver+ws by its own implementation
location /backend/firstws {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d; # dont kill connection after 60s of inactivity
proxy_pass http://127.0.0.1:7898/;
}
#this one also, no any issues, it is pure ws server on nodejs
location /backend/secondws/ {
add_header Access-Control-Allow-Origin *;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d; # dont kill connection after 60s of inactivity
proxy_pass http://127.0.0.1:3000/socket.io/;
}
#and here i'm trying to proxify requests to noVNC via NGINX.
location /backend/vnc/ {
proxy_pass http://127.0.0.1:5800/;
}
# Redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/lib/nginx/html;
}
# Set the cache-control headers on assets to cache for 3 hours
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 1h;
}
# Deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
这个块有些错误:
#and here i'm trying to proxify requests to noVNC via NGINX.
location /backend/vnc/ {
proxy_pass http://127.0.0.1:5800/;
}
它完美地代理了index.html、目录,但是当尝试加载任何js文件时就会发生这种情况:
目录-好的: 图1
目录中的 js 文件 - 404???如何?这里是: 图2
我用 try_files 尝试了很多,但没有人工作。 现在我已经将所有资产放在nginx之前,它可以工作,但这不是最好的方法,因为它需要在资产更改时重写conf文件。 我期望所有文件,而不仅仅是目录,都将由 nginx 提供服务,没有任何问题。 将非常高兴聆听 nginx 专家的意见。谢谢大家的宝贵时间,即使您只阅读了我的问题!
参见 Nginx 如何处理请求。
Nginx 收到 URL
/backend/vnc/foo.js
后,会寻找 location
来处理请求。
正则表达式
\.(jpg|jpeg|gif|png|css|js|ico|xml)$
与URL匹配,因此使用location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$
来处理请求。
虽然
location /backend/vnc/
也匹配 URL,但正则表达式位置优先。
要覆盖此行为,请使用
^~
运算符,例如:
location ^~ /backend/vnc/ {
proxy_pass http://127.0.0.1:5800/;
}
^~
表示此前缀位置将优先于任何正则表达式位置。
如果您仍然需要在某些 URL 模式上使用
expires
指令,请使用 map
(参见此示例)以避免破坏位置搜索。