使Nginx Proxy仅在Docker Compose堆栈中升级SignalR WebSocket连接

问题描述 投票:0回答:1

有没有办法只在你的nginx.conf文件中升级SignalR WebSocket连接而不重复整个部分?

ASP.NET Core在未升级时拒绝我的SignalR连接,但是我在该ASP.NET Core项目上运行API,当请求具有标题“Connection:”升级时,该API不接受带有正文的POST请求“”。

我目前的解决方案是复制我的nginx.conf文件中的“位置”部分,如下所示。有更优雅的解决方案吗?

谢谢!

我的nginx.conf文件:

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;

        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            proxy_pass http://ui:80;
            rewrite ^/(.*)$ /$1 break;
        }

        location /shopvac/signalr {
            proxy_pass http://shopvac:80;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            rewrite ^/shopvac/(.*)$ /$1 break;
        }

        location /shopvac {
            proxy_pass http://shopvac:80;
            rewrite ^/shopvac/(.*)$ /$1 break;
        }
    }
}

我的docker-compose.yaml文件:

version: "3"

networks:
  proxy:
  database:

volumes:
  database-data:

services:
  proxy:
    image: nginx:latest
    depends_on:
      - ui
      - shopvac
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./logs/nginx:/var/log/nginx
    ports:
      - "80:80"
      - "443:443"
    networks:
      - proxy
  database:
    image: postgres:latest
    volumes:
      - database-data:/var/lib/postgresql/data
    expose:
      - "5432"
    networks:
      - database
    environment:
      - POSTGRES_DB=home-system
      - POSTGRES_USER=home-system
  ui:
    build:
      context: ./ui
      dockerfile: Dockerfile
    expose:
      - "80"
    networks:
      - proxy
  shopvac:
    build:
      context: ./shopvac
      dockerfile: Dockerfile
    depends_on:
      - database
    volumes:
      - /app
    expose:
      - "8080"
    networks:
      - proxy
      - database
docker nginx asp.net-core docker-compose signalr
1个回答
0
投票

找到了解决方案!

因此,您可以编辑nginx.conf中的“proxy_set_header连接”属性以向前传递必要的值。

nginx.conf的重要部分现在如下:

location /shopvac {
    proxy_pass http://shopvac:80;
    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;
    rewrite ^/shopvac/(.*)$ /$1 break;
}
© www.soinside.com 2019 - 2024. All rights reserved.