2

I am trying to make a website that would have http functions including http post functions, and also web sockets (such as signalR). I am trying to host this website on an ubuntu server using nginx. Generally the set up is something like this on nginx:

server {
 listen 80;
 location / {
    proxy_pass http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
 }
}

However, I later found that I need to add proxy_set_header Connection "upgrade"; in order to use websockets. However, adding this line in a .net core project results in all http post requests showing a 400 error as shown here 400 status error when posting form data in ASP.Net Core. Is there a set up that would allow both posts and web sockets?

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79

3 Answers3

8

using $http_connection instead of keep-alive or upgrade

proxy_set_header Connection $http_connection;
Kevin
  • 81
  • 1
  • 2
  • Perfect! It works! Could you please give a short explanation what this change does? – Alex K Dec 18 '18 at 17:34
  • This works partially for me. Websocket works, POST request works but now I have a new problem where after logging it with Identity Server, it redirects me to the callback page without any parameters. – Desmond Chin Aug 15 '19 at 15:15
0

This is not a fix to your question, but may be a workaround. I got around this by setting up 2 proxy locations in nginx to a single asp.net core kestrel site. One for the websockets with the

proxy_cache_bypass $http_upgrade;

and one for everything else. Looks like this. Top one is for all but websockets, bottom is for websockets. Note I have rewrites also.

location /n/ {
        proxy_pass http://localhost:5001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
}

location /nsock/ {
        proxy_pass http://localhost:5001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
}
kapieyow
  • 1
  • 1
  • Why are you rewriting every request? – miknik Jul 03 '18 at 21:39
  • Using the top configuration, it basically removes "n/" from the URL as it proxies the asp.net core site. For example this URL http://hostname/n/api/entries is proxied to the 5001 port as http://hostname:5001/api/entries. Note there is no "n/". I am doing this because there are several kestrel instances running on different ports and I proxy to them from different "sub web folders" in the regular 80/443 nginx ports. – kapieyow Jul 07 '18 at 00:55
  • No need for a rewrite, which causes Nginx to process the request all over again. You just need to change your proxy pass to `proxy_pass http://localhost:5001/;`, same for the second directive – miknik Jul 07 '18 at 02:05
  • Thank you! I didn't know the trailing slash would work like that on proxy_pass. Works exactly as you said w/o the rewrite. This is running on a Pi so it thanks you for not having process each request twice in nginx. I edited my snippet above in case anyone stumbles across it. – kapieyow Jul 07 '18 at 15:20
  • It works like this: `proxy_pass` to a naked url, i.e. nothing at all after domain/ip/port and the full client request url gets passed to the proxy. Add anything, even just a slash to the `proxy_pass` and whatever you add replaces the part of the client request url which matches that location block. So if you had `location /foo { proxy_pass http://example.com/bar;` then a client request for `example.com/foo/baz` would get proxied to `example.com/bar/baz` – miknik Jul 07 '18 at 15:36
0

Here is my configuration for .Net Core API with nginx:

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_http_version 1.1;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Proto $scheme;

The main idea are remove these lines (if you have)

proxy_set_header Connection "upgrade";
proxy_set_header Connection keep-alive;

and insert this line in your configuration

proxy_set_header Connection $http_connection;

Hope it helps.

Tang Thanh Tam
  • 431
  • 3
  • 12