I have successfully configurated Nginx as a reverse proxy for my web-application. It correctly redirects requests made from my Angular SPA to Web API written in Asp Core 2.1. However, in my Web API I want to use WebSockets that use 
ws://mydomain.com/ws to establish a connection.
This is how my sites-available looks now
server {
    listen 80;
    listen 443;
    ssl on;
    ssl_certificate /etc/***/***.crt;
    ssl_certificate_key /etc/***/***.key;
    root /home/***/***/***/wwwroot;
    # Add index.php to the list if you are using PHP
    index index.html;
    server_name my.domain.com;
    location / {
            try_files $uri $uri/ /index.html;
    }
    location /api/ {
            proxy_pass http://localhost:5000;
    }
    location /api/signalr {
            proxy_pass http://localhost:5000;
            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;
    }
    location /ws{
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_pass http://localhost:5000;
    }
}
and the nginx.conf
http {
    map $http_upgrade $connection_upgrade{
            default upgrade;
            '' close;
    }
    upstream websocket{
            server my.domain.com;
    }
    #
    # Basic Settings
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ##
    # SSL Settings
    ##
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    ##
    # Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    ##
    # Gzip Settings
    ##
    gzip on;
    gzip_disable "msie6";
    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    ##
    # Virtual Host Configs
    ##
}
Any advice how can I debug my configuration to see why the requests are not redirected correctly?
Found the solution by adding upstream to my nginx.conf file.
http {
    map $http_upgrade $connection_upgrade{
            default upgrade;
            `` close;
    }
    upstream websocket{
        server 127.0.0.1:58550; 
        #SERVER endpoint that handle ws:// connections
    }
    server{
        #Now you can connect via ws:// protocol on ws://yourdomainaddress:8020/ws
        listen 8020;
        location /ws {
                proxy_pass http://websocket;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }
    } 
}
Hope someone finds it useful :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With