我有一个与 Asp.Net core API 对话的 React 应用程序。它们都部署在我的虚拟机上的 Docker 上。已安装并配置了 Nginx 来解析应用程序的域名(thesis.uno - 用于 React 应用程序,api.thesis.uno - 用于 asp.net core api)
我使用 SignalR 为我的应用添加了聊天支持,但当 React 尝试通过 api.thesis.uno 建立 wss 连接时失败
当我用虚拟机 IP 和端口替换 api 域名时,一切似乎都正常运行,这意味着问题出在 Nginx 配置上。
我尝试用谷歌搜索这个问题,但似乎没有解决方案可以帮助我(https://stackoverflow.com/questions/48300288/signalr-in-asp-net-core-behind-nginx,https://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss)
我的 nginx 配置:
client_max_body_size 64M;
upstream backend {
# enable sticky session based on IP
ip_hash;
server localhost:5000;
}
server {
root /var/www/thesis.uno/html; # Directly serves anything in the Rails public folder
index index.html index.htm index.nginx-debian.html;
server_name thesis.uno www.thesis.uno; # managed by Certbot
location / {
proxy_pass http://localhost:5001;
proxy_set_header Host $host;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/thesis.uno/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/thesis.uno/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.thesis.uno) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = thesis.uno) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
server_name thesis.uno www.thesis.uno;
return 404; # managed by Certbot
}
server {
server_name api.thesis.uno www.api.thesis.uno;
# all other requests
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
}
# web socket chat requests
location /api/chat {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.thesis.uno/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.thesis.uno/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.api.thesis.uno) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = api.thesis.uno) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name api.thesis.uno www.api.thesis.uno;
return 404; # managed by Certbot
}
更新:
原来 nginx 有一个日志文件,似乎 nginx 无法正确重定向请求,请求时给出 404
答案1
这是否可能与需要粘性会话以及此评论中提到的“随机 404”有关https://github.com/dotnet/AspNetCore.Docs/issues/10827#issuecomment-631088853