Seems like nginx proxy server doen't work - nginx

I'm newbie in linux and nginx hosting. I have a simple asp net core app that I need to run on https://localhost:5050. So I send a request on https://localhost and wait that nginx process it to :5050 port. But it doesn't..
I created \etc\nginx\sites-available\aspnetcore.conf with the following code
server {
listen 443;
location / {
proxy_pass https://localhost:5050;
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;
}
}
and a symbolinc link to /etc/nginx/sites-enabled/aspnetcore.conf.
Also I have in Startup.cs file
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
So my app doesn't get any request. What am I doing wrong?
UPD:
The problem in https contact. I configured ssl as it describes here
https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-on-debian-9
But still there are no requests...

upstream backend {
server localhost:5050;
}
server {
listen 443;
location / {
proxy_pass http://backend;
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;
}
}

Related

NGINX Problem with Configuring Websocket, no problem with SSE

i am working with NGINX configuration right now.
I am stuck at some strange point.
NGINX allow me to connect via SSE but not via Websocket.
This is my config:
server {
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:5000;
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;
}
}
This is error from browser console via WEBSOCKET:
[2022-08-30T07:49:22.667Z] Error: Failed to start the transport 'WebSockets': Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.
This is WEBSOCKET: Browser Error
This is SSE: SSE
##EDIT:
This is section which fail:
try {
await this._startTransport(connectUrl, requestedTransferFormat);
this.connectionId = negotiate.connectionId;
return;
}
catch (ex) {
this._logger.log(LogLevel.Error, `Failed to start the transport '${endpoint.transport}': ${ex}`);
negotiate = undefined;
transportExceptions.push(new FailedToStartTransportError(`${endpoint.transport} failed: ${ex}`, HttpTransportType[endpoint.transport]));
if (this._connectionState !== "Connecting" /* Connecting */) {
const message = "Failed to select transport before stop() was called.";
this._logger.log(LogLevel.Debug, message);
return Promise.reject(new Error(message));
}
###UPDATE
I FOUND THE SOLUTION. MY ANSWER IS CORRECT ANSWER! :)
I am comming back with the solution.
It is easy.
First thing is:
You need to add in nginx.conf in http { } section this:
map $http_upgrade $connection_upgrade
{
default upgrade;
'' close;
}
and this is config:
server {
listen 50150;
server_name _;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache off;
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;
}
}
and you are good to go!
[2022-08-30T10:30:48.900Z] Information: WebSocket connected to ws:~~/~hub?id=Zk-~~.
I hope someone will get use of it.
Best regards

How can I properly configure nginx to work with Angular with using hot reload?

I run my server app with command: ng serve and get error in browser console:
Firefox cannot connect to the wss://dev.domain.com/ws server.
[webpack-dev-server] Trying to reconnect...
or on chrome:
WebSocket connection to 'wss://dev.domain.com/ws' failed
[webpack-dev-server] Disconnected!
[webpack-dev-server] Trying to reconnect...
My config nginx is:
location ^~ / {
proxy_pass http://localhost:4200;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
location ^~ /sockjs-node/ {
proxy_pass http://localhost:4200;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
location ^~ /ws/ {
proxy_pass http://localhost:4200;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
This should work, but it doesn't. my configuration is wrong?
That works for me for Angular 14 :
location /ng-cli-ws {
proxy_pass http://host-gateway:4200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
If your app is using /ws, try to remove ending slash, else nginx responds with 301 to /ws/...
I second Vlad's answer, but I would like to elaborate on the solution.
I wanted my Angular application to be able to reload any changes at development time and be able to display them through HTTPS. So the following is what is working fine for me. It includes configuration managed by Certbot; and omits an additional default server block.
upstream frontend {
server 0.0.0.0:4200 fail_timeout=3s max_fails=3;
server 0.0.0.0:80 backup;
}
upstream wsfront {
ip_hash;
server 0.0.0.0:4200;
}
server {
server_name my.domain.net;
location ~ ^/ng-cli-w(s|s/)$ {
proxy_pass http://wsfront;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://frontend;
}
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/my.domain.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/my.domain.net/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = my.domain.net) {
return 301 https://$host$request_uri;
}
listen 80 ;
listen [::]:80 ;
server_name my.domain.net;
return 404;
}

Nginx Proxy to multiple ports from same location

I currently have this configuration
server {
listen 4000;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_redirect off;
}
upstream websocket {
server localhost:4444;
}
upstream websocket2 {
server localhost:4445;
}
With this configuration, all requests from localhost:4000 are being proxied to localhost:4444.
I don't want to proxy those request only to localhost:4444 but also to localhost:4445
localhost:4000 proxies requests to
localhost:4444
localhost:4445
Is it possible?
From the Nginx documentation here, you can declare an upstream with multiple servers. Then you can forward the traffic to this upstream and the traffic will be dispatched between servers. So just put your servers in 1 upstream websocket to archive what you want.
upstream websocket {
server localhost:4444;
server localhost:4445;
}
server {
listen 4000;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_redirect off;
}

Nginx: How to host 2 different websites on same server?

Here's what I have in my /etc/nginx/sites-available/ folder
allverse.co
server {
listen 80;
server_name allverse.co www.allverse.co;
root /var/www/allverse;
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 $host;
proxy_cache_bypass $http_upgrade;
}
}
jademgr.com
server {
listen 80;
server_name jademgr.com www.jademgr.com;
root /var/www/jademgr;
location / {
proxy_pass http://localhost:5010;
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;
}
}
I have them running under supervisor.
[program:jademgr]
command=/usr/bin/dotnet /var/www/jademgr/jademgr.dll
directory=/var/www/jademgr/
autostart=true
autorestart=true
stderr_logfile=/var/log/jademgr.err.log
stdout_logfile=/var/log/jademgr.out.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=www-data
stopsignal=INT
allverse
[program:allverse]
command=/usr/bin/dotnet /var/www/allverse/allverse.dll
directory=/var/www/allverse/
autostart=true
autorestart=true
stderr_logfile=/var/log/allverse.err.log
stdout_logfile=/var/log/allverse.out.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=www-data
stopsignal=INT
I have allverse set to run on port 5000 and jademgr set to run on port 5010. What am I doing wrong? When I start both sites they both point to the same jademgr site.
Thanks.

Accept only subdomain in nginx

I have a reverse proxy set up in nginx for a node.js server.
server {
listen 80;
server_name sub.domain.tld;
location / {
proxy_pass http://localhost:3000;
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;
}
}
It works all fine and dandy, however, I only want sub.domain.tld to work. Opening up domain.tld in a browser still routes to the node.js server.
try to add this
server {
listen 80;
server_name domain.tld;
return 404;
}

Resources