我对托管和 nginx 非常陌生,对 linux 也相当陌生,刚刚开始使用 nginx 在我的 raspberry pi(仅限内联网)上托管一个 web 应用程序。
它在 上提供角度 SPA /
,重定向到 上的 .NET API /api
,并重定向到 上的 signalR 集线器/realTimeFeed
(如果您不了解 signalR,这基本上意味着 websockets)。
/api
一切似乎都运行良好,但正如您在下面看到的,和之间存在很多冗余/realTimeFeed
。
我怎样才能改变这一点?应该我甚至改变了这一点?这真的正确吗?
我的参考资料如下(诚然,我从这些链接中复制了我的解决方案):
- https://github.com/diginex/nginx-spa/blob/master/default.conf(温泉)
- https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1#configure-nginx(API)
- https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/blazor/server?view=aspnetcore-3.1#linux-with-nginx(signalR/websockets)
下面是我的 nginx 配置文件/etc/nginx/sites-available/default
。
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html;
location /api {
proxy_pass http://localhost:5000/api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /realTimeFeed {
proxy_pass http://localhost:5000/realTimeFeed;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
root /usr/share/nginx/html;
try_files $uri /index.html; # redirect all request to index.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
为了使 signalR / websockets 正常工作,我还将此部分添加到/etc/nginx/nginx.conf
:
# For SignalR
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}
如果这与主题无关或属于其他地方,请告诉我:)
编辑:
我尝试了以下方法,但没有奏效。无效意味着 websocket 连接未正确代理,客户端抛出错误,提示无法建立连接。
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location /api {
proxy_pass http://localhost:5000/api;
proxy_set_header Connection keep-alive;
}
location /realTimeFeed {
proxy_pass http://localhost:5000/realTimeFeed;
proxy_set_header Connection $connection_upgrade;
}
location / {
root /usr/share/nginx/html;
try_files $uri /index.html; # redirect all request to index.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
答案1
我是否应该改变这一点?
如果它运行正常,我会说不。
这到底正确吗?
proxy_pass
从技术上来说,代理基本请求不需要其他选项。但是,如果您愿意,您可以尝试逐个删除附加指令(每个location
),直到出现问题为止。;)
更严重的是,您指定的其他指令可能出于其他原因(包括允许您的应用程序在代理后正常运行)是可取的/必要的。如果 Microsoft 推荐了这些设置,那么背后可能有一些想法。
/api
一切似乎都运行良好,但正如您在下面看到的,和之间存在大量冗余/realTimeFeed
。我该如何改变这种情况?
您可以尝试使用include
指令来包含您的常见代理设置,例如:
例如 common_proxy_settings.conf
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
例如“包含”服务器块
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html;
location /api {
proxy_pass http://localhost:5000/api;
include /path/to/common_proxy_settings.conf
}
location /realTimeFeed {
proxy_pass http://localhost:5000/realTimeFeed;
include /path/to/common_proxy_settings.conf
}
location / {
root /usr/share/nginx/html;
try_files $uri /index.html; # redirect all request to index.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}